Skip to content

Commit

Permalink
fix(DataPacker): 兼容 v2 的 StructuredListTransformer 产生的数据
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 8, 2021
1 parent 8613fd4 commit d6cc8cb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion haoma-compile.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const removeIsTypePlugin = defineBabelPlugin(t => ({
t.isIdentifier(path.node.callee) &&
path.node.callee.name === 'isType'
) {
path.remove()
path.replaceWith(t.booleanLiteral(true))
}
},
},
Expand Down
22 changes: 22 additions & 0 deletions src/utils/DataPacker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ describe('DataPacker', () => {
},
})
})

test('兼容 v2 的 StructuredListTransformer 产生的数据', () => {
const data = {
__IS_PACKED_STRUCTURED_LIST__: true,
keys: ['id', 'name'],
values: [
['1111', 1],
['2222', 2],
],
signature: 'ZGLmZmL2ZQZmAQx3Av4kYwN',
}
expect(DataPacker.unpackIfNeeded(data as any)).toEqual([
{
id: 1,
name: '1111',
},
{
id: 2,
name: '2222',
},
])
})
})
40 changes: 23 additions & 17 deletions src/utils/DataPacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import { isType } from './isType'
import { rot13 } from './rot13'

export type RawObjectData = Record<any, any>
export type RawListData<
TRawObjectData extends RawObjectData = RawObjectData
> = TRawObjectData[]
export type RawListData<TRawObjectData extends RawObjectData = RawObjectData> =
TRawObjectData[]
export type RawData<TRawObjectData extends RawObjectData = RawObjectData> =
| TRawObjectData
| RawListData<TRawObjectData>
export type ElementOfRawData<
TRawData extends RawData
> = TRawData extends RawData<infer X> ? X : never
export type KeyOfRawData<
TRawData extends RawData
> = keyof ElementOfRawData<TRawData>
export type ValueOfRawData<
TRawData extends RawData
> = ElementOfRawData<TRawData>[KeyOfRawData<TRawData>]
export type ElementOfRawData<TRawData extends RawData> =
TRawData extends RawData<infer X> ? X : never
export type KeyOfRawData<TRawData extends RawData> =
keyof ElementOfRawData<TRawData>
export type ValueOfRawData<TRawData extends RawData> =
ElementOfRawData<TRawData>[KeyOfRawData<TRawData>]
export type PackedData<TRawData extends RawData> = {
readonly _k: Array<KeyOfRawData<TRawData>>
readonly _v: Array<Array<ValueOfRawData<TRawData>>>
Expand Down Expand Up @@ -105,12 +101,22 @@ export class DataPacker {
* @returns 返回结果数据
*/
static unpackIfNeeded(value: any, depth = 2): any {
if (isPlainObject(value) && isType<PackedData<RawObjectData>>(value)) {
if (value._k && value._v && value._s) {
return DataPacker.unpack(value)
if (isPlainObject(value)) {
// 兼容 v2 的 StructuredListTransformer 产生的数据
// https://github.com/fjc0k/vtils/blob/v2/packages/vtils/src/StructuredListTransformer.ts
if (value.__IS_PACKED_STRUCTURED_LIST__) {
value._k = value.keys
value._v = value.values
value._s = value.signature
delete value.__IS_PACKED_STRUCTURED_LIST__
}
if (depth > 0) {
return mapValues(value, v => DataPacker.unpackIfNeeded(v, depth - 1))
if (isType<PackedData<RawObjectData>>(value)) {
if (value._k && value._v && value._s) {
return DataPacker.unpack(value)
}
if (depth > 0) {
return mapValues(value, v => DataPacker.unpackIfNeeded(v, depth - 1))
}
}
}
return value
Expand Down

0 comments on commit d6cc8cb

Please sign in to comment.