@augment-vir/common 19.5.0 → 19.6.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,7 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getValueFromNestedKeys = void 0;
|
|
3
|
+
exports.getValueFromNestedKeys = exports.setValueWithNestedKeys = void 0;
|
|
4
|
+
const object_1 = require("./object");
|
|
4
5
|
const typed_has_property_1 = require("./typed-has-property");
|
|
6
|
+
function setValueWithNestedKeys(inputObject, nestedKeys, value) {
|
|
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 nextKey = nestedKeys[0];
|
|
13
|
+
if (!(nextKey in inputObject)) {
|
|
14
|
+
inputObject[nextKey] = {};
|
|
15
|
+
}
|
|
16
|
+
else if (!(0, object_1.isObject)(inputObject[nextKey])) {
|
|
17
|
+
throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`);
|
|
18
|
+
}
|
|
19
|
+
const nextParent = inputObject[nextKey];
|
|
20
|
+
if (nestedKeys.length > 2) {
|
|
21
|
+
setValueWithNestedKeys(nextParent, nestedKeys.slice(1), value);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
nextParent[nestedKeys[1]] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.setValueWithNestedKeys = setValueWithNestedKeys;
|
|
5
28
|
function getValueFromNestedKeys(inputObject, nestedKeys) {
|
|
6
29
|
/**
|
|
7
30
|
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
@@ -1,4 +1,26 @@
|
|
|
1
|
+
import { isObject } from './object';
|
|
1
2
|
import { typedHasProperty } from './typed-has-property';
|
|
3
|
+
export function setValueWithNestedKeys(inputObject, nestedKeys, value) {
|
|
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 nextKey = nestedKeys[0];
|
|
10
|
+
if (!(nextKey in inputObject)) {
|
|
11
|
+
inputObject[nextKey] = {};
|
|
12
|
+
}
|
|
13
|
+
else if (!isObject(inputObject[nextKey])) {
|
|
14
|
+
throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`);
|
|
15
|
+
}
|
|
16
|
+
const nextParent = inputObject[nextKey];
|
|
17
|
+
if (nestedKeys.length > 2) {
|
|
18
|
+
setValueWithNestedKeys(nextParent, nestedKeys.slice(1), value);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
nextParent[nestedKeys[1]] = value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
2
24
|
export function getValueFromNestedKeys(inputObject, nestedKeys) {
|
|
3
25
|
/**
|
|
4
26
|
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
@@ -8,4 +8,5 @@ export type NestedKeys<ObjectGeneric extends object> = UnionToIntersection<Extra
|
|
|
8
8
|
...(NestedKeys<UnionToIntersection<Extract<PropertyValueType<ObjectGeneric>, object>>> | [])
|
|
9
9
|
] : [keyof ObjectGeneric];
|
|
10
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 setValueWithNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(inputObject: ObjectGeneric, nestedKeys: KeysGeneric, value: NestedValue<ObjectGeneric, KeysGeneric>): void;
|
|
11
12
|
export declare function getValueFromNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(inputObject: ObjectGeneric, nestedKeys: KeysGeneric): NestedValue<ObjectGeneric, KeysGeneric> | undefined;
|