Skip to content

Commit

Permalink
fix: use a simpler space regexp (#80)
Browse files Browse the repository at this point in the history
* fix: use a simpler space regexp

* fix: parse path to string correctly

Co-authored-by: yuqi.pyq <[email protected]>
  • Loading branch information
xiaoiver and xiaoiver authored May 22, 2022
1 parent 3baf8c2 commit 6ff61cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 53 deletions.
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.0.0",
"version": "3.0.2",
"license": "MIT",
"sideEffects": false,
"main": "lib/index.js",
Expand Down
103 changes: 51 additions & 52 deletions src/path/parse-path-string.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
const SPACES =
'\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029';
const SPACES = '\\s';
const PATH_COMMAND = new RegExp(
'([a-z])[' + SPACES + ',]*((-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?[' + SPACES + ']*,?[' + SPACES + ']*)+)',
'ig',
);
const PATH_VALUES = new RegExp('(-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?)[' + SPACES + ']*,?[' + SPACES + ']*', 'ig');

// Parses given path string into an array of arrays of path segments
export function parsePathString(pathString: string) {
if (!pathString) {
return null;
}

if (Array.isArray(pathString)) {
return pathString;
}
const paramCounts = {
a: 7,
c: 6,
o: 2,
h: 1,
l: 2,
m: 2,
r: 4,
q: 4,
s: 4,
t: 2,
v: 1,
u: 3,
z: 0,
};
const data = [];
export function parsePathString(pathString: string | any[]) {
if (typeof pathString === 'string') {
if (pathString) {
const paramCounts = {
a: 7,
c: 6,
o: 2,
h: 1,
l: 2,
m: 2,
r: 4,
q: 4,
s: 4,
t: 2,
v: 1,
u: 3,
z: 0,
};
const data = [];

String(pathString).replace(PATH_COMMAND, function (a, b, c) {
const params = [];
let name = b.toLowerCase();
c.replace(PATH_VALUES, function (a, b) {
b && params.push(+b);
});
if (name === 'm' && params.length > 2) {
data.push([b].concat(params.splice(0, 2)));
name = 'l';
b = b === 'm' ? 'l' : 'L';
}
if (name === 'o' && params.length === 1) {
data.push([b, params[0]]);
}
if (name === 'r') {
data.push([b].concat(params));
} else {
while (params.length >= paramCounts[name]) {
data.push([b].concat(params.splice(0, paramCounts[name])));
if (!paramCounts[name]) {
break;
pathString.replace(PATH_COMMAND, function (a, b, c) {
const params = [];
let name = b.toLowerCase();
c.replace(PATH_VALUES, function (a, b) {
b && params.push(+b);
});
if (name === 'm' && params.length > 2) {
data.push([b].concat(params.splice(0, 2)));
name = 'l';
b = b === 'm' ? 'l' : 'L';
}
}
if (name === 'o' && params.length === 1) {
data.push([b, params[0]]);
}
if (name === 'r') {
data.push([b].concat(params));
} else {
while (params.length >= paramCounts[name]) {
data.push([b].concat(params.splice(0, paramCounts[name])));
if (!paramCounts[name]) {
break;
}
}
}
return '';
});
return data;
} else {
return null;
}
return '';
});

return data;
} else if (Array.isArray(pathString)) {
return pathString;
}
}

0 comments on commit 6ff61cc

Please sign in to comment.