cafe-utility 4.5.0 → 4.7.1
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/index.d.ts +12 -6
- package/index.js +29 -10
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ declare function ensureDeep(object: CafeObject, path: string, value: unknown): u
|
|
|
31
31
|
declare function deleteDeep(object: CafeObject, path: string): void;
|
|
32
32
|
declare function replaceDeep(object: CafeObject, path: string, value: unknown): unknown;
|
|
33
33
|
declare function getFirstDeep(object: CafeObject, paths: string[], fallbackToAnyKey: string): unknown;
|
|
34
|
-
declare function forever(callable: () => Promise<void> | (() => void), millis: number): Promise<never>;
|
|
34
|
+
declare function forever(callable: (() => Promise<void>) | (() => void), millis: number): Promise<never>;
|
|
35
35
|
declare function readUtf8FileAsync(path: string): Promise<string>;
|
|
36
36
|
declare function readJsonAsync(path: string): Promise<CafeObject>;
|
|
37
37
|
declare function writeJsonAsync(path: string, object: CafeObject, prettify?: boolean): Promise<void>;
|
|
@@ -58,6 +58,7 @@ declare function isString(value: any): value is string;
|
|
|
58
58
|
declare function isNumber(value: any): value is number;
|
|
59
59
|
declare function isDate(value: any): value is Date;
|
|
60
60
|
declare function isBlank(value: any): boolean;
|
|
61
|
+
declare function isId(value: any): value is number;
|
|
61
62
|
declare function randomLetterString(length: number): string;
|
|
62
63
|
declare function randomAlphanumericString(length: number): string;
|
|
63
64
|
declare function randomRichAsciiString(length: number): string;
|
|
@@ -67,11 +68,12 @@ declare function asString(string: any): string;
|
|
|
67
68
|
declare function asNumber(number: any): number;
|
|
68
69
|
declare function asDate(date: any): Date;
|
|
69
70
|
declare function asNullableString(string: any): string | null;
|
|
71
|
+
declare function asId(value: any): number;
|
|
70
72
|
declare function createLogger(module: string): {
|
|
71
|
-
trace: (...pieces:
|
|
72
|
-
info: (...pieces:
|
|
73
|
-
warn: (...pieces:
|
|
74
|
-
error: (...pieces:
|
|
73
|
+
trace: (...pieces: any[]) => void;
|
|
74
|
+
info: (...pieces: any[]) => void;
|
|
75
|
+
warn: (...pieces: any[]) => void;
|
|
76
|
+
error: (...pieces: any[]) => void;
|
|
75
77
|
errorObject: (error: Error, stackTrace?: boolean) => void;
|
|
76
78
|
};
|
|
77
79
|
declare function enableFileLogging(path: string): void;
|
|
@@ -231,7 +233,8 @@ declare function countTruthyValues(object: CafeObject): number;
|
|
|
231
233
|
declare function flatten(object: CafeObject, arrays?: boolean, prefix?: string): CafeObject | Array<unknown>;
|
|
232
234
|
declare function unflatten(object: CafeObject): CafeObject;
|
|
233
235
|
declare function match(value: string, options: CafeObject<string>, fallback: string): string;
|
|
234
|
-
declare function indexArray<T>(array: T[], keyFn: (item: T) => Indexable
|
|
236
|
+
declare function indexArray<T>(array: T[], keyFn: (item: T) => Indexable): CafeObject<T>;
|
|
237
|
+
declare function indexArrayToCollection<T>(array: T[], keyFn: (item: T) => Indexable): CafeObject<T[]>;
|
|
235
238
|
declare function splitBySize<T>(array: T[], size: number): T[][];
|
|
236
239
|
declare function splitByCount<T>(array: T[], count: number): T[][];
|
|
237
240
|
declare function tokenizeByLength(string: string, length: number): string[];
|
|
@@ -280,6 +283,7 @@ export declare const Arrays: {
|
|
|
280
283
|
splitBySize: typeof splitBySize;
|
|
281
284
|
splitByCount: typeof splitByCount;
|
|
282
285
|
index: typeof indexArray;
|
|
286
|
+
indexCollection: typeof indexArrayToCollection;
|
|
283
287
|
onlyOrThrow: typeof onlyOrThrow;
|
|
284
288
|
onlyOrNull: typeof onlyOrNull;
|
|
285
289
|
firstOrNull: typeof firstOrNull;
|
|
@@ -427,10 +431,12 @@ export declare const Types: {
|
|
|
427
431
|
isNumber: typeof isNumber;
|
|
428
432
|
isDate: typeof isDate;
|
|
429
433
|
isBlank: typeof isBlank;
|
|
434
|
+
isId: typeof isId;
|
|
430
435
|
asString: typeof asString;
|
|
431
436
|
asNumber: typeof asNumber;
|
|
432
437
|
asDate: typeof asDate;
|
|
433
438
|
asNullableString: typeof asNullableString;
|
|
439
|
+
asId: typeof asId;
|
|
434
440
|
};
|
|
435
441
|
export declare const Strings: {
|
|
436
442
|
tokenizeByCount: typeof tokenizeByCount;
|
package/index.js
CHANGED
|
@@ -381,6 +381,10 @@ function isBlank(value) {
|
|
|
381
381
|
return !isString(value) || value.trim().length === 0
|
|
382
382
|
}
|
|
383
383
|
|
|
384
|
+
function isId(value) {
|
|
385
|
+
return Number.isInteger(value) && value >= 1
|
|
386
|
+
}
|
|
387
|
+
|
|
384
388
|
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
|
|
385
389
|
const alphanumericAlphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
|
|
386
390
|
const richAsciiAlphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+-=[]{}|;:<>?,./'
|
|
@@ -454,6 +458,13 @@ function asNullableString(string) {
|
|
|
454
458
|
return string
|
|
455
459
|
}
|
|
456
460
|
|
|
461
|
+
function asId(value) {
|
|
462
|
+
if (!isId(value)) {
|
|
463
|
+
throw new TypeError('Expected id, got: ' + value)
|
|
464
|
+
}
|
|
465
|
+
return value
|
|
466
|
+
}
|
|
467
|
+
|
|
457
468
|
function represent(value) {
|
|
458
469
|
if (isObject(value)) {
|
|
459
470
|
return JSON.stringify(value, null, 4)
|
|
@@ -1477,18 +1488,23 @@ function match(value, options, fallback) {
|
|
|
1477
1488
|
return options[value] ? options[value] : fallback
|
|
1478
1489
|
}
|
|
1479
1490
|
|
|
1480
|
-
function indexArray(array, keyFn
|
|
1491
|
+
function indexArray(array, keyFn) {
|
|
1481
1492
|
const target = {}
|
|
1482
1493
|
for (const element of array) {
|
|
1483
1494
|
const key = keyFn(element)
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1495
|
+
target[key] = element
|
|
1496
|
+
}
|
|
1497
|
+
return target
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
function indexArrayToCollection(array, keyFn) {
|
|
1501
|
+
const target = {}
|
|
1502
|
+
for (const element of array) {
|
|
1503
|
+
const key = keyFn(element)
|
|
1504
|
+
if (!target[key]) {
|
|
1505
|
+
target[key] = []
|
|
1491
1506
|
}
|
|
1507
|
+
target[key].push(element)
|
|
1492
1508
|
}
|
|
1493
1509
|
return target
|
|
1494
1510
|
}
|
|
@@ -1525,7 +1541,7 @@ function tokenizeByCount(string, count) {
|
|
|
1525
1541
|
}
|
|
1526
1542
|
|
|
1527
1543
|
function makeUnique(array, fn) {
|
|
1528
|
-
return Object.values(indexArray(array, fn
|
|
1544
|
+
return Object.values(indexArray(array, fn))
|
|
1529
1545
|
}
|
|
1530
1546
|
|
|
1531
1547
|
function countUnique(array, mapper, plain, sort, reverse) {
|
|
@@ -1678,6 +1694,7 @@ exports.Arrays = {
|
|
|
1678
1694
|
splitBySize,
|
|
1679
1695
|
splitByCount,
|
|
1680
1696
|
index: indexArray,
|
|
1697
|
+
indexCollection: indexArrayToCollection,
|
|
1681
1698
|
onlyOrThrow,
|
|
1682
1699
|
onlyOrNull,
|
|
1683
1700
|
firstOrNull,
|
|
@@ -1831,10 +1848,12 @@ exports.Types = {
|
|
|
1831
1848
|
isNumber,
|
|
1832
1849
|
isDate,
|
|
1833
1850
|
isBlank,
|
|
1851
|
+
isId,
|
|
1834
1852
|
asString,
|
|
1835
1853
|
asNumber,
|
|
1836
1854
|
asDate,
|
|
1837
|
-
asNullableString
|
|
1855
|
+
asNullableString,
|
|
1856
|
+
asId
|
|
1838
1857
|
}
|
|
1839
1858
|
|
|
1840
1859
|
exports.Strings = {
|