@jsenv/log
Advanced tools
Comparing version 1.6.1 to 1.6.2
@@ -13,3 +13,3 @@ /* | ||
export { createTaskLog } from "./src/task_log.js" | ||
export { msAsDuration } from "./src/duration_log.js" | ||
export { msAsEllapsedTime, msAsDuration } from "./src/duration_log.js" | ||
export { byteAsFileSize, byteAsMemoryUsage } from "./src/size_log.js" | ||
@@ -16,0 +16,0 @@ export { distributePercentages } from "./src/percentage_distribution.js" |
{ | ||
"name": "@jsenv/log", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "Nice and dynamic logs in the terminal", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -7,19 +7,54 @@ export const getPrecision = (number) => { | ||
export const setRoundedPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: Math.round }) | ||
export const setRoundedPrecision = ( | ||
number, | ||
{ decimals = 1, decimalsWhenSmall = decimals } = {}, | ||
) => { | ||
return setDecimalsPrecision(number, { | ||
decimals, | ||
decimalsWhenSmall, | ||
transform: Math.round, | ||
}) | ||
} | ||
export const setFlooredPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: Math.floor }) | ||
export const setFlooredPrecision = ( | ||
number, | ||
{ decimals = 1, decimalsWhenSmall = decimals } = {}, | ||
) => { | ||
return setDecimalsPrecision(number, { | ||
decimals, | ||
decimalsWhenSmall, | ||
transform: Math.floor, | ||
}) | ||
} | ||
export const setCeiledPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: Math.ceil }) | ||
export const setCeiledPrecision = ( | ||
number, | ||
{ decimals = 1, decimalsWhenSmall = decimals } = {}, | ||
) => { | ||
return setDecimalsPrecision(number, { | ||
decimals, | ||
decimalsWhenSmall, | ||
transform: Math.ceil, | ||
}) | ||
} | ||
export const setPrecision = (number, decimals) => { | ||
return setDecimalsPrecision(number, { decimals, transform: parseInt }) | ||
export const setPrecision = ( | ||
number, | ||
{ decimals = 1, decimalsWhenSmall = decimals } = {}, | ||
) => { | ||
return setDecimalsPrecision(number, { | ||
decimals, | ||
decimalsWhenSmall, | ||
transform: parseInt, | ||
}) | ||
} | ||
const setDecimalsPrecision = (number, { transform, decimals = 1 } = {}) => { | ||
const setDecimalsPrecision = ( | ||
number, | ||
{ | ||
transform, | ||
decimals, // max decimals for number in [-Infinity, -1[]1, Infinity] | ||
decimalsWhenSmall, // max decimals for number in [-1,1] | ||
} = {}, | ||
) => { | ||
if (number === 0) { | ||
@@ -30,3 +65,3 @@ return 0 | ||
if (numberCandidate < 1) { | ||
const integerGoal = Math.pow(10, decimals - 1) | ||
const integerGoal = Math.pow(10, decimalsWhenSmall - 1) | ||
let i = 1 | ||
@@ -33,0 +68,0 @@ while (numberCandidate < integerGoal) { |
@@ -1,10 +0,5 @@ | ||
import { setPrecision } from "./decimals.js" | ||
import { setRoundedPrecision } from "./decimals.js" | ||
export const msAsDuration = (ms) => { | ||
if (ms < 1) { | ||
// it would be barely readable to write 0.0001 second (stands for 0.1 millisecond) | ||
// and this precision does not matter | ||
// (this function is meant to display a duration to a human) | ||
// so in this case we'll return "0 second" which means "less than 1 millisecond" | ||
// (I prefer "0 second" to be consistent with other logs wich will likely measure in "second") | ||
export const msAsEllapsedTime = (ms) => { | ||
if (ms < 1000) { | ||
return "0 second" | ||
@@ -14,29 +9,42 @@ } | ||
if (!remaining) { | ||
return formatUnit(primary, determineMaxDecimals(primary)) | ||
return formatEllapsedUnit(primary) | ||
} | ||
return `${formatUnit(primary)} and ${formatUnit(remaining)}` | ||
return `${formatEllapsedUnit(primary)} and ${formatEllapsedUnit(remaining)}` | ||
} | ||
const determineMaxDecimals = (unit) => { | ||
if (unit.name !== "second") { | ||
return 0 | ||
const formatEllapsedUnit = (unit) => { | ||
const count = | ||
unit.name === "second" ? Math.floor(unit.count) : Math.round(unit.count) | ||
if (count <= 1) { | ||
return `${count} ${unit.name}` | ||
} | ||
const count = unit.count | ||
if (count < 0.001) { | ||
return 4 | ||
return `${count} ${unit.name}s` | ||
} | ||
export const msAsDuration = (ms) => { | ||
// ignore ms below meaningfulMs so that: | ||
// msAsDuration(0.5) -> "0 second" | ||
// msAsDuration(1.1) -> "0.001 second" (and not "0.0011 second") | ||
// This tool is meant to be read by humans and it would be barely readable to see | ||
// "0.0001 second" (stands for 0.1 millisecond) | ||
// yes we could return "0.1 millisecond" but we choosed consistency over precision | ||
// so that the prefered unit is "second" (and does not become millisecond when ms is super small) | ||
if (ms < 1) { | ||
return "0 second" | ||
} | ||
if (count < 0.01) { | ||
return 3 | ||
const { primary, remaining } = parseMs(ms) | ||
if (!remaining) { | ||
return formatDurationUnit(primary, primary.name === "second" ? 1 : 0) | ||
} | ||
if (count < 0.1) { | ||
return 2 | ||
} | ||
if (count < 1) { | ||
return 1 | ||
} | ||
return 1 | ||
return `${formatDurationUnit(primary, 0)} and ${formatDurationUnit( | ||
remaining, | ||
0, | ||
)}` | ||
} | ||
const formatUnit = (unit, maxDecimals = 0) => { | ||
const count = setPrecision(unit.count, maxDecimals) | ||
const formatDurationUnit = (unit, decimals) => { | ||
const count = setRoundedPrecision(unit.count, { | ||
decimals, | ||
}) | ||
if (count <= 1) { | ||
@@ -43,0 +51,0 @@ return `${count} ${unit.name}` |
@@ -11,4 +11,11 @@ import { require } from "@jsenv/utils/require.js" | ||
export const byteAsMemoryUsage = (metricValue) => { | ||
return bytes(metricValue, { decimalPlaces: 2, unitSeparator: " " }) | ||
export const byteAsMemoryUsage = ( | ||
metricValue, | ||
{ fixedDecimals = true } = {}, | ||
) => { | ||
return bytes(metricValue, { | ||
decimalPlaces: 2, | ||
unitSeparator: " ", | ||
fixedDecimals, | ||
}) | ||
} |
@@ -8,2 +8,3 @@ export const startSpinner = ({ | ||
text = "", | ||
update = (text) => text, | ||
effect = () => {}, | ||
@@ -19,2 +20,3 @@ }) => { | ||
const render = () => { | ||
spinner.text = update(spinner.text) | ||
return `${frames[frameIndex]} ${spinner.text}` | ||
@@ -21,0 +23,0 @@ } |
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
18002
568