@naturalcycles/js-lib 15.55.1 → 15.56.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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyObject, KeyValueTuple, MutateOptions, ObjectMapper, ObjectPredicate, Reviver, StringMap, ValueOf } from '../types.js';
|
|
1
|
+
import type { AnyObject, KeyValueTuple, MutateOptions, ObjectMapper, ObjectPredicate, RequiredProp, Reviver, StringMap, ValueOf } from '../types.js';
|
|
2
2
|
import { SKIP } from '../types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Returns clone of `obj` with only `props` preserved.
|
|
@@ -228,4 +228,13 @@ export declare function _deepFreeze(o: any): void;
|
|
|
228
228
|
* To make mutation extra clear - function returns void (unlike Object.assign).
|
|
229
229
|
*/
|
|
230
230
|
export declare function _objectAssignExact<T extends AnyObject>(target: T, source: T): void;
|
|
231
|
+
/**
|
|
232
|
+
* type MyObj = { a?: string, b?: string }
|
|
233
|
+
*
|
|
234
|
+
* const collection: MyObj[] = [...]
|
|
235
|
+
*
|
|
236
|
+
* const collectionA = collection.filter(_hasProp('a'))
|
|
237
|
+
* --> collectionA is now RequiredProp<MyObj, 'a'>[], i.e. { a: string, b?: string }[]
|
|
238
|
+
*/
|
|
239
|
+
export declare function _hasProp<T, Prop extends keyof T>(prop: Prop): (object: T) => object is RequiredProp<T, Prop>;
|
|
231
240
|
export {};
|
|
@@ -446,3 +446,16 @@ export function _objectAssignExact(target, source) {
|
|
|
446
446
|
}
|
|
447
447
|
}
|
|
448
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* type MyObj = { a?: string, b?: string }
|
|
451
|
+
*
|
|
452
|
+
* const collection: MyObj[] = [...]
|
|
453
|
+
*
|
|
454
|
+
* const collectionA = collection.filter(_hasProp('a'))
|
|
455
|
+
* --> collectionA is now RequiredProp<MyObj, 'a'>[], i.e. { a: string, b?: string }[]
|
|
456
|
+
*/
|
|
457
|
+
export function _hasProp(prop) {
|
|
458
|
+
return function (object) {
|
|
459
|
+
return typeof object[prop] !== 'undefined';
|
|
460
|
+
};
|
|
461
|
+
}
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
MutateOptions,
|
|
6
6
|
ObjectMapper,
|
|
7
7
|
ObjectPredicate,
|
|
8
|
+
RequiredProp,
|
|
8
9
|
Reviver,
|
|
9
10
|
StringMap,
|
|
10
11
|
ValueOf,
|
|
@@ -527,3 +528,17 @@ export function _objectAssignExact<T extends AnyObject>(target: T, source: T): v
|
|
|
527
528
|
}
|
|
528
529
|
}
|
|
529
530
|
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* type MyObj = { a?: string, b?: string }
|
|
534
|
+
*
|
|
535
|
+
* const collection: MyObj[] = [...]
|
|
536
|
+
*
|
|
537
|
+
* const collectionA = collection.filter(_hasProp('a'))
|
|
538
|
+
* --> collectionA is now RequiredProp<MyObj, 'a'>[], i.e. { a: string, b?: string }[]
|
|
539
|
+
*/
|
|
540
|
+
export function _hasProp<T, Prop extends keyof T>(prop: Prop) {
|
|
541
|
+
return function (object: T): object is RequiredProp<T, Prop> {
|
|
542
|
+
return typeof object[prop] !== 'undefined'
|
|
543
|
+
}
|
|
544
|
+
}
|