@augment-vir/common 28.2.2 → 28.2.4

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getValueFromNestedKeys = exports.setValueWithNestedKeys = void 0;
4
4
  const run_time_assertions_1 = require("run-time-assertions");
5
+ const tuple_1 = require("../tuple");
5
6
  const object_1 = require("./object");
6
7
  const typed_has_property_1 = require("./typed-has-property");
7
8
  /** This mutates the {@link originalObject} input. */
@@ -19,21 +20,18 @@ function setValueWithNestedKeys(originalObject, nestedKeys, value) {
19
20
  setValueWithNestedKeys(entry, nestedKeysInput, value);
20
21
  }
21
22
  });
22
- return;
23
23
  }
24
- const nextKey = nestedKeysInput[0];
25
- if (!(nextKey in inputObject)) {
26
- inputObject[nextKey] = {};
27
- }
28
- else if (!(0, object_1.isObject)(inputObject[nextKey])) {
29
- throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`);
30
- }
31
- const nextParent = inputObject[nextKey];
32
- if (nestedKeysInput.length > 2) {
24
+ else if ((0, tuple_1.isLengthAtLeast)(nestedKeysInput, 2)) {
25
+ /** If there are more keys to traverse into. */
26
+ const nextKey = nestedKeysInput[0];
27
+ if (!(nextKey in inputObject)) {
28
+ inputObject[nextKey] = {};
29
+ }
30
+ const nextParent = inputObject[nextKey];
33
31
  setValueWithNestedKeys(nextParent, nestedKeysInput.slice(1), value);
34
32
  }
35
- else {
36
- nextParent[nestedKeysInput[1]] = value;
33
+ else if ((0, tuple_1.isLengthAtLeast)(nestedKeysInput, 1)) {
34
+ inputObject[nestedKeysInput[0]] = value;
37
35
  }
38
36
  }
39
37
  exports.setValueWithNestedKeys = setValueWithNestedKeys;
@@ -1,4 +1,5 @@
1
1
  import { isRunTimeType } from 'run-time-assertions';
2
+ import { isLengthAtLeast } from '../tuple';
2
3
  import { isObject } from './object';
3
4
  import { typedHasProperty } from './typed-has-property';
4
5
  /** This mutates the {@link originalObject} input. */
@@ -16,21 +17,18 @@ export function setValueWithNestedKeys(originalObject, nestedKeys, value) {
16
17
  setValueWithNestedKeys(entry, nestedKeysInput, value);
17
18
  }
18
19
  });
19
- return;
20
20
  }
21
- const nextKey = nestedKeysInput[0];
22
- if (!(nextKey in inputObject)) {
23
- inputObject[nextKey] = {};
24
- }
25
- else if (!isObject(inputObject[nextKey])) {
26
- throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`);
27
- }
28
- const nextParent = inputObject[nextKey];
29
- if (nestedKeysInput.length > 2) {
21
+ else if (isLengthAtLeast(nestedKeysInput, 2)) {
22
+ /** If there are more keys to traverse into. */
23
+ const nextKey = nestedKeysInput[0];
24
+ if (!(nextKey in inputObject)) {
25
+ inputObject[nextKey] = {};
26
+ }
27
+ const nextParent = inputObject[nextKey];
30
28
  setValueWithNestedKeys(nextParent, nestedKeysInput.slice(1), value);
31
29
  }
32
- else {
33
- nextParent[nestedKeysInput[1]] = value;
30
+ else if (isLengthAtLeast(nestedKeysInput, 1)) {
31
+ inputObject[nestedKeysInput[0]] = value;
34
32
  }
35
33
  }
36
34
  export function getValueFromNestedKeys(originalObject, nestedKeys) {
@@ -12,7 +12,7 @@ export type NestedKeys<ObjectGeneric extends object, Depth extends TsRecursionTr
12
12
  keyof ObjectGeneric,
13
13
  ...(NestedKeys<UnionToIntersection<Extract<PropertyValueType<ObjectGeneric>, object>>, TsRecurse<Depth>> | [])
14
14
  ] : [keyof ObjectGeneric];
15
- export type NestedValue<ObjectGeneric extends object, NestedKeysGeneric extends NestedSequentialKeys<ObjectGeneric>> = ObjectGeneric extends ReadonlyArray<any> ? NestedValue<Extract<ObjectGeneric[number], object>, NestedKeysGeneric>[] : 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;
15
+ export type NestedValue<ObjectGeneric extends object, NestedKeysGeneric extends NestedSequentialKeys<ObjectGeneric>, Depth extends TsRecursionTracker = TsRecursionStart> = ObjectGeneric extends ReadonlyArray<any> ? NestedValue<Extract<ObjectGeneric[number], object>, NestedKeysGeneric, TsRecurse<Depth>>[] : 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, TsRecurse<Depth>> : never : never : never : never;
16
16
  /** This mutates the {@link originalObject} input. */
17
17
  export declare function setValueWithNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(originalObject: ObjectGeneric, nestedKeys: Readonly<KeysGeneric>, value: Readonly<NestedValue<ObjectGeneric, KeysGeneric>>): void;
18
18
  export declare function getValueFromNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(originalObject: ObjectGeneric, nestedKeys: KeysGeneric): NestedValue<ObjectGeneric, KeysGeneric>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "28.2.2",
3
+ "version": "28.2.4",
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"