@augment-vir/common 21.1.0 → 21.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/number.js +11 -1
- package/dist/cjs/augments/object/nested-keys.js +25 -9
- package/dist/esm/augments/number.js +9 -0
- package/dist/esm/augments/object/nested-keys.js +25 -9
- package/dist/types/augments/number.d.ts +5 -0
- package/dist/types/augments/object/nested-keys.d.ts +10 -6
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toEnsuredNumber = void 0;
|
|
3
|
+
exports.wrapNumber = exports.toEnsuredNumber = void 0;
|
|
4
4
|
function toEnsuredNumber(input) {
|
|
5
5
|
const numeric = Number(input);
|
|
6
6
|
if (isNaN(numeric)) {
|
|
@@ -11,3 +11,13 @@ function toEnsuredNumber(input) {
|
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
exports.toEnsuredNumber = toEnsuredNumber;
|
|
14
|
+
function wrapNumber({ max, min, value }) {
|
|
15
|
+
if (value > max) {
|
|
16
|
+
return min;
|
|
17
|
+
}
|
|
18
|
+
else if (value < min) {
|
|
19
|
+
return max;
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
exports.wrapNumber = wrapNumber;
|
|
@@ -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,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>;
|