-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Move utils to modules for better minification
- Loading branch information
1 parent
3524764
commit 43e846a
Showing
15 changed files
with
155 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const EPSILON = Number.EPSILON | ||
|
||
export const DEFAULT_PRECISION = Math.floor(Math.abs(Math.log10(EPSILON))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DEFAULT_PRECISION } from "./constants.js" | ||
import toPrecision from "./to-precision.js" | ||
|
||
/** | ||
* Counts the number of decimal places specified by a number | ||
* @private | ||
* @param {number} value | ||
* @returns {number} decimals places | ||
*/ | ||
export default (n, precision = DEFAULT_PRECISION) => { | ||
let count = 0 | ||
n = Math.abs(n) | ||
|
||
while (toPrecision(n % 1, precision) > 0 && count < precision) { | ||
count++ | ||
n *= 10 | ||
} | ||
|
||
return count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Calculate the magnitude of a number (power of 10) | ||
* @private | ||
* @param {number} n | ||
* @returns {number} | ||
*/ | ||
export default (n) => Math.floor(Math.log10(Math.abs(n))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Convert a value between 0 and 1 to a percentage string. The suffix is ignored | ||
* for zero. The precision is limited to reduce the size of the rendered SVG. | ||
* @private | ||
* @param {number} value | ||
* @param {string} [units] | ||
* @returns {string} percent | ||
*/ | ||
export default (value, units = "%") => | ||
value !== 0 ? +(value * 100).toFixed(2) + units : "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DEFAULT_PRECISION } from "./constants.js" | ||
|
||
/** | ||
* Add the values in an array, reading `item.value` where present. | ||
* @private | ||
* @param {Array} array | ||
* @returns {number} total | ||
*/ | ||
export default (array, precision = DEFAULT_PRECISION) => { | ||
const f = Math.pow(10, precision) | ||
return array.reduce( | ||
(m, v = 0) => (f * m + f * ((v && v.value) || +v || 0)) / f, | ||
0 | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { DEFAULT_PRECISION } from "./constants.js" | ||
|
||
/** | ||
* Convert a value to a specified precision to avoid floating point errors | ||
* @private | ||
* @param {number} value | ||
* @param {number} [precision] | ||
* @returns {number} value | ||
*/ | ||
export default (value, precision = DEFAULT_PRECISION) => { | ||
const f = Math.pow(10, precision) | ||
const n = precision < 0 ? value : 0.01 / f + value | ||
return Math.round(n * f) / f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.