cafe-utility 9.8.0 → 9.10.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/index.d.ts +4 -0
- package/index.js +26 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ declare function asBoolean(bool: any): boolean;
|
|
|
59
59
|
declare function asDate(date: any): Date;
|
|
60
60
|
declare function asNullableString(string: any): string | null;
|
|
61
61
|
declare function asId(value: any): number;
|
|
62
|
+
declare function asTime(value: any): string;
|
|
62
63
|
declare function asArray(value: any): unknown[];
|
|
63
64
|
declare function asObject(value: any): Record<string, unknown>;
|
|
64
65
|
declare function represent(value: any): string;
|
|
@@ -254,6 +255,7 @@ declare function filterObjectValues<T>(object: CafeObject<T>, predicate: (value:
|
|
|
254
255
|
declare function mapObject<T, K>(object: CafeObject<T>, mapper: (value: T) => K): CafeObject<K>;
|
|
255
256
|
declare function rethrow<T>(asyncFn: () => Promise<T>, throwable: Error): Promise<T>;
|
|
256
257
|
declare function setSomeOnObject(object: CafeObject, key: string, value: unknown): void;
|
|
258
|
+
declare function setSomeDeep(target: CafeObject, targetPath: string, source: CafeObject, sourcePath: string): void;
|
|
257
259
|
declare function flip(object: Record<string, Indexable>): CafeObject;
|
|
258
260
|
declare function getAllPermutations(object: CafeObject<unknown[]>): CafeObject[];
|
|
259
261
|
declare function countTruthyValues(object: CafeObject): number;
|
|
@@ -441,6 +443,7 @@ export declare const Objects: {
|
|
|
441
443
|
filterValues: typeof filterObjectValues;
|
|
442
444
|
rethrow: typeof rethrow;
|
|
443
445
|
setSomeOnObject: typeof setSomeOnObject;
|
|
446
|
+
setSomeDeep: typeof setSomeDeep;
|
|
444
447
|
flip: typeof flip;
|
|
445
448
|
getAllPermutations: typeof getAllPermutations;
|
|
446
449
|
countTruthyValues: typeof countTruthyValues;
|
|
@@ -481,6 +484,7 @@ export declare const Types: {
|
|
|
481
484
|
asDate: typeof asDate;
|
|
482
485
|
asNullableString: typeof asNullableString;
|
|
483
486
|
asId: typeof asId;
|
|
487
|
+
asTime: typeof asTime;
|
|
484
488
|
asArray: typeof asArray;
|
|
485
489
|
asObject: typeof asObject;
|
|
486
490
|
};
|
package/index.js
CHANGED
|
@@ -459,6 +459,22 @@ function asId(value) {
|
|
|
459
459
|
return typeof value === 'string' ? parseInt(value, 10) : value
|
|
460
460
|
}
|
|
461
461
|
|
|
462
|
+
function asTime(value) {
|
|
463
|
+
if (!isString(value)) {
|
|
464
|
+
throw new TypeError('Expected time, got: ' + value)
|
|
465
|
+
}
|
|
466
|
+
const parts = value.split(':')
|
|
467
|
+
if (parts.length !== 2) {
|
|
468
|
+
throw new TypeError('Expected time, got: ' + value)
|
|
469
|
+
}
|
|
470
|
+
const hours = parseInt(parts[0], 10)
|
|
471
|
+
const minutes = parseInt(parts[1], 10)
|
|
472
|
+
if (!isNumber(hours) || !isNumber(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
|
|
473
|
+
throw new TypeError('Expected time, got: ' + value)
|
|
474
|
+
}
|
|
475
|
+
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`
|
|
476
|
+
}
|
|
477
|
+
|
|
462
478
|
function asArray(value) {
|
|
463
479
|
if (!Array.isArray(value)) {
|
|
464
480
|
throw new TypeError('Expected array, got: ' + value)
|
|
@@ -1692,6 +1708,14 @@ function setSomeOnObject(object, key, value) {
|
|
|
1692
1708
|
}
|
|
1693
1709
|
}
|
|
1694
1710
|
|
|
1711
|
+
function setSomeDeep(target, targetPath, source, sourcePath) {
|
|
1712
|
+
const value = getDeep(source, sourcePath)
|
|
1713
|
+
if (typeof value === 'undefined' || value === null) {
|
|
1714
|
+
return
|
|
1715
|
+
}
|
|
1716
|
+
setDeep(target, targetPath, value)
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1695
1719
|
function flip(object) {
|
|
1696
1720
|
const result = {}
|
|
1697
1721
|
for (const [key, value] of Object.entries(object)) {
|
|
@@ -2410,6 +2434,7 @@ exports.Objects = {
|
|
|
2410
2434
|
filterValues: filterObjectValues,
|
|
2411
2435
|
rethrow,
|
|
2412
2436
|
setSomeOnObject,
|
|
2437
|
+
setSomeDeep,
|
|
2413
2438
|
flip,
|
|
2414
2439
|
getAllPermutations,
|
|
2415
2440
|
countTruthyValues,
|
|
@@ -2452,6 +2477,7 @@ exports.Types = {
|
|
|
2452
2477
|
asDate,
|
|
2453
2478
|
asNullableString,
|
|
2454
2479
|
asId,
|
|
2480
|
+
asTime,
|
|
2455
2481
|
asArray,
|
|
2456
2482
|
asObject
|
|
2457
2483
|
}
|