diff --git a/README.md b/README.md index 11a6fdb..7264275 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,21 @@ const str: PathArray = [ expect(path2String(str)).toEqual('M10 10L100 100l10 10h20v20'); ``` +### path2Array + +将 PathArray 转换成数组,不会对原始定义中的命令进行修改: + +```js +const str = 'M10 10L100 100l10 10h20v20'; +expect(path2Array(str)).toEqual([ + ['M', 10, 10], + ['L', 100, 100], + ['l', 10, 10], + ['h', 20], + ['v', 20], +]); +``` + ### path2Absolute 将定义中的相对命令转换成绝对命令,例如: diff --git a/__tests__/unit/path/path-2-array.spec.ts b/__tests__/unit/path/path-2-array.spec.ts new file mode 100644 index 0000000..d840b6c --- /dev/null +++ b/__tests__/unit/path/path-2-array.spec.ts @@ -0,0 +1,68 @@ +import { path2Array, PathArray } from '../../../src'; + +describe('string to path', () => { + it('should transform path to array correctly.', () => { + const str = 'M10 10L100 100l10 10h20v20'; + + expect(path2Array(str)).toEqual([ + ['M', 10, 10], + ['L', 100, 100], + ['l', 10, 10], + ['h', 20], + ['v', 20], + ]); + + function getCirclePath(cx: number, cy: number, rx: number, ry: number): PathArray { + return [ + // ['M', cx, cy - ry], + // ['A', rx, ry, 0, 1, 1, cx, cy + ry], + // ['A', rx, ry, 0, 1, 1, cx, cy - ry], + ['M', cx - rx, ry], + ['A', rx, ry, 0, 1, 0, cx + rx, ry], + ['A', rx, ry, 0, 1, 0, cx - rx, ry], + ['Z'], + ]; + } + + expect(path2Array('M-100 100A100 100 0 1 0 100 100A100 100 0 1 0 -100 100Z')).toEqual( + getCirclePath(0, 0, 100, 100), + ); + + expect( + path2Array( + 'M2 4A2 2 0 0 0 0 6V12A2 2 0 0 0 2 14H14A2 2 0 0 0 16 12V6A2 2 0 0 0 14 4H12.828A2 2 0 0 1 11.414 3.414L10.586 2.586A2 2 0 0 0 9.172 2H6.828A2 2 0 0 0 5.414 2.586L4.586 3.414A2 2 0 0 1 3.172 4H2ZM10.5 8.5A2.5 2.5 0 0 0 5.5 8.5A2.5 2.5 0 1 0 10.5 8.5ZM2.5 6A0.5 0.5 0 0 1 2.5 5A0.5 0.5 0 1 1 2.5 6ZM11.5 8.5A3.5 3.5 0 1 1 4.5 8.5A3.5 3.5 0 0 1 11.5 8.5Z', + ), + ).toEqual([ + ['M', 2, 4], + ['A', 2, 2, 0, 0, 0, 0, 6], + ['V', 12], + ['A', 2, 2, 0, 0, 0, 2, 14], + ['H', 14], + ['A', 2, 2, 0, 0, 0, 16, 12], + ['V', 6], + ['A', 2, 2, 0, 0, 0, 14, 4], + ['H', 12.828], + ['A', 2, 2, 0, 0, 1, 11.414, 3.414], + ['L', 10.586, 2.586], + ['A', 2, 2, 0, 0, 0, 9.172, 2], + ['H', 6.828], + ['A', 2, 2, 0, 0, 0, 5.414, 2.586], + ['L', 4.586, 3.414], + ['A', 2, 2, 0, 0, 1, 3.172, 4], + ['H', 2], + ['Z'], + ['M', 10.5, 8.5], + ['A', 2.5, 2.5, 0, 0, 0, 5.5, 8.5], + ['A', 2.5, 2.5, 0, 1, 0, 10.5, 8.5], + ['Z'], + ['M', 2.5, 6], + ['A', 0.5, 0.5, 0, 0, 1, 2.5, 5], + ['A', 0.5, 0.5, 0, 1, 1, 2.5, 6], + ['Z'], + ['M', 11.5, 8.5], + ['A', 3.5, 3.5, 0, 1, 1, 4.5, 8.5], + ['A', 3.5, 3.5, 0, 0, 1, 11.5, 8.5], + ['Z'], + ]); + }); +}); diff --git a/package.json b/package.json index 678523b..8cb690c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/util", - "version": "3.3.6", + "version": "3.3.7", "license": "MIT", "sideEffects": false, "main": "lib/index.js", diff --git a/src/path/convert/path-2-array.ts b/src/path/convert/path-2-array.ts new file mode 100644 index 0000000..4430627 --- /dev/null +++ b/src/path/convert/path-2-array.ts @@ -0,0 +1,6 @@ +import { parsePathString } from '../parser/parse-path-string'; +import type { PathArray } from '../types'; + +export function path2Array(pathInput: string): PathArray { + return parsePathString(pathInput) as PathArray; +} diff --git a/src/path/index.ts b/src/path/index.ts index 7c8d8c8..b6e1abf 100644 --- a/src/path/index.ts +++ b/src/path/index.ts @@ -1,6 +1,7 @@ export { path2String } from './convert/path-2-string'; export { path2Curve } from './convert/path-2-curve'; export { path2Absolute } from './convert/path-2-absolute'; +export { path2Array } from './convert/path-2-array'; export { clonePath } from './process/clone-path'; export { normalizePath } from './process/normalize-path'; export { reverseCurve } from './process/reverse-curve';