Skip to content

Commit

Permalink
improve: Now returning "0s" for 0 ms or invalid input
Browse files Browse the repository at this point in the history
Signed-off-by: Jaid <[email protected]>
  • Loading branch information
Jaid committed Nov 14, 2019
1 parent 5d8f7ee commit c9564b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import humanizeDuration from "humanize-duration"

/**
* @function
* @param {number} milliseconds
* @returns {string} Formatted duration
* @example
* import readableMs from "readable-ms"
* const result = readableMs(3021)
* result === "3s 21ms"
*/
export default humanizeDuration.humanizer({
const humanizer = humanizeDuration.humanizer({
units: ["h", "m", "s", "ms"],
spacer: "",
delimiter: " ",
Expand All @@ -24,4 +15,23 @@ export default humanizeDuration.humanizer({
ms: () => "ms",
},
},
})
})

/**
* @function
* @param {number} milliseconds
* @returns {string} Formatted duration
* @example
* import readableMs from "readable-ms"
* const result = readableMs(3021)
* result === "3s 21ms"
* const result2 = readableMs(0)
* result2 === "0s"
*/
export default ms => {
if (Number.isFinite(ms) && ms !== 0) {
return humanizer(ms)
} else {
return "0s"
}
}
5 changes: 3 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const indexModule = (process.env.MAIN ? path.resolve(process.env.MAIN) : path.jo
const {default: readableMs} = indexModule

it("should run", () => {
const result = readableMs(3021)
expect(result).toBe("3s 21ms")
expect(readableMs(3021)).toBe("3s 21ms")
expect(readableMs(0)).toBe("0s")
expect(readableMs()).toBe("0s")
})

0 comments on commit c9564b5

Please sign in to comment.