@uxf/core 11.90.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 +42 -0
- package/package.json +1 -1
- package/utils/in-array.d.ts +1 -0
- package/utils/in-array.js +6 -0
- package/utils/in-array.test.d.ts +1 -0
- package/utils/in-array.test.js +19 -0
- 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
|
@@ -343,6 +343,26 @@ formatBytes(17.5 * 1024);
|
|
|
343
343
|
//=> "17.5 kB"
|
|
344
344
|
```
|
|
345
345
|
|
|
346
|
+
## inArray
|
|
347
|
+
|
|
348
|
+
Type-safe helper for checking if a value exists in an array. Particularly useful with `as const` arrays where the value type is wider than the array element type.
|
|
349
|
+
|
|
350
|
+
```tsx
|
|
351
|
+
import { inArray } from "@uxf/core/utils/in-array";
|
|
352
|
+
|
|
353
|
+
// Basic usage
|
|
354
|
+
inArray("a", ["a", "b", "c"]); /* returns true */
|
|
355
|
+
inArray("d", ["a", "b", "c"]); /* returns false */
|
|
356
|
+
|
|
357
|
+
// Useful with const arrays and wider value types
|
|
358
|
+
const statuses = ["pending", "active", "completed"] as const;
|
|
359
|
+
const status: string = getStatusFromApi();
|
|
360
|
+
|
|
361
|
+
if (inArray(status, statuses)) {
|
|
362
|
+
// status is now narrowed to the array values
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
|
|
346
366
|
## isEmpty
|
|
347
367
|
|
|
348
368
|
```tsx
|
|
@@ -509,6 +529,28 @@ numberOrNull(null); /* returns null */
|
|
|
509
529
|
numberOrNull(undefined); /* returns null */
|
|
510
530
|
```
|
|
511
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
|
+
|
|
512
554
|
## slugify
|
|
513
555
|
|
|
514
556
|
```tsx
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function inArray<Value, Array extends Value>(value: Value, array: readonly Array[]): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const in_array_1 = require("./in-array");
|
|
4
|
+
test("in-array", () => {
|
|
5
|
+
expect((0, in_array_1.inArray)("a", ["a", "b", "c"])).toBeTruthy();
|
|
6
|
+
expect((0, in_array_1.inArray)("b", ["a", "b", "c"])).toBeTruthy();
|
|
7
|
+
expect((0, in_array_1.inArray)("c", ["a", "b", "c"])).toBeTruthy();
|
|
8
|
+
expect((0, in_array_1.inArray)("d", ["a", "b", "c"])).toBeFalsy();
|
|
9
|
+
expect((0, in_array_1.inArray)(1, [1, 2, 3])).toBeTruthy();
|
|
10
|
+
expect((0, in_array_1.inArray)(4, [1, 2, 3])).toBeFalsy();
|
|
11
|
+
expect((0, in_array_1.inArray)("value", [])).toBeFalsy();
|
|
12
|
+
});
|
|
13
|
+
test("in-array with const array", () => {
|
|
14
|
+
const statuses = ["pending", "active", "completed"];
|
|
15
|
+
const status = "active";
|
|
16
|
+
expect((0, in_array_1.inArray)(status, statuses)).toBeTruthy();
|
|
17
|
+
const unknownStatus = "unknown";
|
|
18
|
+
expect((0, in_array_1.inArray)(unknownStatus, statuses)).toBeFalsy();
|
|
19
|
+
});
|
|
@@ -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
|
+
});
|