@uxf/core 11.91.0 → 11.93.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/README.md +22 -0
- package/package.json +1 -1
- package/utils/plural.d.ts +10 -0
- package/utils/plural.js +15 -0
- package/utils/plural.test.d.ts +1 -0
- package/utils/plural.test.js +37 -0
package/README.md
CHANGED
|
@@ -529,6 +529,28 @@ numberOrNull(null); /* returns null */
|
|
|
529
529
|
numberOrNull(undefined); /* returns null */
|
|
530
530
|
```
|
|
531
531
|
|
|
532
|
+
## plural
|
|
533
|
+
|
|
534
|
+
Simple pluralization function for a specific language and set of terms.
|
|
535
|
+
|
|
536
|
+
```tsx
|
|
537
|
+
import { createPlural } from "@uxf/core/utils/plural";
|
|
538
|
+
|
|
539
|
+
const terms = {
|
|
540
|
+
items: {
|
|
541
|
+
one: "položka",
|
|
542
|
+
few: "položky",
|
|
543
|
+
other: "položek",
|
|
544
|
+
},
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
const t = createPlural(terms, "cs");
|
|
548
|
+
|
|
549
|
+
t("items", 1); // returns "položka"
|
|
550
|
+
t("items", 3); // returns "položky"
|
|
551
|
+
t("items", 5); // returns "položek"
|
|
552
|
+
```
|
|
553
|
+
|
|
532
554
|
## slugify
|
|
533
555
|
|
|
534
556
|
```tsx
|
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface PluralOptions {
|
|
2
|
+
zero?: string;
|
|
3
|
+
one?: string;
|
|
4
|
+
two?: string;
|
|
5
|
+
few?: string;
|
|
6
|
+
many?: string;
|
|
7
|
+
other: string;
|
|
8
|
+
}
|
|
9
|
+
export type PluralTerms<T extends string> = Record<T, PluralOptions>;
|
|
10
|
+
export declare function createPlural<T extends string>(terms: PluralTerms<T>, locale: string): (key: T, count: number) => string;
|
package/utils/plural.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPlural = createPlural;
|
|
4
|
+
function plural(count, options, locale) {
|
|
5
|
+
var _a;
|
|
6
|
+
const rules = new Intl.PluralRules(locale);
|
|
7
|
+
const category = rules.select(count);
|
|
8
|
+
return (_a = options[category]) !== null && _a !== void 0 ? _a : options.other;
|
|
9
|
+
}
|
|
10
|
+
function createPlural(terms, locale) {
|
|
11
|
+
return (key, count) => {
|
|
12
|
+
const options = terms[key];
|
|
13
|
+
return plural(count, options, locale);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const plural_1 = require("./plural");
|
|
4
|
+
describe("pluralization", () => {
|
|
5
|
+
describe("createPlural", () => {
|
|
6
|
+
test("creates a typed plural function", () => {
|
|
7
|
+
const terms = {
|
|
8
|
+
items: {
|
|
9
|
+
one: "item",
|
|
10
|
+
other: "items",
|
|
11
|
+
},
|
|
12
|
+
apples: {
|
|
13
|
+
one: "apple",
|
|
14
|
+
other: "apples",
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
const t = (0, plural_1.createPlural)(terms, "en");
|
|
18
|
+
expect(t("items", 1)).toBe("item");
|
|
19
|
+
expect(t("items", 5)).toBe("items");
|
|
20
|
+
expect(t("apples", 1)).toBe("apple");
|
|
21
|
+
expect(t("apples", 10)).toBe("apples");
|
|
22
|
+
});
|
|
23
|
+
test("works with czech terms", () => {
|
|
24
|
+
const terms = {
|
|
25
|
+
items: {
|
|
26
|
+
one: "položka",
|
|
27
|
+
few: "položky",
|
|
28
|
+
other: "položek",
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const t = (0, plural_1.createPlural)(terms, "cs");
|
|
32
|
+
expect(t("items", 1)).toBe("položka");
|
|
33
|
+
expect(t("items", 3)).toBe("položky");
|
|
34
|
+
expect(t("items", 5)).toBe("položek");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|