@augment-vir/common
Advanced tools
Comparing version 19.5.0 to 19.6.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getValueFromNestedKeys = void 0; | ||
exports.getValueFromNestedKeys = exports.setValueWithNestedKeys = void 0; | ||
const object_1 = require("./object"); | ||
const typed_has_property_1 = require("./typed-has-property"); | ||
function setValueWithNestedKeys(inputObject, nestedKeys, value) { | ||
/** | ||
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since | ||
* the inputs and outputs of this function are well typed, these internal as any casts do not | ||
* affect the external API of this function. | ||
*/ | ||
const nextKey = nestedKeys[0]; | ||
if (!(nextKey in inputObject)) { | ||
inputObject[nextKey] = {}; | ||
} | ||
else if (!(0, object_1.isObject)(inputObject[nextKey])) { | ||
throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`); | ||
} | ||
const nextParent = inputObject[nextKey]; | ||
if (nestedKeys.length > 2) { | ||
setValueWithNestedKeys(nextParent, nestedKeys.slice(1), value); | ||
} | ||
else { | ||
nextParent[nestedKeys[1]] = value; | ||
} | ||
} | ||
exports.setValueWithNestedKeys = setValueWithNestedKeys; | ||
function getValueFromNestedKeys(inputObject, nestedKeys) { | ||
@@ -6,0 +29,0 @@ /** |
@@ -0,2 +1,24 @@ | ||
import { isObject } from './object'; | ||
import { typedHasProperty } from './typed-has-property'; | ||
export function setValueWithNestedKeys(inputObject, nestedKeys, value) { | ||
/** | ||
* Lots of as any casts in here because these types are, under the hood, pretty complex. Since | ||
* the inputs and outputs of this function are well typed, these internal as any casts do not | ||
* affect the external API of this function. | ||
*/ | ||
const nextKey = nestedKeys[0]; | ||
if (!(nextKey in inputObject)) { | ||
inputObject[nextKey] = {}; | ||
} | ||
else if (!isObject(inputObject[nextKey])) { | ||
throw new Error(`Cannot set value at key '${String(nextKey)}' as its not an object.`); | ||
} | ||
const nextParent = inputObject[nextKey]; | ||
if (nestedKeys.length > 2) { | ||
setValueWithNestedKeys(nextParent, nestedKeys.slice(1), value); | ||
} | ||
else { | ||
nextParent[nestedKeys[1]] = value; | ||
} | ||
} | ||
export function getValueFromNestedKeys(inputObject, nestedKeys) { | ||
@@ -3,0 +25,0 @@ /** |
@@ -11,2 +11,3 @@ import { PropertyValueType } from './object'; | ||
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; | ||
export declare function setValueWithNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(inputObject: ObjectGeneric, nestedKeys: KeysGeneric, value: NestedValue<ObjectGeneric, KeysGeneric>): void; | ||
export declare function getValueFromNestedKeys<const ObjectGeneric extends object, const KeysGeneric extends NestedSequentialKeys<ObjectGeneric>>(inputObject: ObjectGeneric, nestedKeys: KeysGeneric): NestedValue<ObjectGeneric, KeysGeneric> | undefined; |
{ | ||
"name": "@augment-vir/common", | ||
"version": "19.5.0", | ||
"version": "19.6.0", | ||
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common", | ||
@@ -5,0 +5,0 @@ "bugs": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
158753
3827
3