@augment-vir/common 21.0.1 → 21.2.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/object/merge-deep.js +24 -11
- package/dist/cjs/augments/object/nested-keys.js +25 -9
- package/dist/cjs/augments/object/object.js +1 -0
- package/dist/esm/augments/object/merge-deep.js +24 -11
- package/dist/esm/augments/object/nested-keys.js +25 -9
- package/dist/esm/augments/object/object.js +1 -0
- package/dist/types/augments/object/nested-keys.d.ts +10 -6
- package/dist/types/augments/object/object.d.ts +1 -0
- package/package.json +1 -1
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.mergeDeep = void 0;
|
|
4
4
|
const runtime_type_of_1 = require("../runtime-type-of");
|
|
5
5
|
const tuple_1 = require("../tuple");
|
|
6
|
+
const object_1 = require("./object");
|
|
6
7
|
/**
|
|
7
8
|
* Accepts multiple objects and merges their key-value pairs recursively.
|
|
8
9
|
*
|
|
@@ -10,28 +11,40 @@ const tuple_1 = require("../tuple");
|
|
|
10
11
|
*/
|
|
11
12
|
function mergeDeep(...inputs) {
|
|
12
13
|
if (!(0, tuple_1.isLengthAtLeast)(inputs, 1)) {
|
|
14
|
+
// nothing to merge if no inputs
|
|
13
15
|
return {};
|
|
14
16
|
}
|
|
15
17
|
if (inputs.length === 1) {
|
|
18
|
+
// nothing to merge if only one input
|
|
16
19
|
return inputs[0];
|
|
17
20
|
}
|
|
18
|
-
let result =
|
|
21
|
+
let result = undefined;
|
|
19
22
|
const mergeProps = {};
|
|
20
23
|
inputs.forEach((individualInput) => {
|
|
21
24
|
try {
|
|
25
|
+
if (!(0, object_1.isObject)(individualInput)) {
|
|
26
|
+
/** If not an object, we can't merge. So overwrite instead. */
|
|
27
|
+
result = individualInput;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
22
30
|
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
mergeProps[key] = mergePropsArray;
|
|
27
|
-
}
|
|
28
|
-
mergePropsArray.push(value);
|
|
31
|
+
const mergePropsArray = mergeProps[key] || [];
|
|
32
|
+
if (!mergeProps[key]) {
|
|
33
|
+
mergeProps[key] = mergePropsArray;
|
|
29
34
|
}
|
|
35
|
+
mergePropsArray.push(value);
|
|
30
36
|
});
|
|
31
|
-
result
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
if (result) {
|
|
38
|
+
if ((0, runtime_type_of_1.isRuntimeTypeOf)(result, 'object')) {
|
|
39
|
+
result = {
|
|
40
|
+
...result,
|
|
41
|
+
...individualInput,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
result = individualInput;
|
|
47
|
+
}
|
|
35
48
|
}
|
|
36
49
|
catch (error) {
|
|
37
50
|
/** Ignore errors, such as individualInput not actually being an object. */
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getValueFromNestedKeys = exports.setValueWithNestedKeys = void 0;
|
|
4
|
+
const runtime_type_of_1 = require("../runtime-type-of");
|
|
4
5
|
const object_1 = require("./object");
|
|
5
6
|
const typed_has_property_1 = require("./typed-has-property");
|
|
6
|
-
function setValueWithNestedKeys(
|
|
7
|
+
function setValueWithNestedKeys(originalObject, nestedKeys, value) {
|
|
7
8
|
/**
|
|
8
9
|
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
9
10
|
* the inputs and outputs of this function are well typed, these internal as any casts do not
|
|
10
11
|
* affect the external API of this function.
|
|
11
12
|
*/
|
|
12
|
-
const
|
|
13
|
+
const nestedKeysInput = nestedKeys;
|
|
14
|
+
const inputObject = originalObject;
|
|
15
|
+
if ((0, runtime_type_of_1.isRuntimeTypeOf)(inputObject, 'array')) {
|
|
16
|
+
inputObject.forEach((entry) => {
|
|
17
|
+
if ((0, object_1.isObject)(entry)) {
|
|
18
|
+
setValueWithNestedKeys(entry, nestedKeysInput, value);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const nextKey = nestedKeysInput[0];
|
|
13
24
|
if (!(nextKey in inputObject)) {
|
|
14
25
|
inputObject[nextKey] = {};
|
|
15
26
|
}
|
|
@@ -17,27 +28,32 @@ function setValueWithNestedKeys(inputObject, nestedKeys, value) {
|
|
|
17
28
|
throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`);
|
|
18
29
|
}
|
|
19
30
|
const nextParent = inputObject[nextKey];
|
|
20
|
-
if (
|
|
21
|
-
setValueWithNestedKeys(nextParent,
|
|
31
|
+
if (nestedKeysInput.length > 2) {
|
|
32
|
+
setValueWithNestedKeys(nextParent, nestedKeysInput.slice(1), value);
|
|
22
33
|
}
|
|
23
34
|
else {
|
|
24
|
-
nextParent[
|
|
35
|
+
nextParent[nestedKeysInput[1]] = value;
|
|
25
36
|
}
|
|
26
37
|
}
|
|
27
38
|
exports.setValueWithNestedKeys = setValueWithNestedKeys;
|
|
28
|
-
function getValueFromNestedKeys(
|
|
39
|
+
function getValueFromNestedKeys(originalObject, nestedKeys) {
|
|
29
40
|
/**
|
|
30
41
|
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
31
42
|
* the inputs and outputs of this function are well typed, these internal as any casts do not
|
|
32
43
|
* affect the external API of this function.
|
|
33
44
|
*/
|
|
34
|
-
const
|
|
45
|
+
const nestedKeysInput = nestedKeys;
|
|
46
|
+
const inputObject = originalObject;
|
|
47
|
+
if ((0, runtime_type_of_1.isRuntimeTypeOf)(inputObject, 'array')) {
|
|
48
|
+
return inputObject.map((entry) => getValueFromNestedKeys(entry, nestedKeys));
|
|
49
|
+
}
|
|
50
|
+
const keyToAccess = nestedKeysInput[0];
|
|
35
51
|
if (!(0, typed_has_property_1.typedHasProperty)(inputObject, keyToAccess)) {
|
|
36
52
|
return undefined;
|
|
37
53
|
}
|
|
38
54
|
const currentValue = inputObject[keyToAccess];
|
|
39
|
-
if (
|
|
40
|
-
return getValueFromNestedKeys(currentValue,
|
|
55
|
+
if (nestedKeysInput.length > 1) {
|
|
56
|
+
return getValueFromNestedKeys(currentValue, nestedKeysInput.slice(1));
|
|
41
57
|
}
|
|
42
58
|
else {
|
|
43
59
|
return currentValue;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.copyThroughJson = exports.isObject = void 0;
|
|
4
|
+
/** Checks if the input is non-null and an object. Includes arrays. */
|
|
4
5
|
function isObject(input) {
|
|
5
6
|
return !!input && typeof input === 'object';
|
|
6
7
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isRuntimeTypeOf } from '../runtime-type-of';
|
|
2
2
|
import { isLengthAtLeast } from '../tuple';
|
|
3
|
+
import { isObject } from './object';
|
|
3
4
|
/**
|
|
4
5
|
* Accepts multiple objects and merges their key-value pairs recursively.
|
|
5
6
|
*
|
|
@@ -7,28 +8,40 @@ import { isLengthAtLeast } from '../tuple';
|
|
|
7
8
|
*/
|
|
8
9
|
export function mergeDeep(...inputs) {
|
|
9
10
|
if (!isLengthAtLeast(inputs, 1)) {
|
|
11
|
+
// nothing to merge if no inputs
|
|
10
12
|
return {};
|
|
11
13
|
}
|
|
12
14
|
if (inputs.length === 1) {
|
|
15
|
+
// nothing to merge if only one input
|
|
13
16
|
return inputs[0];
|
|
14
17
|
}
|
|
15
|
-
let result =
|
|
18
|
+
let result = undefined;
|
|
16
19
|
const mergeProps = {};
|
|
17
20
|
inputs.forEach((individualInput) => {
|
|
18
21
|
try {
|
|
22
|
+
if (!isObject(individualInput)) {
|
|
23
|
+
/** If not an object, we can't merge. So overwrite instead. */
|
|
24
|
+
result = individualInput;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
19
27
|
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
mergeProps[key] = mergePropsArray;
|
|
24
|
-
}
|
|
25
|
-
mergePropsArray.push(value);
|
|
28
|
+
const mergePropsArray = mergeProps[key] || [];
|
|
29
|
+
if (!mergeProps[key]) {
|
|
30
|
+
mergeProps[key] = mergePropsArray;
|
|
26
31
|
}
|
|
32
|
+
mergePropsArray.push(value);
|
|
27
33
|
});
|
|
28
|
-
result
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
if (result) {
|
|
35
|
+
if (isRuntimeTypeOf(result, 'object')) {
|
|
36
|
+
result = {
|
|
37
|
+
...result,
|
|
38
|
+
...individualInput,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
result = individualInput;
|
|
44
|
+
}
|
|
32
45
|
}
|
|
33
46
|
catch (error) {
|
|
34
47
|
/** Ignore errors, such as individualInput not actually being an object. */
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
import { isRuntimeTypeOf } from '../runtime-type-of';
|
|
1
2
|
import { isObject } from './object';
|
|
2
3
|
import { typedHasProperty } from './typed-has-property';
|
|
3
|
-
export function setValueWithNestedKeys(
|
|
4
|
+
export function setValueWithNestedKeys(originalObject, nestedKeys, value) {
|
|
4
5
|
/**
|
|
5
6
|
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
6
7
|
* the inputs and outputs of this function are well typed, these internal as any casts do not
|
|
7
8
|
* affect the external API of this function.
|
|
8
9
|
*/
|
|
9
|
-
const
|
|
10
|
+
const nestedKeysInput = nestedKeys;
|
|
11
|
+
const inputObject = originalObject;
|
|
12
|
+
if (isRuntimeTypeOf(inputObject, 'array')) {
|
|
13
|
+
inputObject.forEach((entry) => {
|
|
14
|
+
if (isObject(entry)) {
|
|
15
|
+
setValueWithNestedKeys(entry, nestedKeysInput, value);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const nextKey = nestedKeysInput[0];
|
|
10
21
|
if (!(nextKey in inputObject)) {
|
|
11
22
|
inputObject[nextKey] = {};
|
|
12
23
|
}
|
|
@@ -14,26 +25,31 @@ export function setValueWithNestedKeys(inputObject, nestedKeys, value) {
|
|
|
14
25
|
throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`);
|
|
15
26
|
}
|
|
16
27
|
const nextParent = inputObject[nextKey];
|
|
17
|
-
if (
|
|
18
|
-
setValueWithNestedKeys(nextParent,
|
|
28
|
+
if (nestedKeysInput.length > 2) {
|
|
29
|
+
setValueWithNestedKeys(nextParent, nestedKeysInput.slice(1), value);
|
|
19
30
|
}
|
|
20
31
|
else {
|
|
21
|
-
nextParent[
|
|
32
|
+
nextParent[nestedKeysInput[1]] = value;
|
|
22
33
|
}
|
|
23
34
|
}
|
|
24
|
-
export function getValueFromNestedKeys(
|
|
35
|
+
export function getValueFromNestedKeys(originalObject, nestedKeys) {
|
|
25
36
|
/**
|
|
26
37
|
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since
|
|
27
38
|
* the inputs and outputs of this function are well typed, these internal as any casts do not
|
|
28
39
|
* affect the external API of this function.
|
|
29
40
|
*/
|
|
30
|
-
const
|
|
41
|
+
const nestedKeysInput = nestedKeys;
|
|
42
|
+
const inputObject = originalObject;
|
|
43
|
+
if (isRuntimeTypeOf(inputObject, 'array')) {
|
|
44
|
+
return inputObject.map((entry) => getValueFromNestedKeys(entry, nestedKeys));
|
|
45
|
+
}
|
|
46
|
+
const keyToAccess = nestedKeysInput[0];
|
|
31
47
|
if (!typedHasProperty(inputObject, keyToAccess)) {
|
|
32
48
|
return undefined;
|
|
33
49
|
}
|
|
34
50
|
const currentValue = inputObject[keyToAccess];
|
|
35
|
-
if (
|
|
36
|
-
return getValueFromNestedKeys(currentValue,
|
|
51
|
+
if (nestedKeysInput.length > 1) {
|
|
52
|
+
return getValueFromNestedKeys(currentValue, nestedKeysInput.slice(1));
|
|
37
53
|
}
|
|
38
54
|
else {
|
|
39
55
|
return currentValue;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import { ArrayElement } from '../type';
|
|
1
2
|
import { PropertyValueType } from './object';
|
|
2
3
|
import { UnionToIntersection } from './old-union-to-intersection';
|
|
3
|
-
export type NestedSequentialKeys<ObjectGeneric extends object> = PropertyValueType<{
|
|
4
|
-
[Prop in keyof ObjectGeneric]: NonNullable<ObjectGeneric[Prop]> extends object ? Readonly<[
|
|
4
|
+
export type NestedSequentialKeys<ObjectGeneric extends object> = NonNullable<ObjectGeneric> extends ReadonlyArray<any> ? NestedSequentialKeys<Extract<NonNullable<ObjectGeneric>[number], object>> : PropertyValueType<{
|
|
5
|
+
[Prop in keyof ObjectGeneric]: NonNullable<ObjectGeneric[Prop]> extends object ? Readonly<[
|
|
6
|
+
Prop,
|
|
7
|
+
...(NestedSequentialKeys<NonNullable<ObjectGeneric[Prop]>> | [])
|
|
8
|
+
]> : Readonly<[Prop]>;
|
|
5
9
|
}>;
|
|
6
|
-
export type NestedKeys<ObjectGeneric extends object> = UnionToIntersection<Extract<PropertyValueType<ObjectGeneric>, object>> extends object ? [
|
|
10
|
+
export type NestedKeys<ObjectGeneric extends object> = ObjectGeneric extends ReadonlyArray<any> ? NestedKeys<ArrayElement<ObjectGeneric>> : UnionToIntersection<Extract<PropertyValueType<ObjectGeneric>, object>> extends object ? [
|
|
7
11
|
keyof ObjectGeneric,
|
|
8
12
|
...(NestedKeys<UnionToIntersection<Extract<PropertyValueType<ObjectGeneric>, object>>> | [])
|
|
9
13
|
] : [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 setValueWithNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(
|
|
12
|
-
export declare function getValueFromNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(
|
|
14
|
+
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 declare function setValueWithNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(originalObject: ObjectGeneric, nestedKeys: KeysGeneric, value: NestedValue<ObjectGeneric, KeysGeneric>): void;
|
|
16
|
+
export declare function getValueFromNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(originalObject: ObjectGeneric, nestedKeys: KeysGeneric): NestedValue<ObjectGeneric, KeysGeneric>;
|
|
@@ -4,6 +4,7 @@ export type PartialAndNullable<T extends object> = {
|
|
|
4
4
|
export type PartialAndUndefined<T extends object> = {
|
|
5
5
|
[Prop in keyof T]?: T[Prop] | undefined;
|
|
6
6
|
};
|
|
7
|
+
/** Checks if the input is non-null and an object. Includes arrays. */
|
|
7
8
|
export declare function isObject(input: any): input is NonNullable<object>;
|
|
8
9
|
/** The input here must be serializable otherwise JSON parsing errors will be thrown */
|
|
9
10
|
export declare function copyThroughJson<T>(input: T): T;
|