polish-pluralize 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- declare function pluralize(count: number, forms: [string, string, string]): string;
1
+ declare function pluralize(count: number, forms: [string, string, string], includeNumber?: boolean): string;
2
2
 
3
3
  export { pluralize };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare function pluralize(count: number, forms: [string, string, string]): string;
1
+ declare function pluralize(count: number, forms: [string, string, string], includeNumber?: boolean): string;
2
2
 
3
3
  export { pluralize };
package/dist/index.js CHANGED
@@ -23,22 +23,29 @@ __export(index_exports, {
23
23
  pluralize: () => pluralize
24
24
  });
25
25
  module.exports = __toCommonJS(index_exports);
26
- function pluralize(count, forms) {
27
- const remainder = count % 10;
28
- const remainder100 = count % 100;
29
- if (remainder100 > 10 && remainder100 < 20) {
30
- return forms[2];
26
+ function pluralize(count, forms, includeNumber = false) {
27
+ if (!Array.isArray(forms) || forms.length !== 3) {
28
+ throw new Error(
29
+ "Biblioteka polish-pluralize wymaga parametru 'forms' jako tablicy z dok\u0142adnie 3 formami s\u0142owa (np. ['plik', 'pliki', 'plik\xF3w'])."
30
+ );
31
31
  }
32
- switch (remainder) {
33
- case 1:
34
- return forms[0];
35
- case 2:
36
- case 3:
37
- case 4:
38
- return forms[1];
39
- default:
40
- return forms[2];
32
+ if (typeof count !== "number" || Number.isNaN(count)) {
33
+ throw new Error("Parametr 'count' musi by\u0107 poprawn\u0105 liczb\u0105.");
41
34
  }
35
+ const absoluteCount = Math.floor(Math.abs(count));
36
+ let form;
37
+ if (absoluteCount === 1 && count === 1) {
38
+ form = forms[0];
39
+ } else {
40
+ const remainder100 = absoluteCount % 100;
41
+ const remainder10 = absoluteCount % 10;
42
+ if (remainder10 >= 2 && remainder10 <= 4 && (remainder100 < 10 || remainder100 >= 20)) {
43
+ form = forms[1];
44
+ } else {
45
+ form = forms[2];
46
+ }
47
+ }
48
+ return includeNumber ? `${count} ${form}` : form;
42
49
  }
43
50
  // Annotate the CommonJS export names for ESM import in node:
44
51
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,20 +1,27 @@
1
1
  // src/index.ts
2
- function pluralize(count, forms) {
3
- const remainder = count % 10;
4
- const remainder100 = count % 100;
5
- if (remainder100 > 10 && remainder100 < 20) {
6
- return forms[2];
2
+ function pluralize(count, forms, includeNumber = false) {
3
+ if (!Array.isArray(forms) || forms.length !== 3) {
4
+ throw new Error(
5
+ "Biblioteka polish-pluralize wymaga parametru 'forms' jako tablicy z dok\u0142adnie 3 formami s\u0142owa (np. ['plik', 'pliki', 'plik\xF3w'])."
6
+ );
7
7
  }
8
- switch (remainder) {
9
- case 1:
10
- return forms[0];
11
- case 2:
12
- case 3:
13
- case 4:
14
- return forms[1];
15
- default:
16
- return forms[2];
8
+ if (typeof count !== "number" || Number.isNaN(count)) {
9
+ throw new Error("Parametr 'count' musi by\u0107 poprawn\u0105 liczb\u0105.");
17
10
  }
11
+ const absoluteCount = Math.floor(Math.abs(count));
12
+ let form;
13
+ if (absoluteCount === 1 && count === 1) {
14
+ form = forms[0];
15
+ } else {
16
+ const remainder100 = absoluteCount % 100;
17
+ const remainder10 = absoluteCount % 10;
18
+ if (remainder10 >= 2 && remainder10 <= 4 && (remainder100 < 10 || remainder100 >= 20)) {
19
+ form = forms[1];
20
+ } else {
21
+ form = forms[2];
22
+ }
23
+ }
24
+ return includeNumber ? `${count} ${form}` : form;
18
25
  }
19
26
  export {
20
27
  pluralize
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polish-pluralize",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Mała biblioteka do odmiany słów w języku polskim dla liczebników.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",