diff --git a/packages/vmind/__tests__/unit/vchartSpec/util.test.ts b/packages/vmind/__tests__/unit/vchartSpec/util.test.ts index 6b409b1..643a4ec 100644 --- a/packages/vmind/__tests__/unit/vchartSpec/util.test.ts +++ b/packages/vmind/__tests__/unit/vchartSpec/util.test.ts @@ -1,4 +1,4 @@ -import { checkDuplicatedKey, convertFunctionString } from '../../../src/atom/VChartSpec/utils'; +import { checkDuplicatedKey, convertFunctionString, removeUnneedArrayInSpec } from '../../../src/atom/VChartSpec/utils'; describe('checkDuplicatedKey', () => { it('should return null when not match', () => { @@ -95,3 +95,47 @@ describe('convertFunctionString', () => { expect(obj1.a).toEqual(spec.a); }); }); + +describe('removeUnneedArrayInSpec', () => { + it('should remove unneed array in spec', () => { + expect( + removeUnneedArrayInSpec( + { + legends: [{ orient: 'left' }] + }, + 'legends', + 'legends' + ) + ).toEqual({ orient: 'left' }); + + expect( + removeUnneedArrayInSpec( + { + legends: [{ orient: 'left' }] + }, + 'legends', + 'legends[0]' + ) + ).toEqual({ orient: 'left' }); + + expect( + removeUnneedArrayInSpec( + { + 'legends[0]': { orient: 'left' } + }, + 'legends', + 'legends[0]' + ) + ).toEqual({ orient: 'left' }); + + expect( + removeUnneedArrayInSpec( + { + 'legends[0]': { orient: 'left' } + }, + 'legends', + 'legends' + ) + ).toEqual({ orient: 'left' }); + }); +}); diff --git a/packages/vmind/src/atom/VChartSpec/utils.ts b/packages/vmind/src/atom/VChartSpec/utils.ts index 74f4f8d..f18d381 100644 --- a/packages/vmind/src/atom/VChartSpec/utils.ts +++ b/packages/vmind/src/atom/VChartSpec/utils.ts @@ -313,16 +313,6 @@ export const removeUnneedArrayInSpec = (leafSpec: any, compKey: string, parentKe : leafSpec; }; -export const addArrayInSpec = (leafSpec: any, compKey: string, parentKeyPath: string) => { - return leafSpec[compKey] - ? isArray(leafSpec[compKey]) - ? leafSpec[compKey][0] - : leafSpec[compKey] - : parentKeyPath in leafSpec - ? leafSpec[parentKeyPath] - : leafSpec; -}; - const ALIAS_NAME_KEY = '_alias_name'; export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, chartSpec: any, leafSpec: any) => { @@ -341,7 +331,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch } return { parentKeyPath: subPaths.join('.'), - leafSpec: addArrayInSpec(leafSpec, compKey, parentKeyPath) + leafSpec: removeUnneedArrayInSpec(leafSpec, compKey, parentKeyPath) }; } @@ -359,7 +349,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch } return { parentKeyPath: subPaths.join('.'), - leafSpec: addArrayInSpec(leafSpec, compKey, parentKeyPath) + leafSpec: removeUnneedArrayInSpec(leafSpec, compKey, parentKeyPath) }; }