@augment-vir/common 7.1.0 → 8.1.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.
Files changed (59) hide show
  1. package/dist/cjs/augments/nested-keys.d.ts +1 -1
  2. package/dist/cjs/augments/object/enum.d.ts +5 -0
  3. package/dist/cjs/augments/object/enum.js +37 -0
  4. package/dist/cjs/augments/object/map-object.d.ts +11 -0
  5. package/dist/cjs/augments/object/map-object.js +39 -0
  6. package/dist/cjs/augments/object/matches-object-shape.d.ts +37 -0
  7. package/dist/cjs/augments/object/matches-object-shape.js +118 -0
  8. package/dist/cjs/augments/object/nested-keys.d.ts +12 -0
  9. package/dist/cjs/augments/object/nested-keys.js +23 -0
  10. package/dist/cjs/augments/object/object-entries.d.ts +6 -0
  11. package/dist/cjs/augments/object/object-entries.js +32 -0
  12. package/dist/cjs/augments/object/object.d.ts +10 -0
  13. package/dist/cjs/augments/object/object.js +42 -0
  14. package/dist/cjs/augments/object/pick-deep.d.ts +8 -0
  15. package/dist/cjs/augments/object/pick-deep.js +2 -0
  16. package/dist/cjs/augments/object/typed-has-property.d.ts +8 -0
  17. package/dist/cjs/augments/object/typed-has-property.js +30 -0
  18. package/dist/cjs/augments/pick-deep.d.ts +1 -1
  19. package/dist/cjs/augments/promise.js +7 -5
  20. package/dist/cjs/augments/runtime-type-of.d.ts +20 -0
  21. package/dist/cjs/augments/runtime-type-of.js +23 -0
  22. package/dist/cjs/index.d.ts +9 -4
  23. package/dist/cjs/index.js +9 -4
  24. package/dist/esm/augments/nested-keys.d.ts +1 -1
  25. package/dist/esm/augments/object/enum.d.ts +5 -0
  26. package/dist/esm/augments/object/enum.js +30 -0
  27. package/dist/esm/augments/object/map-object.d.ts +11 -0
  28. package/dist/esm/augments/object/map-object.js +35 -0
  29. package/dist/esm/augments/object/matches-object-shape.d.ts +37 -0
  30. package/dist/esm/augments/object/matches-object-shape.js +113 -0
  31. package/dist/esm/augments/object/nested-keys.d.ts +12 -0
  32. package/dist/esm/augments/object/nested-keys.js +19 -0
  33. package/dist/esm/augments/object/object-entries.d.ts +6 -0
  34. package/dist/esm/augments/object/object-entries.js +24 -0
  35. package/dist/esm/augments/object/object.d.ts +10 -0
  36. package/dist/esm/augments/object/object.js +35 -0
  37. package/dist/esm/augments/object/pick-deep.d.ts +8 -0
  38. package/dist/esm/augments/object/pick-deep.js +1 -0
  39. package/dist/esm/augments/object/typed-has-property.d.ts +8 -0
  40. package/dist/esm/augments/object/typed-has-property.js +25 -0
  41. package/dist/esm/augments/pick-deep.d.ts +1 -1
  42. package/dist/esm/augments/promise.js +6 -4
  43. package/dist/esm/augments/runtime-type-of.d.ts +20 -0
  44. package/dist/esm/augments/runtime-type-of.js +17 -0
  45. package/dist/esm/index.d.ts +9 -4
  46. package/dist/esm/index.js +9 -4
  47. package/dist/types/augments/nested-keys.d.ts +1 -1
  48. package/dist/types/augments/object/enum.d.ts +5 -0
  49. package/dist/types/augments/object/map-object.d.ts +11 -0
  50. package/dist/types/augments/object/matches-object-shape.d.ts +37 -0
  51. package/dist/types/augments/object/nested-keys.d.ts +12 -0
  52. package/dist/types/augments/object/object-entries.d.ts +6 -0
  53. package/dist/types/augments/object/object.d.ts +10 -0
  54. package/dist/types/augments/object/pick-deep.d.ts +8 -0
  55. package/dist/types/augments/object/typed-has-property.d.ts +8 -0
  56. package/dist/types/augments/pick-deep.d.ts +1 -1
  57. package/dist/types/augments/runtime-type-of.d.ts +20 -0
  58. package/dist/types/index.d.ts +9 -4
  59. package/package.json +3 -2
@@ -1,5 +1,5 @@
1
1
  import { UnionToIntersection } from 'type-fest';
2
- import { ObjectValueType } from './object';
2
+ import { ObjectValueType } from '../object';
3
3
  export type NestedSequentialKeys<ObjectGeneric extends object> = ObjectValueType<{
4
4
  [Prop in keyof ObjectGeneric]: NonNullable<ObjectGeneric[Prop]> extends object ? [Prop, ...(NestedSequentialKeys<NonNullable<ObjectGeneric[Prop]>> | [])] : [Prop];
5
5
  }>;
@@ -0,0 +1,5 @@
1
+ export declare function getEnumTypedKeys<T extends object>(input: T): (keyof T)[];
2
+ export declare function getEnumTypedValues<T extends object>(input: T): T[keyof T][];
3
+ export declare function isEnumValue<T extends object>(input: unknown, checkEnum: T): input is T[keyof T];
4
+ export declare function filterToEnumValues<T extends object>(inputs: ReadonlyArray<unknown>, checkEnum: T, caseInsensitive?: boolean): T[keyof T][];
5
+ //# sourceMappingURL=enum.d.ts.map
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterToEnumValues = exports.isEnumValue = exports.getEnumTypedValues = exports.getEnumTypedKeys = void 0;
4
+ const object_entries_1 = require("./object-entries");
5
+ function getEnumTypedKeys(input) {
6
+ // enum keys are always strings
7
+ return (0, object_entries_1.getObjectTypedKeys)(input).filter((key) => isNaN(Number(key)));
8
+ }
9
+ exports.getEnumTypedKeys = getEnumTypedKeys;
10
+ function getEnumTypedValues(input) {
11
+ const keys = getEnumTypedKeys(input);
12
+ return keys.map((key) => input[key]);
13
+ }
14
+ exports.getEnumTypedValues = getEnumTypedValues;
15
+ function isEnumValue(input, checkEnum) {
16
+ return getEnumTypedValues(checkEnum).includes(input);
17
+ }
18
+ exports.isEnumValue = isEnumValue;
19
+ function filterToEnumValues(inputs, checkEnum, caseInsensitive = false) {
20
+ if (caseInsensitive) {
21
+ return inputs.reduce((accum, currentInput) => {
22
+ const matchedEnumValue = getEnumTypedValues(checkEnum).find((actualEnumValue) => {
23
+ return String(actualEnumValue).toUpperCase() === String(currentInput).toUpperCase();
24
+ });
25
+ if (matchedEnumValue) {
26
+ return accum.concat(matchedEnumValue);
27
+ }
28
+ else {
29
+ return accum;
30
+ }
31
+ }, []);
32
+ }
33
+ else {
34
+ return inputs.filter((input) => isEnumValue(input, checkEnum));
35
+ }
36
+ }
37
+ exports.filterToEnumValues = filterToEnumValues;
@@ -0,0 +1,11 @@
1
+ import { UnPromise } from '../type';
2
+ export type InnerMappedValues<EntireInputGeneric extends object, MappedValueGeneric> = {
3
+ [MappedProp in keyof EntireInputGeneric]: MappedValueGeneric;
4
+ };
5
+ export type MappedValues<EntireInputGeneric extends object, MappedValueGeneric> = MappedValueGeneric extends PromiseLike<unknown> ? Promise<InnerMappedValues<EntireInputGeneric, UnPromise<MappedValueGeneric>>> : InnerMappedValues<EntireInputGeneric, UnPromise<MappedValueGeneric>>;
6
+ /**
7
+ * Creates a new object with the same properties as the input object, but with values set to the
8
+ * result of mapCallback for each property.
9
+ */
10
+ export declare function mapObjectValues<EntireInputGeneric extends object, MappedValueGeneric>(inputObject: EntireInputGeneric, mapCallback: (inputKey: keyof EntireInputGeneric, keyValue: EntireInputGeneric[typeof inputKey], fullObject: EntireInputGeneric) => MappedValueGeneric): MappedValues<EntireInputGeneric, MappedValueGeneric>;
11
+ //# sourceMappingURL=map-object.d.ts.map
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapObjectValues = void 0;
4
+ const object_entries_1 = require("./object-entries");
5
+ /**
6
+ * Creates a new object with the same properties as the input object, but with values set to the
7
+ * result of mapCallback for each property.
8
+ */
9
+ function mapObjectValues(inputObject, mapCallback) {
10
+ let gotAPromise = false;
11
+ const mappedObject = (0, object_entries_1.getObjectTypedKeys)(inputObject).reduce((accum, currentKey) => {
12
+ const mappedValue = mapCallback(currentKey, inputObject[currentKey], inputObject);
13
+ if (mappedValue instanceof Promise) {
14
+ gotAPromise = true;
15
+ }
16
+ return {
17
+ ...accum,
18
+ [currentKey]: mappedValue,
19
+ };
20
+ }, {});
21
+ if (gotAPromise) {
22
+ return new Promise(async (resolve, reject) => {
23
+ try {
24
+ await Promise.all((0, object_entries_1.getObjectTypedKeys)(mappedObject).map(async (key) => {
25
+ const value = await mappedObject[key];
26
+ mappedObject[key] = value;
27
+ }));
28
+ resolve(mappedObject);
29
+ }
30
+ catch (error) {
31
+ reject(error);
32
+ }
33
+ });
34
+ }
35
+ else {
36
+ return mappedObject;
37
+ }
38
+ }
39
+ exports.mapObjectValues = mapObjectValues;
@@ -0,0 +1,37 @@
1
+ import { AtLeastOneEntryArray } from '../array';
2
+ import { NoInfer } from '../type';
3
+ /**
4
+ * Checks that the first input, testThisOne, matches the object shape of the second input,
5
+ * compareToThisOne. Does not compare exact values of properties, only types.
6
+ *
7
+ * To allow the test input, the first input, to have additional keys that the compare input, the
8
+ * second input, does not have, pass in a third argument set to true.
9
+ *
10
+ * This function REQUIRES a generic to be assigned to it: it cannot infer it from the inputs.
11
+ *
12
+ * The compare input, the second input, is required to have at least one entry in every array value
13
+ * that exists. If more array values are present, they will be considered other possible types for
14
+ * entries in that array.
15
+ */
16
+ export declare function matchesObjectShape<MatchThisGeneric extends object>(testThisOne: unknown, compareToThisOne: NoInfer<ObjectWithAtLeastSingleEntryArrays<MatchThisGeneric>>, allowExtraProps?: boolean, shouldLogWhy?: boolean): testThisOne is MatchThisGeneric;
17
+ export type ObjectWithAtLeastSingleEntryArrays<BaseObject extends object> = {
18
+ [Prop in keyof BaseObject]: BaseObject[Prop] extends ReadonlyArray<any> ? AtLeastOneEntryArray<BaseObject[Prop]> : BaseObject[Prop] extends object ? ObjectWithAtLeastSingleEntryArrays<BaseObject[Prop]> : BaseObject[Prop];
19
+ };
20
+ export type NestedBoolean<MatchObject extends object> = Partial<{
21
+ [Prop in keyof MatchObject]: MatchObject[Prop] extends object ? NestedBoolean<MatchObject[Prop]> | boolean : boolean;
22
+ }>;
23
+ /**
24
+ * Asserts that the first input, testThisOne, matches the object shape of the second input,
25
+ * compareToThisOne. Does not compare exact values of properties, only types.
26
+ *
27
+ * To allow the test input, the first input, to have additional keys that the compare input, the
28
+ * second input, does not have, pass in a third argument set to true.
29
+ *
30
+ * This function REQUIRES a generic to be assigned to it: it cannot infer it from the inputs.
31
+ *
32
+ * The compare input, the second input, is required to have at least one entry in every array value
33
+ * that exists. If more array values are present, they will be considered other possible types for
34
+ * entries in that array.
35
+ */
36
+ export declare function assertMatchesObjectShape<MatchThisGeneric extends object = never>(testThisOne: unknown, compareToThisOne: NoInfer<ObjectWithAtLeastSingleEntryArrays<MatchThisGeneric>>, allowExtraProps?: boolean, noCheckInnerValueOfTheseKeys?: NestedBoolean<typeof compareToThisOne>): asserts testThisOne is MatchThisGeneric;
37
+ //# sourceMappingURL=matches-object-shape.d.ts.map
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertMatchesObjectShape = exports.matchesObjectShape = void 0;
4
+ const error_1 = require("../error");
5
+ const function_1 = require("../function");
6
+ const object_1 = require("./object");
7
+ const object_entries_1 = require("./object-entries");
8
+ const typed_has_property_1 = require("./typed-has-property");
9
+ /**
10
+ * Checks that the first input, testThisOne, matches the object shape of the second input,
11
+ * compareToThisOne. Does not compare exact values of properties, only types.
12
+ *
13
+ * To allow the test input, the first input, to have additional keys that the compare input, the
14
+ * second input, does not have, pass in a third argument set to true.
15
+ *
16
+ * This function REQUIRES a generic to be assigned to it: it cannot infer it from the inputs.
17
+ *
18
+ * The compare input, the second input, is required to have at least one entry in every array value
19
+ * that exists. If more array values are present, they will be considered other possible types for
20
+ * entries in that array.
21
+ */
22
+ function matchesObjectShape(testThisOne, compareToThisOne, allowExtraProps = false, shouldLogWhy = false) {
23
+ try {
24
+ assertMatchesObjectShape(testThisOne, compareToThisOne, allowExtraProps);
25
+ return true;
26
+ }
27
+ catch (error) {
28
+ if (shouldLogWhy) {
29
+ console.error(error);
30
+ }
31
+ return false;
32
+ }
33
+ }
34
+ exports.matchesObjectShape = matchesObjectShape;
35
+ /**
36
+ * Asserts that the first input, testThisOne, matches the object shape of the second input,
37
+ * compareToThisOne. Does not compare exact values of properties, only types.
38
+ *
39
+ * To allow the test input, the first input, to have additional keys that the compare input, the
40
+ * second input, does not have, pass in a third argument set to true.
41
+ *
42
+ * This function REQUIRES a generic to be assigned to it: it cannot infer it from the inputs.
43
+ *
44
+ * The compare input, the second input, is required to have at least one entry in every array value
45
+ * that exists. If more array values are present, they will be considered other possible types for
46
+ * entries in that array.
47
+ */
48
+ function assertMatchesObjectShape(testThisOne, compareToThisOne, allowExtraProps = false, noCheckInnerValueOfTheseKeys = {}) {
49
+ const testKeys = (0, object_entries_1.getObjectTypedKeys)(testThisOne);
50
+ const matchKeys = new Set((0, object_entries_1.getObjectTypedKeys)(compareToThisOne));
51
+ if (!allowExtraProps) {
52
+ const extraKeys = testKeys.filter((testKey) => !matchKeys.has(testKey));
53
+ if (extraKeys.length) {
54
+ throw new Error(`Test object has extra keys: ${extraKeys.join(', ')}`);
55
+ }
56
+ }
57
+ matchKeys.forEach((key) => {
58
+ var _a;
59
+ if (!(0, typed_has_property_1.typedHasProperty)(testThisOne, key)) {
60
+ throw new Error(`test object does not have key "${String(key)}" from expected shape.`);
61
+ }
62
+ function throwKeyError(reason) {
63
+ throw new Error(`test object value at key "${String(key)}" did not match expected shape: ${reason}`);
64
+ }
65
+ const testValue = testThisOne[key];
66
+ const shouldMatch = compareToThisOne[key];
67
+ if (!noCheckInnerValueOfTheseKeys[key]) {
68
+ compareInnerValue(testValue, shouldMatch, throwKeyError, allowExtraProps, (_a = noCheckInnerValueOfTheseKeys[key]) !== null && _a !== void 0 ? _a : {});
69
+ }
70
+ });
71
+ }
72
+ exports.assertMatchesObjectShape = assertMatchesObjectShape;
73
+ function compareInnerValue(testValue, matchValue, throwKeyError, allowExtraProps, noCheckInnerValueOfTheseKeys) {
74
+ var _a;
75
+ const testType = typeof testValue;
76
+ const shouldMatchType = typeof matchValue;
77
+ if (testType !== shouldMatchType) {
78
+ throwKeyError(`type "${testType}" did not match expected type "${shouldMatchType}"`);
79
+ }
80
+ try {
81
+ if ((0, typed_has_property_1.typedHasProperty)(matchValue, 'constructor')) {
82
+ if (!(0, typed_has_property_1.typedHasProperty)(testValue, 'constructor') ||
83
+ testValue.constructor !== matchValue.constructor) {
84
+ throwKeyError(`constructor "${(_a = testValue === null || testValue === void 0 ? void 0 : testValue.constructor) === null || _a === void 0 ? void 0 : _a.name}" did not match expected constructor "${matchValue.constructor}"`);
85
+ }
86
+ }
87
+ }
88
+ catch (error) {
89
+ // ignore errors from trying to find the constructor
90
+ if (error instanceof throwKeyError) {
91
+ throw error;
92
+ }
93
+ }
94
+ if (Array.isArray(matchValue)) {
95
+ if (!Array.isArray(testValue)) {
96
+ throwKeyError(`expected an array`);
97
+ }
98
+ testValue.forEach((testValueEntry, index) => {
99
+ const errors = matchValue
100
+ .map((matchValue) => {
101
+ try {
102
+ compareInnerValue(testValueEntry, matchValue, throwKeyError, allowExtraProps, noCheckInnerValueOfTheseKeys);
103
+ return undefined;
104
+ }
105
+ catch (error) {
106
+ return new Error(`entry at index "${index}" did not match expected shape: ${(0, error_1.extractErrorMessage)(error)}`);
107
+ }
108
+ })
109
+ .filter(function_1.isTruthy);
110
+ if (errors.length === matchValue.length) {
111
+ throw new Error(`entry at index "${index}" did not match any of the possible types from "${matchValue.join(', ')}"`);
112
+ }
113
+ });
114
+ }
115
+ else if ((0, object_1.isObject)(matchValue)) {
116
+ assertMatchesObjectShape(testValue, matchValue, allowExtraProps, noCheckInnerValueOfTheseKeys);
117
+ }
118
+ }
@@ -0,0 +1,12 @@
1
+ import { UnionToIntersection } from 'type-fest';
2
+ import { ObjectValueType } from './object';
3
+ export type NestedSequentialKeys<ObjectGeneric extends object> = ObjectValueType<{
4
+ [Prop in keyof ObjectGeneric]: NonNullable<ObjectGeneric[Prop]> extends object ? [Prop, ...(NestedSequentialKeys<NonNullable<ObjectGeneric[Prop]>> | [])] : [Prop];
5
+ }>;
6
+ export type NestedKeys<ObjectGeneric extends object> = UnionToIntersection<Extract<ObjectValueType<ObjectGeneric>, object>> extends object ? [
7
+ keyof ObjectGeneric,
8
+ ...(NestedKeys<UnionToIntersection<Extract<ObjectValueType<ObjectGeneric>, object>>> | [])
9
+ ] : [keyof ObjectGeneric];
10
+ export type NestedValue<ObjectGeneric extends object, NestedKeysGeneric extends NestedSequentialKeys<ObjectGeneric>> = NestedKeysGeneric extends readonly [infer FirstEntry, ...infer FollowingEntries] ? FirstEntry extends keyof ObjectGeneric ? FollowingEntries extends never[] ? ObjectGeneric[FirstEntry] : ObjectGeneric[FirstEntry] extends object ? FollowingEntries extends NestedSequentialKeys<ObjectGeneric[FirstEntry]> ? NestedValue<ObjectGeneric[FirstEntry], FollowingEntries> : never : never : never : never;
11
+ export declare function getValueFromNestedKeys<ObjectGeneric extends object, KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(inputObject: ObjectGeneric, nestedKeys: KeysGeneric): NestedValue<ObjectGeneric, KeysGeneric> | undefined;
12
+ //# sourceMappingURL=nested-keys.d.ts.map
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getValueFromNestedKeys = void 0;
4
+ const typed_has_property_1 = require("./typed-has-property");
5
+ function getValueFromNestedKeys(inputObject, nestedKeys) {
6
+ /**
7
+ * Lots of as any casts in here because these types are, under the hood, pretty complex. Since
8
+ * the inputs and outputs of this function are well typed, these internal as any casts do not
9
+ * affect the external API of this function.
10
+ */
11
+ const keyToAccess = nestedKeys[0];
12
+ if (!(0, typed_has_property_1.typedHasProperty)(inputObject, keyToAccess)) {
13
+ return undefined;
14
+ }
15
+ const currentValue = inputObject[keyToAccess];
16
+ if (nestedKeys.length > 1) {
17
+ return getValueFromNestedKeys(currentValue, nestedKeys.slice(1));
18
+ }
19
+ else {
20
+ return currentValue;
21
+ }
22
+ }
23
+ exports.getValueFromNestedKeys = getValueFromNestedKeys;
@@ -0,0 +1,6 @@
1
+ export declare function isKeyof<ObjectGeneric>(key: PropertyKey, object: ObjectGeneric): key is keyof ObjectGeneric;
2
+ export declare function getObjectTypedKeys<ObjectGeneric extends unknown>(input: ObjectGeneric): Array<keyof ObjectGeneric>;
3
+ export declare function getObjectTypedValues<ObjectGeneric extends unknown>(input: ObjectGeneric): ObjectGeneric[keyof ObjectGeneric][];
4
+ export declare function getEntriesSortedByKey(input: object): [string, unknown][];
5
+ export declare function typedObjectFromEntries<KeyType extends PropertyKey, ValueType>(entries: ReadonlyArray<Readonly<[KeyType, ValueType]>>): Record<KeyType, ValueType>;
6
+ //# sourceMappingURL=object-entries.d.ts.map
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typedObjectFromEntries = exports.getEntriesSortedByKey = exports.getObjectTypedValues = exports.getObjectTypedKeys = exports.isKeyof = void 0;
4
+ const typed_has_property_1 = require("./typed-has-property");
5
+ function isKeyof(key, object) {
6
+ return (0, typed_has_property_1.typedHasProperty)(object, key);
7
+ }
8
+ exports.isKeyof = isKeyof;
9
+ function getObjectTypedKeys(input) {
10
+ let reflectKeys;
11
+ try {
12
+ reflectKeys = Reflect.ownKeys(input);
13
+ }
14
+ catch (error) { }
15
+ return (reflectKeys !== null && reflectKeys !== void 0 ? reflectKeys : [
16
+ ...Object.keys(input),
17
+ ...Object.getOwnPropertySymbols(input),
18
+ ]);
19
+ }
20
+ exports.getObjectTypedKeys = getObjectTypedKeys;
21
+ function getObjectTypedValues(input) {
22
+ return getObjectTypedKeys(input).map((key) => input[key]);
23
+ }
24
+ exports.getObjectTypedValues = getObjectTypedValues;
25
+ function getEntriesSortedByKey(input) {
26
+ return Object.entries(input).sort((tupleA, tupleB) => tupleA[0].localeCompare(tupleB[0]));
27
+ }
28
+ exports.getEntriesSortedByKey = getEntriesSortedByKey;
29
+ function typedObjectFromEntries(entries) {
30
+ return Object.fromEntries(entries);
31
+ }
32
+ exports.typedObjectFromEntries = typedObjectFromEntries;
@@ -0,0 +1,10 @@
1
+ export type PartialWithNullable<T extends object> = {
2
+ [Prop in keyof T]?: T[Prop] | null | undefined;
3
+ };
4
+ export declare function isObject(input: any): input is NonNullable<object>;
5
+ export declare function areJsonEqual(a: object, b: object): boolean;
6
+ export declare function filterObject<ObjectGeneric extends object>(inputObject: ObjectGeneric, callback: (key: keyof ObjectGeneric, value: ObjectValueType<ObjectGeneric>, fullObject: ObjectGeneric) => boolean): Partial<ObjectGeneric>;
7
+ /** The input here must be serializable otherwise JSON parsing errors will be thrown */
8
+ export declare function copyThroughJson<T>(input: T): T;
9
+ export type ObjectValueType<T extends object> = T[keyof T];
10
+ //# sourceMappingURL=object.d.ts.map
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.copyThroughJson = exports.filterObject = exports.areJsonEqual = exports.isObject = void 0;
4
+ const object_entries_1 = require("./object-entries");
5
+ function isObject(input) {
6
+ return !!input && typeof input === 'object';
7
+ }
8
+ exports.isObject = isObject;
9
+ function areJsonEqual(a, b) {
10
+ try {
11
+ const sortedAEntries = (0, object_entries_1.getEntriesSortedByKey)(a);
12
+ const sortedBEntries = (0, object_entries_1.getEntriesSortedByKey)(b);
13
+ return JSON.stringify(sortedAEntries) === JSON.stringify(sortedBEntries);
14
+ }
15
+ catch (error) {
16
+ console.error(`Failed to compare objects using JSON.stringify`);
17
+ throw error;
18
+ }
19
+ }
20
+ exports.areJsonEqual = areJsonEqual;
21
+ function filterObject(inputObject, callback) {
22
+ const filteredKeys = (0, object_entries_1.getObjectTypedKeys)(inputObject).filter((key) => {
23
+ const value = inputObject[key];
24
+ return callback(key, value, inputObject);
25
+ });
26
+ return filteredKeys.reduce((accum, key) => {
27
+ accum[key] = inputObject[key];
28
+ return accum;
29
+ }, {});
30
+ }
31
+ exports.filterObject = filterObject;
32
+ /** The input here must be serializable otherwise JSON parsing errors will be thrown */
33
+ function copyThroughJson(input) {
34
+ try {
35
+ return JSON.parse(JSON.stringify(input));
36
+ }
37
+ catch (error) {
38
+ console.error(`Failed to JSON copy for`, input);
39
+ throw error;
40
+ }
41
+ }
42
+ exports.copyThroughJson = copyThroughJson;
@@ -0,0 +1,8 @@
1
+ import { UnionToIntersection } from 'type-fest';
2
+ import { NestedKeys } from './nested-keys';
3
+ type InnerPickDeep<OriginalObjectGeneric extends object, DeepKeys extends any[]> = UnionToIntersection<DeepKeys extends [infer CurrentLevelPick, ...infer RemainingKeys] ? {
4
+ [CurrentProp in Extract<CurrentLevelPick, keyof OriginalObjectGeneric>]: OriginalObjectGeneric[CurrentProp] extends object ? InnerPickDeep<OriginalObjectGeneric[CurrentProp], RemainingKeys> : OriginalObjectGeneric[CurrentProp];
5
+ } : DeepKeys extends [infer CurrentLevelPick] ? CurrentLevelPick extends keyof OriginalObjectGeneric ? Pick<OriginalObjectGeneric, CurrentLevelPick> : never : never>;
6
+ export type PickDeep<OriginalObjectGeneric extends object, DeepKeys extends NestedKeys<OriginalObjectGeneric>> = InnerPickDeep<OriginalObjectGeneric, DeepKeys>;
7
+ export {};
8
+ //# sourceMappingURL=pick-deep.d.ts.map
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { RequiredBy } from '../type';
2
+ type ExtractValue<KeyGeneric extends PropertyKey, ParentGeneric> = KeyGeneric extends keyof ParentGeneric ? RequiredBy<ParentGeneric, KeyGeneric>[KeyGeneric] : KeyGeneric extends keyof Extract<ParentGeneric, Record<KeyGeneric, any>> ? RequiredBy<Extract<ParentGeneric, Record<KeyGeneric, any>>, KeyGeneric>[KeyGeneric] : never;
3
+ type CombinedParentValue<KeyGeneric extends PropertyKey, ParentGeneric> = ExtractValue<KeyGeneric, ParentGeneric> extends never ? unknown : ExtractValue<KeyGeneric, ParentGeneric>;
4
+ type CombineTypeWithKey<KeyGeneric extends PropertyKey, ParentGeneric> = ParentGeneric & Record<KeyGeneric, CombinedParentValue<KeyGeneric, ParentGeneric>>;
5
+ export declare function typedHasProperty<KeyGeneric extends PropertyKey, ParentGeneric>(inputObject: ParentGeneric, inputKey: KeyGeneric): inputObject is CombineTypeWithKey<KeyGeneric, ParentGeneric>;
6
+ export declare function typedHasProperties<KeyGeneric extends PropertyKey, ParentGeneric>(inputObject: ParentGeneric, inputKeys: ReadonlyArray<KeyGeneric>): inputObject is CombineTypeWithKey<KeyGeneric, ParentGeneric>;
7
+ export {};
8
+ //# sourceMappingURL=typed-has-property.d.ts.map
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typedHasProperties = exports.typedHasProperty = void 0;
4
+ const hasPropertyAttempts = [
5
+ (object, key) => {
6
+ return key in object;
7
+ },
8
+ (object, key) => {
9
+ /** This handles cases where the input object can't use `in` directly, like string literals */
10
+ return key in object.constructor.prototype;
11
+ },
12
+ ];
13
+ function typedHasProperty(inputObject, inputKey) {
14
+ if (!inputObject) {
15
+ return false;
16
+ }
17
+ return hasPropertyAttempts.some((attemptCallback) => {
18
+ try {
19
+ return attemptCallback(inputObject, inputKey);
20
+ }
21
+ catch (error) {
22
+ return false;
23
+ }
24
+ });
25
+ }
26
+ exports.typedHasProperty = typedHasProperty;
27
+ function typedHasProperties(inputObject, inputKeys) {
28
+ return inputObject && inputKeys.every((key) => typedHasProperty(inputObject, key));
29
+ }
30
+ exports.typedHasProperties = typedHasProperties;
@@ -1,5 +1,5 @@
1
1
  import { UnionToIntersection } from 'type-fest';
2
- import { NestedKeys } from './nested-keys';
2
+ import { NestedKeys } from './object/nested-keys';
3
3
  type InnerPickDeep<OriginalObjectGeneric extends object, DeepKeys extends any[]> = UnionToIntersection<DeepKeys extends [infer CurrentLevelPick, ...infer RemainingKeys] ? {
4
4
  [CurrentProp in Extract<CurrentLevelPick, keyof OriginalObjectGeneric>]: OriginalObjectGeneric[CurrentProp] extends object ? InnerPickDeep<OriginalObjectGeneric[CurrentProp], RemainingKeys> : OriginalObjectGeneric[CurrentProp];
5
5
  } : DeepKeys extends [infer CurrentLevelPick] ? CurrentLevelPick extends keyof OriginalObjectGeneric ? Pick<OriginalObjectGeneric, CurrentLevelPick> : never : never>;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.waitForCondition = exports.createDeferredPromiseWrapper = exports.wrapPromiseInTimeout = exports.PromiseTimeoutError = exports.isPromiseLike = exports.wait = void 0;
4
4
  const error_1 = require("./error");
5
- const object_1 = require("./object");
5
+ const typed_has_property_1 = require("./object/typed-has-property");
6
6
  function wait(delayMs) {
7
7
  const deferredPromiseWrapper = createDeferredPromiseWrapper();
8
8
  if (delayMs !== Infinity) {
@@ -14,7 +14,7 @@ function wait(delayMs) {
14
14
  }
15
15
  exports.wait = wait;
16
16
  function isPromiseLike(input) {
17
- if ((0, object_1.typedHasProperty)(input, 'then') && typeof input.then === 'function') {
17
+ if ((0, typed_has_property_1.typedHasProperty)(input, 'then') && typeof input.then === 'function') {
18
18
  return true;
19
19
  }
20
20
  else {
@@ -33,9 +33,11 @@ class PromiseTimeoutError extends Error {
33
33
  exports.PromiseTimeoutError = PromiseTimeoutError;
34
34
  function wrapPromiseInTimeout(durationMs, originalPromise) {
35
35
  return new Promise(async (resolve, reject) => {
36
- const timeoutId = setTimeout(() => {
37
- reject(new PromiseTimeoutError(durationMs));
38
- }, durationMs);
36
+ const timeoutId = durationMs === Infinity
37
+ ? undefined
38
+ : setTimeout(() => {
39
+ reject(new PromiseTimeoutError(durationMs));
40
+ }, durationMs);
39
41
  try {
40
42
  const result = await originalPromise;
41
43
  resolve(result);
@@ -0,0 +1,20 @@
1
+ import { AnyFunction } from './function';
2
+ declare function rawGetTypeOf(x: any): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
3
+ type RawTypeOf = ReturnType<typeof rawGetTypeOf>;
4
+ export type RuntimeTypeOf = RawTypeOf | 'array';
5
+ export type RuntimeTypeOfMapping = {
6
+ array: any[];
7
+ bigint: bigint;
8
+ boolean: boolean;
9
+ function: AnyFunction;
10
+ number: number;
11
+ object: Record<PropertyKey, unknown>;
12
+ string: string;
13
+ symbol: symbol;
14
+ undefined: undefined;
15
+ };
16
+ export declare function getRuntimeTypeOf(input: unknown): RuntimeTypeOf;
17
+ export declare function isRuntimeTypeOf<T extends RuntimeTypeOf>(input: unknown, testType: T): input is RuntimeTypeOfMapping[T];
18
+ export declare function assertRuntimeTypeOf<T extends RuntimeTypeOf>(input: unknown, testType: T): asserts input is RuntimeTypeOfMapping[T];
19
+ export {};
20
+ //# sourceMappingURL=runtime-type-of.d.ts.map
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertRuntimeTypeOf = exports.isRuntimeTypeOf = exports.getRuntimeTypeOf = void 0;
4
+ // this function is not used at run time, it's only here for types
5
+ // istanbul ignore next
6
+ function rawGetTypeOf(x) {
7
+ return typeof x;
8
+ }
9
+ function getRuntimeTypeOf(input) {
10
+ return Array.isArray(input) ? 'array' : typeof input;
11
+ }
12
+ exports.getRuntimeTypeOf = getRuntimeTypeOf;
13
+ function isRuntimeTypeOf(input, testType) {
14
+ const inputType = getRuntimeTypeOf(input);
15
+ return inputType === testType;
16
+ }
17
+ exports.isRuntimeTypeOf = isRuntimeTypeOf;
18
+ function assertRuntimeTypeOf(input, testType) {
19
+ if (!isRuntimeTypeOf(input, testType)) {
20
+ throw new TypeError(`'${input}' is not of type '${testType}'`);
21
+ }
22
+ }
23
+ exports.assertRuntimeTypeOf = assertRuntimeTypeOf;
@@ -7,13 +7,18 @@ export * from './augments/date';
7
7
  export * from './augments/environment';
8
8
  export * from './augments/error';
9
9
  export * from './augments/function';
10
- export * from './augments/nested-keys';
11
- export * from './augments/object';
12
- export * from './augments/pick-deep';
10
+ export * from './augments/object/enum';
11
+ export * from './augments/object/map-object';
12
+ export * from './augments/object/matches-object-shape';
13
+ export * from './augments/object/nested-keys';
14
+ export * from './augments/object/object';
15
+ export * from './augments/object/object-entries';
16
+ export * from './augments/object/pick-deep';
17
+ export * from './augments/object/typed-has-property';
13
18
  export * from './augments/promise';
14
19
  export * from './augments/regexp';
20
+ export * from './augments/runtime-type-of';
15
21
  export * from './augments/truncate-number';
16
22
  export * from './augments/tuple';
17
23
  export * from './augments/type';
18
- export * from './augments/type-of';
19
24
  //# sourceMappingURL=index.d.ts.map
package/dist/cjs/index.js CHANGED
@@ -23,12 +23,17 @@ __exportStar(require("./augments/date"), exports);
23
23
  __exportStar(require("./augments/environment"), exports);
24
24
  __exportStar(require("./augments/error"), exports);
25
25
  __exportStar(require("./augments/function"), exports);
26
- __exportStar(require("./augments/nested-keys"), exports);
27
- __exportStar(require("./augments/object"), exports);
28
- __exportStar(require("./augments/pick-deep"), exports);
26
+ __exportStar(require("./augments/object/enum"), exports);
27
+ __exportStar(require("./augments/object/map-object"), exports);
28
+ __exportStar(require("./augments/object/matches-object-shape"), exports);
29
+ __exportStar(require("./augments/object/nested-keys"), exports);
30
+ __exportStar(require("./augments/object/object"), exports);
31
+ __exportStar(require("./augments/object/object-entries"), exports);
32
+ __exportStar(require("./augments/object/pick-deep"), exports);
33
+ __exportStar(require("./augments/object/typed-has-property"), exports);
29
34
  __exportStar(require("./augments/promise"), exports);
30
35
  __exportStar(require("./augments/regexp"), exports);
36
+ __exportStar(require("./augments/runtime-type-of"), exports);
31
37
  __exportStar(require("./augments/truncate-number"), exports);
32
38
  __exportStar(require("./augments/tuple"), exports);
33
39
  __exportStar(require("./augments/type"), exports);
34
- __exportStar(require("./augments/type-of"), exports);
@@ -1,5 +1,5 @@
1
1
  import { UnionToIntersection } from 'type-fest';
2
- import { ObjectValueType } from './object';
2
+ import { ObjectValueType } from '../object';
3
3
  export type NestedSequentialKeys<ObjectGeneric extends object> = ObjectValueType<{
4
4
  [Prop in keyof ObjectGeneric]: NonNullable<ObjectGeneric[Prop]> extends object ? [Prop, ...(NestedSequentialKeys<NonNullable<ObjectGeneric[Prop]>> | [])] : [Prop];
5
5
  }>;
@@ -0,0 +1,5 @@
1
+ export declare function getEnumTypedKeys<T extends object>(input: T): (keyof T)[];
2
+ export declare function getEnumTypedValues<T extends object>(input: T): T[keyof T][];
3
+ export declare function isEnumValue<T extends object>(input: unknown, checkEnum: T): input is T[keyof T];
4
+ export declare function filterToEnumValues<T extends object>(inputs: ReadonlyArray<unknown>, checkEnum: T, caseInsensitive?: boolean): T[keyof T][];
5
+ //# sourceMappingURL=enum.d.ts.map
@@ -0,0 +1,30 @@
1
+ import { getObjectTypedKeys } from './object-entries';
2
+ export function getEnumTypedKeys(input) {
3
+ // enum keys are always strings
4
+ return getObjectTypedKeys(input).filter((key) => isNaN(Number(key)));
5
+ }
6
+ export function getEnumTypedValues(input) {
7
+ const keys = getEnumTypedKeys(input);
8
+ return keys.map((key) => input[key]);
9
+ }
10
+ export function isEnumValue(input, checkEnum) {
11
+ return getEnumTypedValues(checkEnum).includes(input);
12
+ }
13
+ export function filterToEnumValues(inputs, checkEnum, caseInsensitive = false) {
14
+ if (caseInsensitive) {
15
+ return inputs.reduce((accum, currentInput) => {
16
+ const matchedEnumValue = getEnumTypedValues(checkEnum).find((actualEnumValue) => {
17
+ return String(actualEnumValue).toUpperCase() === String(currentInput).toUpperCase();
18
+ });
19
+ if (matchedEnumValue) {
20
+ return accum.concat(matchedEnumValue);
21
+ }
22
+ else {
23
+ return accum;
24
+ }
25
+ }, []);
26
+ }
27
+ else {
28
+ return inputs.filter((input) => isEnumValue(input, checkEnum));
29
+ }
30
+ }