@jsenv/log
Advanced tools
Comparing version 1.6.0 to 1.6.1
@@ -17,2 +17,8 @@ /* | ||
export { roundNumber, setPrecision, getPrecision } from "./src/decimals.js" | ||
export { | ||
getPrecision, | ||
setRoundedPrecision, | ||
setFlooredPrecision, | ||
setCeiledPrecision, | ||
setPrecision, | ||
} from "./src/decimals.js" |
{ | ||
"name": "@jsenv/log", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Nice and dynamic logs in the terminal", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,17 +0,1 @@ | ||
// https://www.codingem.com/javascript-how-to-limit-decimal-places/ | ||
export const roundNumber = (number, maxDecimals) => { | ||
const decimalsExp = Math.pow(10, maxDecimals) | ||
const numberRoundInt = Math.round(decimalsExp * (number + Number.EPSILON)) | ||
const numberRoundFloat = numberRoundInt / decimalsExp | ||
return numberRoundFloat | ||
} | ||
export const setPrecision = (number, precision) => { | ||
if (Math.floor(number) === number) return number | ||
const [int, decimals] = number.toString().split(".") | ||
if (precision <= 0) return int | ||
const numberTruncated = `${int}.${decimals.slice(0, precision)}` | ||
return numberTruncated | ||
} | ||
export const getPrecision = (number) => { | ||
@@ -22,1 +6,56 @@ if (Math.floor(number) === number) return 0 | ||
} | ||
export const setRoundedPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: Math.round }) | ||
} | ||
export const setFlooredPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: Math.floor }) | ||
} | ||
export const setCeiledPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: Math.ceil }) | ||
} | ||
export const setPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: parseInt }) | ||
} | ||
const setDecimalsPrecision = (number, { transform, decimals = 1 } = {}) => { | ||
if (number === 0) { | ||
return 0 | ||
} | ||
let numberCandidate = Math.abs(number) | ||
if (numberCandidate < 1) { | ||
const integerGoal = Math.pow(10, decimals - 1) | ||
let i = 1 | ||
while (numberCandidate < integerGoal) { | ||
numberCandidate *= 10 | ||
i *= 10 | ||
} | ||
const asInteger = transform(numberCandidate) | ||
const asFloat = asInteger / i | ||
return number < 0 ? -asFloat : asFloat | ||
} | ||
const coef = Math.pow(10, decimals) | ||
const numberMultiplied = (number + Number.EPSILON) * coef | ||
const asInteger = transform(numberMultiplied) | ||
const asFloat = asInteger / coef | ||
return number < 0 ? -asFloat : asFloat | ||
} | ||
// https://www.codingem.com/javascript-how-to-limit-decimal-places/ | ||
// export const roundNumber = (number, maxDecimals) => { | ||
// const decimalsExp = Math.pow(10, maxDecimals) | ||
// const numberRoundInt = Math.round(decimalsExp * (number + Number.EPSILON)) | ||
// const numberRoundFloat = numberRoundInt / decimalsExp | ||
// return numberRoundFloat | ||
// } | ||
// export const setPrecision = (number, precision) => { | ||
// if (Math.floor(number) === number) return number | ||
// const [int, decimals] = number.toString().split(".") | ||
// if (precision <= 0) return int | ||
// const numberTruncated = `${int}.${decimals.slice(0, precision)}` | ||
// return numberTruncated | ||
// } |
@@ -1,2 +0,2 @@ | ||
import { roundNumber } from "./decimals.js" | ||
import { setPrecision } from "./decimals.js" | ||
@@ -40,3 +40,3 @@ export const msAsDuration = (ms) => { | ||
const formatUnit = (unit, maxDecimals = 0) => { | ||
const count = roundNumber(unit.count, maxDecimals) | ||
const count = setPrecision(unit.count, maxDecimals) | ||
if (count <= 1) { | ||
@@ -43,0 +43,0 @@ return `${count} ${unit.name}` |
@@ -1,2 +0,2 @@ | ||
import { roundNumber, getPrecision, setPrecision } from "./decimals.js" | ||
import { getPrecision, setRoundedPrecision } from "./decimals.js" | ||
@@ -18,31 +18,33 @@ export const distributePercentages = ( | ||
const ratios = numbers.map((number) => number / total) | ||
const lowestValue = 1 / Math.pow(10, maxPrecisionHint) | ||
let biggestPrecisionRequired = 0 | ||
for (const ratio of ratios) { | ||
if (ratio < lowestValue) { | ||
const precision = getPrecision(ratio) | ||
if (precision > biggestPrecisionRequired) { | ||
biggestPrecisionRequired = precision | ||
const percentages = {} | ||
ratios.pop() | ||
ratios.forEach((ratio, index) => { | ||
const percentage = ratio * 100 | ||
percentages[numberNames[index]] = percentage | ||
}) | ||
const lowestPercentage = (1 / Math.pow(10, maxPrecisionHint)) * 100 | ||
let precision = 0 | ||
Object.keys(percentages).forEach((name) => { | ||
const percentage = percentages[name] | ||
if (percentage < lowestPercentage) { | ||
// check the amout of meaningful decimals | ||
// and that what we will use | ||
const percentageRounded = setRoundedPrecision(percentage) | ||
const percentagePrecision = getPrecision(percentageRounded) | ||
if (percentagePrecision > precision) { | ||
precision = percentagePrecision | ||
} | ||
} | ||
} | ||
}) | ||
let remainingPercentage = 100 | ||
const toPercentage = (ratio) => { | ||
if (biggestPrecisionRequired > maxPrecisionHint) { | ||
return `${setPrecision(ratio * 100, biggestPrecisionRequired - 2)} %` | ||
} | ||
const ratioRounded = roundNumber(ratio, maxPrecisionHint) | ||
return `${setPrecision(ratioRounded * 100, biggestPrecisionRequired - 2)} %` | ||
} | ||
let remainingRatio = 1 | ||
const distribution = {} | ||
ratios.pop() | ||
ratios.forEach((ratio, index) => { | ||
remainingRatio -= ratio | ||
distribution[numberNames[index]] = toPercentage(ratio) | ||
Object.keys(percentages).forEach((name) => { | ||
const percentage = percentages[name] | ||
const percentageAllocated = setRoundedPrecision(percentage, precision) | ||
remainingPercentage -= percentageAllocated | ||
percentages[name] = percentageAllocated | ||
}) | ||
distribution[numberNames[numberNames.length - 1]] = | ||
toPercentage(remainingRatio) | ||
return distribution | ||
const lastName = numberNames[numberNames.length - 1] | ||
percentages[lastName] = setRoundedPrecision(remainingPercentage, precision) | ||
return percentages | ||
} |
@@ -6,3 +6,3 @@ export const startSpinner = ({ | ||
keepProcessAlive = false, | ||
stopOnWriteFromOutside = false, | ||
stopOnWriteFromOutside = true, | ||
text = "", | ||
@@ -9,0 +9,0 @@ effect = () => {}, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
16991
518
0