Skip to content

Commit

Permalink
feat: add path2Array function (#113)
Browse files Browse the repository at this point in the history
* feat: add path2Array function

* chore: bump v3.3.7
  • Loading branch information
heiyexing authored Feb 27, 2024
1 parent cb8571a commit c499a30
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

将定义中的相对命令转换成绝对命令,例如:
Expand Down
68 changes: 68 additions & 0 deletions __tests__/unit/path/path-2-array.spec.ts
Original file line number Diff line number Diff line change
@@ -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'],
]);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/util",
"version": "3.3.6",
"version": "3.3.7",
"license": "MIT",
"sideEffects": false,
"main": "lib/index.js",
Expand Down
6 changes: 6 additions & 0 deletions src/path/convert/path-2-array.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/path/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit c499a30

Please sign in to comment.