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 cd987f3
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 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

Check failure on line 6 in lib/path.js

View workflow job for this annotation

GitHub Actions / Lint

Duplicate identifier '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 Down Expand Up @@ -133,7 +130,8 @@ const readNumber = (string, cursor) => {
};

/**
* @type {(string: string) => PathDataItem[]}
* @param {string} string
* @returns {PathDataItem[]}
*/
export const parsePathData = (string) => {
/**
Expand All @@ -150,7 +148,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 +168,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 cd987f3

Please sign in to comment.