From f44b77a32f48aca6c97e7762d5b97c0e4925d67a Mon Sep 17 00:00:00 2001 From: Andrew Chilton Date: Sat, 18 Apr 2020 13:04:33 +1200 Subject: [PATCH] Add .asEpoch() and .asRandom() (rename .toDate() to .asDate()) --- ReadMe.md | 27 +++++++++++++++++++++++++++ index.js | 23 ++++++++++++++++++++--- test.js | 21 +++++++++++++++------ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 1d26dca..5eb9719 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -19,6 +19,33 @@ A `yid` is: * starts off with `Date.now()` * uses a substring of https://www.npmjs.com/package/math-random for the second part +## `yid.asDate(id)` ## + +Returns the numbers from the first part of the `id` as a `Date()`. + +```js +yid.asDate(id) +// -> Date: "2018-01-27T10:46:29.798Z" +``` + +## `yid.asEpoch(id)` ## + +Returns the numbers from the first part of the `id` as a `Number()`. + +```js +yid.asEpoch(id) +// -> Number: 1517049989798 +``` + +## `yid.asRandom(id)` ## + +Returns the random collection of digits from the second part of the `id`. + +```js +yid.asRandom(id) +// -> "7496988299172" +``` + ## Why? ## Why another ID generating library? diff --git a/index.js b/index.js index d7faa9a..2f79e9b 100644 --- a/index.js +++ b/index.js @@ -10,14 +10,31 @@ function yid() { return id } -function toDate(id) { +function asDate(id) { if ( !id.match(/^\d{13}-\d{13}$/) ) { - throw new Error(`Format of id is incorrect: ${id}`) + throw new Error(`yid.asDate(): Format of id is incorrect: ${id}`) } const epoch = id.split('-')[0] return new Date(Number(epoch)) } -yid.toDate = toDate +function asEpoch(id) { + if ( !id.match(/^\d{13}-\d{13}$/) ) { + throw new Error(`yid.asEpoch(): Format of id is incorrect: ${id}`) + } + const epoch = id.split('-')[0] + return Number(epoch) +} + +function asRandom(id) { + if ( !id.match(/^\d{13}-\d{13}$/) ) { + throw new Error(`yid.asRandom(): Format of id is incorrect: ${id}`) + } + return id.split('-')[1] +} + +yid.asDate = asDate +yid.asEpoch = asEpoch +yid.asRandom = asRandom module.exports = yid diff --git a/test.js b/test.js index f30bb4e..76547ec 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ const yid = require('.') // setup -const times = 1000000 +const times = 100000 const count = { '22' : 0, '23' : 0, @@ -29,17 +29,26 @@ for(let i = 0; i < times; i++) { id = yid() count['' + id.length]++ - // check we can get the date back - const d = yid.toDate(id) + // .asDate() + const d = yid.asDate(id) if (!(d instanceof Date)) { - throw new Error("toDate() returned something that wasn't a date!") + throw new Error("asDate() returned something that wasn't a date!") } if (String(d.valueOf()) != id.split('-')[0]) { throw new Error("the date's epoch (d.valueOf()) didn't match the epoch of the id") } -} -console.log('count:', count) + // .asEpoch() + const epoch = yid.asEpoch(id) + if (d.valueOf() != epoch) { + throw new Error("the date's epoch didn't match the epoch of the id") + } + + // .asRandom() + if (yid.asRandom(id) != id.split('-')[1]) { + throw new Error("the date's random part didn't match the that of the id") + } +} if ( count['27'] !== times ) { throw new Error("A yid was returned that wasn't 27 chars long")