@augment-vir/common 6.2.0 → 6.3.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/dist/cjs/augments/nested-keys.d.ts +12 -0
- package/dist/cjs/augments/nested-keys.js +30 -0
- package/dist/cjs/augments/pick-deep.d.ts +6 -0
- package/dist/cjs/augments/pick-deep.js +2 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +2 -0
- package/dist/esm/augments/nested-keys.d.ts +12 -0
- package/dist/esm/augments/nested-keys.js +26 -0
- package/dist/esm/augments/pick-deep.d.ts +6 -0
- package/dist/esm/augments/pick-deep.js +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/types/augments/nested-keys.d.ts +12 -0
- package/dist/types/augments/pick-deep.d.ts +6 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +4 -3
|
@@ -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,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getValueFromNestedKeys = void 0;
|
|
4
|
+
const error_1 = require("./error");
|
|
5
|
+
const object_1 = require("./object");
|
|
6
|
+
function getValueFromNestedKeys(inputObject, nestedKeys) {
|
|
7
|
+
/**
|
|
8
|
+
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
9
|
+
* the inputs and outputs of this function are well typed, these internal as any casts do not
|
|
10
|
+
* affect the external API of this function.
|
|
11
|
+
*/
|
|
12
|
+
const keyToAccess = nestedKeys[0];
|
|
13
|
+
try {
|
|
14
|
+
if (!(0, object_1.typedHasProperty)(inputObject, keyToAccess)) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
const currentValue = inputObject[keyToAccess];
|
|
18
|
+
if (nestedKeys.length > 1) {
|
|
19
|
+
return getValueFromNestedKeys(currentValue, nestedKeys.slice(1));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return currentValue;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error({ inputObject, nestedKeys });
|
|
27
|
+
throw new Error(`Failed to traverse into inputObject using key "${String(keyToAccess)}": ${(0, error_1.extractErrorMessage)(error)}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.getValueFromNestedKeys = getValueFromNestedKeys;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { UnionToIntersection } from 'type-fest';
|
|
2
|
+
import { NestedKeys } from './nested-keys';
|
|
3
|
+
export type PickDeep<OriginalObjectGeneric extends object, DeepKeys extends NestedKeys<OriginalObjectGeneric>> = UnionToIntersection<DeepKeys extends [infer CurrentLevelPick, ...infer ExtraKeys] ? CurrentLevelPick extends PropertyKey ? CurrentLevelPick extends keyof OriginalObjectGeneric ? {
|
|
4
|
+
[CurrentProp in CurrentLevelPick]: OriginalObjectGeneric[CurrentProp] extends object ? ExtraKeys extends NestedKeys<OriginalObjectGeneric[CurrentProp]> ? PickDeep<OriginalObjectGeneric[CurrentProp], ExtraKeys> : {} : OriginalObjectGeneric[CurrentProp];
|
|
5
|
+
} : never : never : DeepKeys extends [infer CurrentLevelPick] ? CurrentLevelPick extends keyof OriginalObjectGeneric ? Pick<OriginalObjectGeneric, CurrentLevelPick> : never : never>;
|
|
6
|
+
//# sourceMappingURL=pick-deep.d.ts.map
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ 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';
|
|
10
11
|
export * from './augments/object';
|
|
12
|
+
export * from './augments/pick-deep';
|
|
11
13
|
export * from './augments/promise';
|
|
12
14
|
export * from './augments/regexp';
|
|
13
15
|
export * from './augments/tuple';
|
package/dist/cjs/index.js
CHANGED
|
@@ -23,7 +23,9 @@ __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);
|
|
26
27
|
__exportStar(require("./augments/object"), exports);
|
|
28
|
+
__exportStar(require("./augments/pick-deep"), exports);
|
|
27
29
|
__exportStar(require("./augments/promise"), exports);
|
|
28
30
|
__exportStar(require("./augments/regexp"), exports);
|
|
29
31
|
__exportStar(require("./augments/tuple"), exports);
|
|
@@ -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,26 @@
|
|
|
1
|
+
import { extractErrorMessage } from './error';
|
|
2
|
+
import { typedHasProperty } from './object';
|
|
3
|
+
export function getValueFromNestedKeys(inputObject, nestedKeys) {
|
|
4
|
+
/**
|
|
5
|
+
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
6
|
+
* the inputs and outputs of this function are well typed, these internal as any casts do not
|
|
7
|
+
* affect the external API of this function.
|
|
8
|
+
*/
|
|
9
|
+
const keyToAccess = nestedKeys[0];
|
|
10
|
+
try {
|
|
11
|
+
if (!typedHasProperty(inputObject, keyToAccess)) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const currentValue = inputObject[keyToAccess];
|
|
15
|
+
if (nestedKeys.length > 1) {
|
|
16
|
+
return getValueFromNestedKeys(currentValue, nestedKeys.slice(1));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return currentValue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error({ inputObject, nestedKeys });
|
|
24
|
+
throw new Error(`Failed to traverse into inputObject using key "${String(keyToAccess)}": ${extractErrorMessage(error)}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { UnionToIntersection } from 'type-fest';
|
|
2
|
+
import { NestedKeys } from './nested-keys';
|
|
3
|
+
export type PickDeep<OriginalObjectGeneric extends object, DeepKeys extends NestedKeys<OriginalObjectGeneric>> = UnionToIntersection<DeepKeys extends [infer CurrentLevelPick, ...infer ExtraKeys] ? CurrentLevelPick extends PropertyKey ? CurrentLevelPick extends keyof OriginalObjectGeneric ? {
|
|
4
|
+
[CurrentProp in CurrentLevelPick]: OriginalObjectGeneric[CurrentProp] extends object ? ExtraKeys extends NestedKeys<OriginalObjectGeneric[CurrentProp]> ? PickDeep<OriginalObjectGeneric[CurrentProp], ExtraKeys> : {} : OriginalObjectGeneric[CurrentProp];
|
|
5
|
+
} : never : never : DeepKeys extends [infer CurrentLevelPick] ? CurrentLevelPick extends keyof OriginalObjectGeneric ? Pick<OriginalObjectGeneric, CurrentLevelPick> : never : never>;
|
|
6
|
+
//# sourceMappingURL=pick-deep.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ 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';
|
|
10
11
|
export * from './augments/object';
|
|
12
|
+
export * from './augments/pick-deep';
|
|
11
13
|
export * from './augments/promise';
|
|
12
14
|
export * from './augments/regexp';
|
|
13
15
|
export * from './augments/tuple';
|
package/dist/esm/index.js
CHANGED
|
@@ -7,7 +7,9 @@ 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';
|
|
10
11
|
export * from './augments/object';
|
|
12
|
+
export * from './augments/pick-deep';
|
|
11
13
|
export * from './augments/promise';
|
|
12
14
|
export * from './augments/regexp';
|
|
13
15
|
export * from './augments/tuple';
|
|
@@ -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,6 @@
|
|
|
1
|
+
import { UnionToIntersection } from 'type-fest';
|
|
2
|
+
import { NestedKeys } from './nested-keys';
|
|
3
|
+
export type PickDeep<OriginalObjectGeneric extends object, DeepKeys extends NestedKeys<OriginalObjectGeneric>> = UnionToIntersection<DeepKeys extends [infer CurrentLevelPick, ...infer ExtraKeys] ? CurrentLevelPick extends PropertyKey ? CurrentLevelPick extends keyof OriginalObjectGeneric ? {
|
|
4
|
+
[CurrentProp in CurrentLevelPick]: OriginalObjectGeneric[CurrentProp] extends object ? ExtraKeys extends NestedKeys<OriginalObjectGeneric[CurrentProp]> ? PickDeep<OriginalObjectGeneric[CurrentProp], ExtraKeys> : {} : OriginalObjectGeneric[CurrentProp];
|
|
5
|
+
} : never : never : DeepKeys extends [infer CurrentLevelPick] ? CurrentLevelPick extends keyof OriginalObjectGeneric ? Pick<OriginalObjectGeneric, CurrentLevelPick> : never : never>;
|
|
6
|
+
//# sourceMappingURL=pick-deep.d.ts.map
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ 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';
|
|
10
11
|
export * from './augments/object';
|
|
12
|
+
export * from './augments/pick-deep';
|
|
11
13
|
export * from './augments/promise';
|
|
12
14
|
export * from './augments/regexp';
|
|
13
15
|
export * from './augments/tuple';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.3.0",
|
|
4
4
|
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/electrovir/augment-vir/issues"
|
|
@@ -19,10 +19,11 @@
|
|
|
19
19
|
"types": "dist/types/index.d.ts",
|
|
20
20
|
"scripts": {
|
|
21
21
|
"compile": "tsc --project tsconfig.json && tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
|
|
22
|
-
"test": "echo \"use common-test to run tests\" && exit 0"
|
|
22
|
+
"test": "echo \"use common-test to run tests\" && exit 0",
|
|
23
|
+
"test:coverage": "npm test"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
|
-
"type-fest": "^3.
|
|
26
|
+
"type-fest": "^3.5.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"typescript": "^4.9.4"
|