-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement human readable time converter and metric utility functions
- Loading branch information
Showing
7 changed files
with
141 additions
and
5 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 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,48 @@ | ||
import * as math from './math.js' | ||
|
||
export const yotta = 1e24 | ||
export const zetta = 1e21 | ||
export const exa = 1e18 | ||
export const peta = 1e15 | ||
export const tera = 1e12 | ||
export const giga = 1e9 | ||
export const mega = 1e6 | ||
export const kilo = 1e3 | ||
export const hecto = 1e2 | ||
export const deca = 10 | ||
export const deci = .1 | ||
export const centi = .01 | ||
export const milli = 1e-3 | ||
export const micro = 1e-6 | ||
export const nano = 1e-9 | ||
export const pico = 1e-12 | ||
export const femto = 1e-15 | ||
export const atto = 1e-18 | ||
export const zepto = 1e-21 | ||
export const yocto = 1e-24 | ||
|
||
const prefixUp = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] | ||
const prefixDown = ['', 'm', 'μ', 'n', 'p', 'f', 'a', 'z', 'y'] | ||
|
||
/** | ||
* @param {number} n | ||
* @param {number} [baseMultiplier] Multiplier of the base (10^(3*baseMultiplier)). E.g. `convert(time, -3)` if time is already in milli seconds | ||
* @return {{n:number,prefix:string}} | ||
*/ | ||
export const prefix = (n, baseMultiplier = 0) => { | ||
const nPow = math.log10(n) | ||
let mult = 0 | ||
while (nPow < mult * 3 && baseMultiplier > -8) { | ||
baseMultiplier-- | ||
mult-- | ||
} | ||
while (nPow >= 3 + mult * 3 && baseMultiplier < 8) { | ||
baseMultiplier++ | ||
mult++ | ||
} | ||
const prefix = baseMultiplier < 0 ? prefixDown[-baseMultiplier] : prefixUp[baseMultiplier] | ||
return { | ||
n: math.round((mult > 0 ? n / math.exp10(mult * 3) : n * math.exp10(mult * -3)) * 1e12) / 1e12, | ||
prefix | ||
} | ||
} |
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,34 @@ | ||
import * as t from './testing.js' | ||
import * as metric from './metric.js' | ||
|
||
/** | ||
* @param {t.TestCase} tc | ||
*/ | ||
export const testMetricPrefix = tc => { | ||
t.compare(metric.prefix(1, -1), { n: 1, prefix: 'm' }) | ||
t.compare(metric.prefix(1.5), { n: 1.5, prefix: '' }) | ||
t.compare(metric.prefix(100.5), { n: 100.5, prefix: '' }) | ||
t.compare(metric.prefix(1000.5), { n: 1.0005, prefix: 'k' }) | ||
t.compare(metric.prefix(.3), { n: 300, prefix: 'm' }) | ||
t.compare(metric.prefix(.001), { n: 1, prefix: 'm' }) | ||
// up | ||
t.compare(metric.prefix(10000), { n: 10, prefix: 'k' }) | ||
t.compare(metric.prefix(1e7), { n: 10, prefix: 'M' }) | ||
t.compare(metric.prefix(1e11), { n: 100, prefix: 'G' }) | ||
t.compare(metric.prefix(1e12 + 3), { n: (1e12 + 3) / 1e12, prefix: 'T' }) | ||
t.compare(metric.prefix(1e15), { n: 1, prefix: 'P' }) | ||
t.compare(metric.prefix(1e20), { n: 100, prefix: 'E' }) | ||
t.compare(metric.prefix(1e22), { n: 10, prefix: 'Z' }) | ||
t.compare(metric.prefix(1e24), { n: 1, prefix: 'Y' }) | ||
t.compare(metric.prefix(1e28), { n: 10000, prefix: 'Y' }) | ||
// down | ||
t.compare(metric.prefix(.01), { n: 10, prefix: 'm' }) | ||
t.compare(metric.prefix(1e-4), { n: 100, prefix: 'μ' }) | ||
t.compare(metric.prefix(1e-9), { n: 1, prefix: 'n' }) | ||
t.compare(metric.prefix(1e-12), { n: 1, prefix: 'p' }) | ||
t.compare(metric.prefix(1e-14), { n: 10, prefix: 'f' }) | ||
t.compare(metric.prefix(1e-18), { n: 1, prefix: 'a' }) | ||
t.compare(metric.prefix(1e-21), { n: 1, prefix: 'z' }) | ||
t.compare(metric.prefix(1e-22), { n: 100, prefix: 'y' }) | ||
t.compare(metric.prefix(1e-30), { n: .000001, prefix: 'y' }) | ||
} |
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
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 |
---|---|---|
@@ -1,3 +1,29 @@ | ||
|
||
import * as metric from './metric.js' | ||
import * as math from './math.js' | ||
|
||
export const getDate = () => new Date() | ||
export const getUnixTime = Date.now | ||
|
||
/** | ||
* @param {number} d duration in milliseconds | ||
* @return {string} humanized approximation of time | ||
*/ | ||
export const humanizeDuration = d => { | ||
if (d < 60000) { | ||
const p = metric.prefix(d, -1) | ||
return math.round(p.n * 100) / 100 + p.prefix + 's' | ||
} | ||
d = math.floor(d / 1000) | ||
const seconds = d % 60 | ||
const minutes = math.floor(d / 60) % 60 | ||
const hours = math.floor(d / 3600) % 24 | ||
const days = math.floor(d / 86400) | ||
if (days > 0) { | ||
return days + 'd' + (hours > 0 ? ' ' + (minutes > 30 ? hours + 1 : hours) + 'h' : '') | ||
} | ||
if (hours > 0 ) { | ||
return hours + 'h' + (minutes > 0 ? ' ' + (seconds > 30 ? minutes + 1 : minutes) + 'min' : '') | ||
} | ||
return minutes + 'min' + (seconds > 0 ? ' ' + seconds + 's' : '') | ||
} |
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