Skip to content

Commit

Permalink
refactor: dont covert to charcode in iswhitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Jun 16, 2024
1 parent abb23bf commit f790ae2
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { removeLeadingZero, toFixed } from './svgo/tools.js';
/**
* @typedef {import('./types.js').PathDataItem} PathDataItem
* @typedef {import('./types.js').PathDataCommand} PathDataCommand
* @typedef {'none' | 'sign' | 'whole' | 'decimal_point' | 'decimal' | 'e' | 'exponent_sign' | 'exponent'} ReadNumberState
*/

// Based on https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
Expand Down Expand Up @@ -38,20 +39,16 @@ const isCommand = (c) => {
};

/**
* @type {(c: string) => boolean}
* @param {string} c
* @returns {boolean}
*/
const isWsp = (c) => {
const codePoint = c.codePointAt(0);
return (
codePoint === 0x20 ||
codePoint === 0x9 ||
codePoint === 0xd ||
codePoint === 0xa
);
const isWhiteSpace = (c) => {
return c === ' ' || c === '\t' || c === '\r' || c === '\n';
};

/**
* @type {(c: string) => boolean}
* @param {string} c
* @returns {boolean}
*/
const isDigit = (c) => {
const codePoint = c.codePointAt(0);
Expand All @@ -61,10 +58,6 @@ const isDigit = (c) => {
return 48 <= codePoint && codePoint <= 57;
};

/**
* @typedef {'none' | 'sign' | 'whole' | 'decimal_point' | 'decimal' | 'e' | 'exponent_sign' | 'exponent'} ReadNumberState
*/

/**
* @type {(string: string, cursor: number) => [number, ?number]}
*/
Expand Down Expand Up @@ -133,7 +126,8 @@ const readNumber = (string, cursor) => {
};

/**
* @type {(string: string) => PathDataItem[]}
* @param {string} string
* @returns {PathDataItem[]}
*/
export const parsePathData = (string) => {
/**
Expand All @@ -150,7 +144,7 @@ export const parsePathData = (string) => {
let hadComma = false;
for (let i = 0; i < string.length; i += 1) {
const c = string.charAt(i);
if (isWsp(c)) {
if (isWhiteSpace(c)) {
continue;
}
// allow comma only between arguments
Expand All @@ -170,11 +164,9 @@ export const parsePathData = (string) => {
if (c !== 'M' && c !== 'm') {
return pathData;
}
} else {
} else if (args.length !== 0) {
// stop if previous command arguments are not flushed
if (args.length !== 0) {
return pathData;
}
return pathData;
}
command = c;
args = [];
Expand Down

0 comments on commit f790ae2

Please sign in to comment.