Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Change text generation
Browse files Browse the repository at this point in the history
Ref #6
  • Loading branch information
LeoColomb committed Jan 9, 2019
1 parent da96f92 commit c6c0dc9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Cela permet d'une part de **mieux connaître ces actionnaires majoritaires** via
```


## Sources

* Source 1


## License

MIT © Design & Human/Geoffrey Dorne & Léo Colombaro
2 changes: 1 addition & 1 deletion source/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function init () {

const text = document.createElement('p')
text.className = 'qui-possede-les-medias-text'
text.innerHTML = `Ce média appartient à ${renderData(boxData)}.`
text.innerHTML = `Ce média ${renderData(boxData)}.`
box.appendChild(text)

select('body').append(box)
Expand Down
30 changes: 26 additions & 4 deletions source/libs/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
export const isEmpty = variable => (!variable || variable.length === 0 || Object.getOwnPropertyNames(variable).length === 0)

export const renderData = (data) => data
.filter(entity => entity.type === 'holder')
.map(entity => entity.link ? `<a href="${entity.link}" rel="noopener noreferrer" target="_blank">${entity.name}</a>` : entity.name)
.join(', ')
export const renderData = (data) => [
renderPlural(data.filter(entity => entity.type === 'group'), 'du groupe', 'des groupes'),
renderPlural(data.filter(entity => entity.type === 'holder'), 'appartient à', 'appartient à'),
].join(' ')

export const renderPlural = (data, preSingular, prePlural) => {
if (data.length < 1) {
return null
}
data = data.map(entity => entity.link
? `<a href="${entity.link}" rel="noopener noreferrer" target="_blank">${entity.name}</a>`
: entity.name
)
if (data.length === 1) {
return `${preSingular} ${data.pop()}`
}
if (typeof Intl !== 'undefined' && Intl.ListFormat) {
const lf = new Intl.ListFormat('fr')
return `${prePlural} ${lf.format(data)}`
}
const last = data.pop()
return prePlural + ' ' + [
data.join(', '),
last
].join(' et ')
}
2 changes: 1 addition & 1 deletion source/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function init () {

const text = document.createElement('p')
text.className = 'qui-possede-les-medias-text'
text.innerHTML = `Ce média appartient à ${renderData(boxData)}.`
text.innerHTML = `Ce média ${renderData(boxData)}.`
box.appendChild(text)

document.querySelector('body').append(box)
Expand Down
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ test('renderData', t => {
link: null
},
]
t.is(utils.renderData(data), '<a href="link" rel="noopener noreferrer" target="_blank">name</a>, no link')
t.is(utils.renderData(data), ' appartient à <a href="link" rel="noopener noreferrer" target="_blank">name</a> et no link')
})

0 comments on commit c6c0dc9

Please sign in to comment.