Skip to content

Commit

Permalink
fix: allow unpack to unpack deeply
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 7, 2022
1 parent 2f37c5e commit 1afd958
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
35 changes: 8 additions & 27 deletions src/types.ts
@@ -1,31 +1,12 @@
export interface PackArrayOptions<T extends Record<string, any>[]> {
key: keyof T[number] | (keyof T[number])[]
value: keyof T[number] | (keyof T[number])[]
type Arrayable<T> = T | T[]

export interface PackArrayOptions<T extends Record<string, any>[]> extends PackOptions<T[number]> {
key?: Arrayable<keyof T[number] | string>
value?: Arrayable<keyof T[number] | string>
}

export interface PackOptions<T extends Record<keyof T, any>> {
key: keyof T | (keyof T)[]
value: keyof T | (keyof T)[]
key?: Arrayable<keyof T | string>
value?: Arrayable<keyof T | string>
resolveKey?: (key: keyof T) => string
}

// export type ReverseMap<T extends Record<keyof T, keyof any>> = {
// [P in T[keyof T]]: {
// [K in keyof T]: T[K] extends P ? K : never
// }[keyof T]
// }
// type Prepend<T, U extends any[]> = [T, ...U]
// type Keys<T extends Record<string, any>> = Keys_<T, []>
// type Keys_<T extends Record<string, any>, U extends PropertyKey[]> =
// {
// [P in keyof T]: {} extends Omit<T, P> ? [P] : Prepend<P, Keys_<Omit<T, P>, U>>
// }[keyof T]
// export interface ObjectKeys<T extends Record<string, any>> {
// key: Keys<ReverseMap<T>>[0]
// value: Keys<ReverseMap<T>>[1]
// }
// export type GetObjectKeys<Data extends Record<string, any>> = Keys<ReverseMap<Pick<Data, ''>>>[0]
// export type FirstObjectKeyFiltered<Data extends Record<string, any>, Filter extends keyof Data> = Keys<ReverseMap<Pick<Data, Filter>>>[0]
// export type ObjectKeyValue<Data extends Record<string, any>, K extends keyof Data, V extends keyof Data> = Record<
// FirstObjectKeyFiltered<Data, K>,
// FirstObjectKeyFiltered<Data, V>
// >
19 changes: 13 additions & 6 deletions src/unpack/array.ts
@@ -1,23 +1,30 @@
export interface UnpackArrayOptions {
key: string
value: string
key: string | ((key: string) => string)
value: string | ((key: string) => string)
resolveKeyData?: (key: string) => string
resolveValueData?: (value: unknown) => unknown
}

export function unpackToArray(input: Record<string, any>, options: UnpackArrayOptions): Record<string, any>[] {
const unpacked: any[] = []
options = options || { key: 'name', value: 'content' }
const kFn = options.resolveKeyData || ((k: string) => k)
const vFn = options.resolveValueData || ((k: string) => k)

for (const [k, v] of Object.entries(input)) {
unpacked.push(...(Array.isArray(v) ? v : [v]).map((i) => {
// handle nested objects
if (typeof i === 'object')
i = unpackToArray(i!, options)
const val = vFn(i)

if (Array.isArray(val))
return val

return {
[options.key]: kFn(k),
[options.value]: vFn(i),
[typeof options.key === 'function' ? options.key(k) : options.key]: kFn(k),
[typeof options.value === 'function' ? options.value(k) : options.value]: val,
}
}))
}).flat())
}
return unpacked
}

0 comments on commit 1afd958

Please sign in to comment.