docs(ComponentCode): handle objects

This commit is contained in:
Benjamin Canac
2024-07-17 15:26:05 +02:00
parent 11b4dfefa7
commit dd532bb110
2 changed files with 68 additions and 23 deletions

View File

@@ -40,6 +40,21 @@ export function get(object: Record<string, any>, path: (string | number)[] | str
return result !== undefined ? result : defaultValue
}
export function set(object: Record<string, any>, path: (string | number)[] | string, value: any): void {
if (typeof path === 'string') {
path = path.split('.').map((key) => {
const numKey = Number(key)
return Number.isNaN(numKey) ? key : numKey
})
}
path.reduce((acc, key, i) => {
if (acc[key] === undefined) acc[key] = {}
if (i === path.length - 1) acc[key] = value
return acc[key]
}, object)
}
export function looseToNumber(val: any): any {
const n = Number.parseFloat(val)
return Number.isNaN(n) ? val : n