diff --git a/packages/neuron-wallet/src/models/chain/cell-dep.ts b/packages/neuron-wallet/src/models/chain/cell-dep.ts index f281246e20..03773180de 100644 --- a/packages/neuron-wallet/src/models/chain/cell-dep.ts +++ b/packages/neuron-wallet/src/models/chain/cell-dep.ts @@ -15,7 +15,7 @@ export default class CellDep { this.depType = depType } - public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType }): CellDep { + public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType | 'dep_group' }): CellDep { const _depType = snakeToCamel(depType) as DepType return new CellDep(OutPoint.fromObject(outPoint), _depType) } diff --git a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts index 007e4ddc1d..678bfa730e 100644 --- a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts +++ b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts @@ -1,29 +1,19 @@ -export function snakeToCamel(key: string) { - if (!key) return '' - - let keyArr = key.split('_') - for (let i = 0; i < keyArr.length; i++) { - if (i !== 0) { - keyArr[i] = keyArr[i][0].toUpperCase() + keyArr[i].substr(1) - } - } - return keyArr.join('') +export const snakeToCamel = (str: string): string => { + return str.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')) } -type Json = Record - -export function deepCamelizeKeys(param: Json): Json { - Object.keys(param).map(key => { - let item = param[key] - if (item instanceof Object) { - deepCamelizeKeys(item as Json) - } - if (snakeToCamel(key) !== key) { - param[snakeToCamel(key)] = param[key] - delete param[key] - } - }) - return param +export const deepCamelizeKeys = (item: unknown): unknown => { + if (Array.isArray(item)) { + return item.map((el: unknown) => deepCamelizeKeys(el)) + } else if (typeof item === 'function' || item !== Object(item)) { + return item + } + return Object.fromEntries( + Object.entries(item as Record).map(([key, value]: [string, unknown]) => [ + key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')), + deepCamelizeKeys(value), + ]) + ) } export default { deepCamelizeKeys, snakeToCamel }