Skip to content

Commit

Permalink
Speed up stringifyNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Nov 27, 2022
1 parent ae32acf commit 327d5b3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { removeLeadingZero } = require('./svgo/tools');

/**
* @typedef {import('./types').PathDataItem} PathDataItem
* @typedef {import('./types').PathDataCommand} PathDataCommand
Expand Down Expand Up @@ -249,7 +251,7 @@ const stringifyNumber = (number, precision) => {
number = Math.round(number * ratio) / ratio;
}
// remove zero whole from decimal number
return number.toString().replace(/^0\./, '.').replace(/^-0\./, '-.');
return removeLeadingZero(number);
};

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/svgo/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ exports.cleanupOutData = (data, params, command) => {
*/
const removeLeadingZero = (num) => {
var strNum = num.toString();

if (0 < num && num < 1 && strNum.charAt(0) === '0') {
strNum = strNum.slice(1);
return strNum.slice(1);
} else if (-1 < num && num < 0 && strNum.charAt(1) === '0') {
strNum = strNum.charAt(0) + strNum.slice(2);
return '-' + strNum.slice(2);
}
return strNum;
};
Expand Down

0 comments on commit 327d5b3

Please sign in to comment.