Skip to content

Commit

Permalink
fix: handle de-dupe key better
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 7, 2022
1 parent 7d92a81 commit 83e6918
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/pack/array.ts
@@ -1,18 +1,19 @@
import type { PackArrayOptions } from '../types'
import { packObject } from './object'
import { InternalKeySymbol, packObject } from './object'

export function packArray<T extends Record<string, any>[]>(input: T, options?: PackArrayOptions<T>): Partial<Record<string, any>> {
const packed: Partial<Record<string, any>> = {}
for (const i of input) {
const packedObj = packObject(i, options)
const pKey = Object.keys(packedObj)[0] as keyof typeof packed
const isDedupeKey = pKey.startsWith(InternalKeySymbol)

if (!pKey.startsWith('key-') && packed[pKey]) {
if (!isDedupeKey && packed[pKey]) {
packed[pKey] = Array.isArray(packed[pKey]) ? packed[pKey] : [packed[pKey]]
packed[pKey].push(Object.values(packedObj)[0])
}
else {
packed[pKey.startsWith('key-') ? (pKey.split('-').pop() || pKey) : pKey] = packedObj[pKey]
packed[isDedupeKey ? (pKey.split('-').slice(1).join('-') || pKey) : pKey] = packedObj[pKey]
}
}
return packed
Expand Down

0 comments on commit 83e6918

Please sign in to comment.