Skip to content

Allow partial application #9

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

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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ commentsLabel(1); // komentarz
commentsLabel(0); // komentarzy
commentsLabel(3); // komentarze
```

### Partial application
And if you dislike `bind` for some reason - you can do partial application like this:

```javascript
import { polishPlurals } from 'polish-plurals';

const commentsLabel = polishPlurals(null, 'komentarz', 'komentarze', 'komentarzy');
commentsLabel(1); // komentarz
commentsLabel(0); // komentarzy
commentsLabel(3); // komentarze
```
12 changes: 12 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
export function polishPlurals(singularNominativ, pluralNominativ, pluralGenitive, value) {
const partial = value => _polishPlurals(singularNominativ, pluralNominativ, pluralGenitive, value);

if (typeof value !== "undefined") {
return partial(value);
}
else {
return partial;
}
}

function _polishPlurals(singularNominativ, pluralNominativ, pluralGenitive, value) {
value = Math.abs(value);

if (value === 1) {
return singularNominativ;
} else if (value % 10 >= 2 && value % 10 <= 4 && (value % 100 < 10 || value % 100 >= 20)) {
Expand Down
6 changes: 6 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ describe('polish plurals', () => {
expect(commentsLabel(-3)).to.eql('komentarze');
expect(commentsLabel(-5)).to.eql('komentarzy');
});

it('should allow partial aplication', () => {
const partialCommentsLabel = polishPlurals('komentarz', 'komentarze', 'komentarzy');

expect(partialCommentsLabel(1)).to.eql(polishPlurals('komentarz', 'komentarze', 'komentarzy', 1));
});
});