Skip to content

Commit

Permalink
fix(keys): improve perf by avoiding excessive array allocations (#25)
Browse files Browse the repository at this point in the history
* fix(keys): improve perf by avoiding excessive array allocations

* fix: failed test
  • Loading branch information
aleclarson authored Jun 26, 2024
1 parent eca358b commit f0e06ba
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/object/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ import { isArray, isPlainObject } from 'radashi'
* keys({ name: 'ra' }) // ['name']
* keys({ name: 'ra', children: [{ name: 'hathor' }] }) // ['name', 'children.0.name']
*/
export const keys = <TValue extends object>(value: TValue): string[] => {
export const keys = (value: object): string[] => {
if (!value) return []
const getKeys = (nested: any, paths: string[]): string[] => {
if (isPlainObject(nested)) {
return Object.entries(nested).flatMap(([k, v]) =>
getKeys(v, [...paths, k])
)
const keys: string[] = []
const keyPath: (string | number)[] = []
const recurse = (value: any) => {
if (isPlainObject(value)) {
for (const [prop, propValue] of Object.entries(value)) {
keyPath.push(prop)
recurse(propValue)
keyPath.pop()
}
} else if (isArray(value)) {
value.forEach((item, index) => {
keyPath.push(index)
recurse(item)
keyPath.pop()
})
} else {
keys.push(keyPath.join('.'))
}
if (isArray(nested)) {
return nested.flatMap((item, i) => getKeys(item, [...paths, `${i}`]))
}
return [paths.join('.')]
}
return getKeys(value, [])
recurse(value)
return keys
}

0 comments on commit f0e06ba

Please sign in to comment.