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 f469309
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 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
8 changes: 4 additions & 4 deletions lib/svgo/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,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);
} else if (-1 < num && num < 0 && strNum.charAt(1) === '0') {
strNum = strNum.charAt(0) + strNum.slice(2);
if (0 < num && num < 1) {
return strNum.slice(1);
} else if (-1 < num && num < 0) {
return '-' + strNum.slice(2);
}
return strNum;
};
Expand Down

0 comments on commit f469309

Please sign in to comment.