From 27409984a726ac6148de23d4db275c947df44e80 Mon Sep 17 00:00:00 2001 From: "Olgierd \"Allgreed\" Kasprowicz" Date: Tue, 23 Oct 2018 00:20:37 +0200 Subject: [PATCH] Allow partial application --- README.md | 12 ++++++++++++ index.mjs | 12 ++++++++++++ test/index.test.js | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/README.md b/README.md index 640c3c2..da03250 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/index.mjs b/index.mjs index 628b1da..cef2113 100644 --- a/index.mjs +++ b/index.mjs @@ -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)) { diff --git a/test/index.test.js b/test/index.test.js index 6cfea84..69ce84d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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)); + }); });