Skip to content

Commit 95e0c79

Browse files
author
Cully Larson
committed
Added the 'bignames' and 'onlytwo' exports, with tests.
1 parent a418acb commit 95e0c79

4 files changed

Lines changed: 238 additions & 17 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "formatmicro",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Formats microseconds into a human consumable form.",
55
"license": "MIT",
66
"repository": "cullylarson/formatmicro",

readme.md

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,79 @@ console.log(formatmicro(totalTimeMult))
4343

4444
```
4545

46+
### bignames
47+
48+
You can also import `bignames`, to output increments using their full names (e.g "days", "hours", etc.).
49+
For example:
50+
51+
```js
52+
import {bignames} from 'formatmicro'
53+
54+
const oneµs = 1
55+
const oneMs = 1000
56+
const oneS = 1000*1000
57+
const oneM = 60*1000*1000
58+
const oneH = 60*60*1000*1000
59+
const oneD = 24*60*60*1000*1000
60+
61+
const totalTime = oneD + 12*oneH + 16*oneM + 59*oneS + oneMs + 6*oneµs
62+
63+
console.log("Task completed in: " + bignames(totalTime))
64+
// Task completed in: 1 day 12 hours 16 minutes 59 seconds 1 millisecond 6 microseconds
65+
```
66+
67+
The function signature is the same as `formatmicro`, so you could provide a custom reduce or custom
68+
increment names if you wanted.
69+
70+
### onlytwo
71+
72+
You can also import `onlytwo`, to output only the first two, non-zero increments.
73+
For example:
74+
75+
```js
76+
import {onlytwo} from 'formatmicro'
77+
78+
const oneµs = 1
79+
const oneMs = 1000
80+
const oneS = 1000*1000
81+
const oneM = 60*1000*1000
82+
const oneH = 60*60*1000*1000
83+
const oneD = 24*60*60*1000*1000
84+
85+
const totalTime = oneD + 12*oneH + 16*oneM + 59*oneS + oneMs + 6*oneµs
86+
87+
console.log("Task completed in: " + onlytwo(totalTime))
88+
// Task completed in: 1 d 12 h
89+
```
90+
91+
The function signature is the same as `formatmicro`, so you could provide a custom reduce or custom
92+
increment names if you wanted. For example:
93+
94+
```js
95+
import {onlytwo} from 'formatmicro'
96+
97+
const oneµs = 1
98+
const oneMs = 1000
99+
const oneS = 1000*1000
100+
const oneM = 60*1000*1000
101+
const oneH = 60*60*1000*1000
102+
const oneD = 24*60*60*1000*1000
103+
104+
const totalTime = 12*oneH + oneS + 9*oneMs + oneµs
105+
106+
const incrementNames = {
107+
'd' : ['day', 'days'],
108+
'h' : ['hour', 'hours'],
109+
'm' : ['minute', 'minutes'],
110+
's' : ['second', 'seconds'],
111+
'ms' : ['millisecond', 'milliseconds'],
112+
'µs' : ['microsecond', 'microseconds'],
113+
}
114+
115+
console.log("Task completed in: " + onlytwo(totalTime, incrementNames))
116+
// Task completed in: 12 hours 1 second
117+
```
118+
46119
## Options
47120

48121
You can optionally pass the names of the increments (e.g hours, minutes, days, etc.) as the second
@@ -84,7 +157,7 @@ It accepts the following parameters:
84157

85158
* __carry__ _(string)_ This is the return from the last call to the reducer function (starts
86159
as a empty string).
87-
* __incrementName__ _(string)_ This will be: d, h, m, s, ms, or µs
160+
* __incrementKey__ _(string)_ This will be: d, h, m, s, ms, or µs
88161
* __value__ _(int)_ The value for this increment (e.g. the number of seconds, or the number of minutes).
89162

90163
Here are some examples:
@@ -111,12 +184,12 @@ const incrementNames = {
111184
'µs' : ['µs', 'µs'],
112185
}
113186

114-
const formatReduce = (carry, incrementName, value) => {
187+
const formatReduce = (carry, incrementKey, value) => {
115188
if(value === 0) return carry
116189
else return carry +
117190
((carry === "") ? "" : " ") +
118191
value.toString() + " " +
119-
((value === 1) ? incrementNames[incrementName][0] : incrementNames[incrementName][1])
192+
((value === 1) ? incrementNames[incrementKey][0] : incrementNames[incrementKey][1])
120193
}
121194

122195
console.log("Task completed in: " + formatmicro(period, formatReduce))
@@ -126,7 +199,7 @@ console.log("Task completed in: " + formatmicro(period, formatReduce))
126199

127200
```js
128201
let foundNum = 0
129-
const formatReduce = (carry, incrementName, value) => {
202+
const formatReduce = (carry, incrementKey, value) => {
130203
if(foundNum >= 3) return carry
131204
if(value === 0) return carry
132205

@@ -135,7 +208,7 @@ const formatReduce = (carry, incrementName, value) => {
135208
return carry +
136209
((carry !== "") ? " " : "") +
137210
value.toString() + " " +
138-
incrementName
211+
incrementKey
139212
}
140213
```
141214

src/index.js

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
'use strict'
2+
3+
const defaultIncrementNames = {
4+
'd': ['d', 'd'],
5+
'h': ['h', 'h'],
6+
'm': ['m', 'm'],
7+
's': ['s', 's'],
8+
'ms': ['ms', 'ms'],
9+
'µs': ['µs', 'µs'],
10+
}
211
/**
312
* @module formatmicro
413
*/
514

615
/**
16+
* Formats microseconds into strings that look like: 1 d 3 h 7 m 28 s 500 ms 324 µs
17+
*
718
* @param {number} timeMicro
8-
* @param {object|function} incrementNames
19+
* @param {object|function|undefined} incrementNames
920
* @returns {string}
1021
* @throws {Error} If either argument isn't the correct type
1122
*/
12-
export default function formatmicro(timeMicro, incrementNames) {
23+
const formatmicro = (timeMicro, incrementNames) => {
1324
if(Number(parseFloat(timeMicro)) !== timeMicro) throw new Error("First parameter must be a number.")
1425
if(incrementNames && (typeof incrementNames !== "function") && (typeof incrementNames !== "object")) throw new Error("Second parameter, if provided, must be a function or an object")
1526

@@ -20,21 +31,16 @@ export default function formatmicro(timeMicro, incrementNames) {
2031
}
2132
else {
2233
incrementNames = {
23-
'd' : ['d', 'd'],
24-
'h' : ['h', 'h'],
25-
'm' : ['m', 'm'],
26-
's' : ['s', 's'],
27-
'ms' : ['ms', 'ms'],
28-
'µs' : ['µs', 'µs'],
34+
...defaultIncrementNames,
2935
...incrementNames,
3036
}
3137

32-
formatReduce = (carry, incrementName, value) => {
38+
formatReduce = (carry, incrementKey, value) => {
3339
if(value === 0) return carry
3440
else return carry +
3541
((carry === "") ? "" : " ") +
3642
value.toString() + " " +
37-
((value === 1) ? incrementNames[incrementName][0] : incrementNames[incrementName][1])
43+
((value === 1) ? incrementNames[incrementKey][0] : incrementNames[incrementKey][1])
3844
}
3945
}
4046

@@ -59,3 +65,70 @@ export default function formatmicro(timeMicro, incrementNames) {
5965
else return formatReduce(carry, key, parts[key])
6066
}, "")
6167
}
68+
69+
/**
70+
* Formats microseconds into strings that look like: 1 day 3 hours 7 minutes 28 seconds 500 milliseconds 324 microseconds
71+
*
72+
* @param {number} timeMicro
73+
* @param {object|function|undefined} incrementNames
74+
* @returns {string}
75+
* @throws {Error} If either argument isn't the correct type
76+
*/
77+
const bignames = (timeMicro, incrementNames) => {
78+
if(!incrementNames || (typeof incrementNames === "object")) {
79+
incrementNames = {
80+
'd' : ['day', 'days'],
81+
'h' : ['hour', 'hours'],
82+
'm' : ['minute', 'minutes'],
83+
's' : ['second', 'seconds'],
84+
'ms' : ['millisecond', 'milliseconds'],
85+
'µs' : ['microsecond', 'microseconds'],
86+
...incrementNames,
87+
}
88+
}
89+
90+
return formatmicro(timeMicro, incrementNames)
91+
}
92+
93+
/**
94+
* Same as {@link formatmicro}, except it only returns the first two non-zero values.
95+
*
96+
* @param {number} timeMicro
97+
* @param {object|function|undefined} incrementNames
98+
* @returns {string}
99+
* @throws {Error} If either argument isn't the correct type
100+
*/
101+
const onlytwo = (timeMicro, incrementNames) => {
102+
if(!incrementNames || (typeof incrementNames === "object")) {
103+
incrementNames = {
104+
...defaultIncrementNames,
105+
...incrementNames,
106+
}
107+
}
108+
else if(typeof incrementNames === "function") {
109+
return formatmicro(timeMicro, incrementNames)
110+
}
111+
112+
let foundNum = 0
113+
const takeFor = 2
114+
const formatReduce = (carry, incrementKey, value) => {
115+
if(foundNum >= takeFor) return carry
116+
if(value === 0) return carry
117+
118+
foundNum++
119+
120+
return carry +
121+
((carry === "") ? "" : " ") +
122+
value.toString() + " " +
123+
((value === 1) ? incrementNames[incrementKey][0] : incrementNames[incrementKey][1])
124+
}
125+
126+
return formatmicro(timeMicro, formatReduce)
127+
}
128+
129+
export {
130+
formatmicro as default,
131+
formatmicro,
132+
bignames,
133+
onlytwo,
134+
}

src/test.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert'
2-
import formatmicro from "./../dist/index.js"
2+
import {default as formatmicro, bignames, onlytwo} from "./../dist/index.js"
33

44
const oneµs = 1
55
const oneMs = 1000
@@ -17,6 +17,15 @@ const customIncrementNames = {
1717
'µs' : ['microsecond', 'microseconds'],
1818
}
1919

20+
const defaultIncrementNames = {
21+
'd': ['d', 'd'],
22+
'h': ['h', 'h'],
23+
'm': ['m', 'm'],
24+
's': ['s', 's'],
25+
'ms': ['ms', 'ms'],
26+
'µs': ['µs', 'µs'],
27+
}
28+
2029
describe('Format Micro', () => {
2130
describe("For only one increment", () => {
2231
it("Should work with microseconds", () => {
@@ -202,3 +211,69 @@ describe('Format Micro', () => {
202211
})
203212
})
204213
})
214+
215+
describe("Big Names", () => {
216+
describe("For mixed increments", () => {
217+
it("Should use the singular custom names", () => {
218+
const totalTimeOne = oneD + oneH + oneM + oneS + oneMs + oneµs
219+
220+
assert.equal(bignames(totalTimeOne), "1 day 1 hour 1 minute 1 second 1 millisecond 1 microsecond")
221+
})
222+
223+
it("Should use the plural custom names", () => {
224+
const totalTimeMult = 4*oneD + 12*oneH + 16*oneM + 59*oneS + 9*oneMs + 6*oneµs
225+
226+
assert.equal(bignames(totalTimeMult), "4 days 12 hours 16 minutes 59 seconds 9 milliseconds 6 microseconds")
227+
})
228+
229+
it("Should use the plural and singular custom names", () => {
230+
const totalTimeMixed = oneD + 12*oneH + oneM + 59*oneS + 9*oneMs + oneµs
231+
232+
assert.equal(bignames(totalTimeMixed), "1 day 12 hours 1 minute 59 seconds 9 milliseconds 1 microsecond")
233+
})
234+
})
235+
236+
describe("For alternate increment names array", () => {
237+
it("Should use the alternate names", () => {
238+
const totalTimeMixed = oneD + 12*oneH + oneM + 59*oneS + 9*oneMs + oneµs
239+
240+
assert.equal(bignames(totalTimeMixed, defaultIncrementNames), "1 d 12 h 1 m 59 s 9 ms 1 µs")
241+
})
242+
})
243+
})
244+
245+
describe("Only Two", () => {
246+
describe("For all non-zero values", () => {
247+
it("Should format only the first two", () => {
248+
const totalTimeMult = 4*oneD + oneH + 16*oneM + 59*oneS + 9*oneMs + 6*oneµs
249+
250+
assert.equal(onlytwo(totalTimeMult), "4 d 1 h")
251+
})
252+
})
253+
describe("For some zero values", () => {
254+
it("Should format only the first two non-zero", () => {
255+
const totalTimeMult = 4*oneD + 59*oneS + 9*oneMs + 6*oneµs
256+
257+
assert.equal(onlytwo(totalTimeMult), "4 d 59 s")
258+
})
259+
})
260+
describe("For custom increment names array", () => {
261+
it("Should use the singular custom names", () => {
262+
const totalTimeOne = oneD + oneH + oneM + oneS + oneMs + oneµs
263+
264+
assert.equal(onlytwo(totalTimeOne, customIncrementNames), "1 day 1 hour")
265+
})
266+
267+
it("Should use the plural custom names", () => {
268+
const totalTimeMult = 4*oneD + 12*oneH + 16*oneM + 59*oneS + 9*oneMs + 6*oneµs
269+
270+
assert.equal(onlytwo(totalTimeMult, customIncrementNames), "4 days 12 hours")
271+
})
272+
273+
it("Should use the plural and singular custom names", () => {
274+
const totalTimeMixed = 12*oneH + oneS + 9*oneMs + oneµs
275+
276+
assert.equal(onlytwo(totalTimeMixed, customIncrementNames), "12 hours 1 second")
277+
})
278+
})
279+
})

0 commit comments

Comments
 (0)