Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional withNumber parameter #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import numd from 'numd';

numd(2, 'dollar', 'dollars'); // 2 dollars
numd(14, 'рубль', 'рубля', 'рублей'); // 14 рублей
numd(14, 'метр', 'метра', 'метров', {withNumber: false}); // метров
```

## API

### numd([num, ]word[, singular], plural)
### numd([num, ]word[, singular], plural[, options])

Return a string if `num` is defined:

Expand All @@ -37,6 +38,10 @@ otherwise return a function:
const rur = numd('рубль', 'рубля', 'рублей');
rur(4); // 4 рубля
rur(51); // 51 рубль

const meters = numd('метр', 'метра', 'метров', {withNumber: false});
rur(4); // метра
rur(55); // метров
```

#### num
Expand All @@ -63,6 +68,25 @@ Type: `string`

Word in the plural/genitive plural.

#### options

Type: `object`

Options object. Care about passing the third parameter `plural` when passing the options:

```js
import numd from 'numd';

numd(2, 'dollar', 'dollars', null, {withNumber: false}); // dollars
numd(14, 'рубль', 'рубля', 'рублей', {withNumber: false}); // рублей
```

#### options.withNumber

Type: `boolean`

Flag indicating whether the number should be included in the result string

## License

MIT
Expand Down
17 changes: 14 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
declare function numd(num: number, word: string, singular: string, plural?: string): string
declare function numd(word: string, singular: string, plural?: string): (num: number) => string
export default numd
declare function numd(
num: number,
word: string,
singular: string,
plural?: string,
options?: { withNumber?: boolean }
): string;
declare function numd(
word: string,
singular: string,
plural?: string | null,
options?: { withNumber?: boolean }
): (num: number) => string;
export default numd;
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ const isGenitivePlural = num =>

const isGenitiveSingular = num => Math.floor(num) !== num || num % 10 >= 2;

const pluralize = (word, singular, plural) => num => {
const pluralize = (word, singular, plural, options = {}) => num => {
const { withNumber = true } = options;
const displayNum = withNumber ? `${num} ` : '';
const abs = Math.abs(num);
if (abs === 1) return `${num} ${word}`;
if (!plural) return `${num} ${singular}`;
if (isGenitivePlural(abs)) return `${num} ${plural}`;
if (isGenitiveSingular(abs)) return `${num} ${singular}`;
return `${num} ${word}`;
if (abs === 1) return `${displayNum}${word}`;
if (!plural) return `${displayNum}${singular}`;
if (isGenitivePlural(abs)) return `${displayNum}${plural}`;
if (isGenitiveSingular(abs)) return `${displayNum}${singular}`;
return `${displayNum}${word}`;
};

export default function numd(num, ...words) {
export default function numd(num, ...params) {
return typeof num === 'number' ?
pluralize(...words)(num) :
pluralize(num, ...words);
pluralize(...params)(num) :
pluralize(num, ...params);
}
Loading