Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
21 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
// > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |