Skip to content

Commit

Permalink
Add .asEpoch() and .asRandom() (rename .toDate() to .asDate())
Browse files Browse the repository at this point in the history
  • Loading branch information
chilts committed Apr 18, 2020
1 parent e1b1cc4 commit f44b77a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
27 changes: 27 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 15 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const yid = require('.')

// setup
const times = 1000000
const times = 100000
const count = {
'22' : 0,
'23' : 0,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f44b77a

Please sign in to comment.