phuocng / 1loc

What's your favorite JavaScript single LOC (line of code)?

Home Page:https://phuoc.ng/collection/1-loc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE] Pluralise based on length of array.

maclong9 opened this issue · comments

A simple line of code to pluralise a word based on the length of an array.

const pluralise = (items, singular, plural) => items.length > 1 || items.length === 0 ? plural : singular;

Use Case

For if you have copy text that requires the interchangeability of pluralising based on the amount of items in an array.

let items = [0];
let string = `The ${pluralise(items, "test", "tests")}.`
console.log(string); // 'The item.'
items = [0, 1, 2, 3];
console.log(string); // 'The items'

Hi @MAC-Long, can you provide that snippet with use cases examples ?

@elkarouani I've added use case snippet

That's good @MAC-Long, can you also add the expected results of the console.log lines ?
There is a missing closing curly bracket in the second line

@elkarouani that should be closed now and I've added some comments which display the results

@MAC-Long, you can create a pull request to request adding this feature. Attention to the use case, I think you would repeat the second line after changing the items value