Comparing version 0.1.0 to 0.2.0
@@ -16,5 +16,7 @@ // standard Node.js crypto library | ||
// generate a kuuid | ||
let kuuid = function () { | ||
let kuuid = function (t) { | ||
// get time stamp for now | ||
const timestamp = ts() | ||
const timestamp = ts(t) | ||
@@ -21,0 +23,0 @@ // turn timestamp into 8-digit, base-62 encoded string |
@@ -1,10 +0,7 @@ | ||
// the epoch which we shall measure time from | ||
const epoch = new Date('2018-01-01').getTime() / 1000 | ||
// generate a timestamp since the epoch | ||
let ts = function () { | ||
const now = new Date().getTime() / 1000 | ||
return Math.floor(now - epoch) | ||
// generate a timestamp - number of seconds since the epoch | ||
let ts = function (t) { | ||
let d = t ? new Date(t) : new Date() | ||
return Math.floor(d.getTime() / 1000) | ||
} | ||
module.exports = ts |
{ | ||
"name": "kuuid", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "K-sortable UUID - roughly time-sortable unique id generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -33,3 +33,3 @@ # kuuid | ||
let id = kuuid() | ||
// 0001AKWJ2fG1h23DMFgI473u2G4JbUvy | ||
// 001fgS7k4gJxqY1aXpni3gHuOy0WusLe | ||
``` | ||
@@ -46,6 +46,19 @@ | ||
} | ||
// {"_id":"0001AKY50w6w4833bGxL26pDWU4UFhTX","name":"Glynn","location":"UK","verified":true} | ||
// {"_id":"001fgS954GN35e4NJPyK1W9aiE44m2xD","name":"Glynn","location":"UK","verified":true} | ||
db.insert(doc) | ||
``` | ||
Supplying no parameter to `kuuid()` uses the current time to generate the timestamp part of the id. You may also supply your own date/time: | ||
```js | ||
// 'now' | ||
kuuid() | ||
// ISO String | ||
kuuid('2018-07-20T10:10:34.234Z') | ||
// millseconds since 1970 | ||
kuuid(1514764800) | ||
``` | ||
## How does it work? | ||
@@ -55,3 +68,3 @@ | ||
- 8 characters representing the number of seconds since 1st January 2018. | ||
- 8 characters representing the number of seconds since 1st January 1970. | ||
- 24 characters containing random data. | ||
@@ -65,4 +78,4 @@ | ||
1) The `kuuid` library can only be used to store dates after `2018-01-01` as it uses that date as its "epoch". | ||
1) The `kuuid` library can only be used to store dates after `1970-01-01`. | ||
2) The random number is genreated using Node.js's [crypto.randomBytes](https://nodejs.org/dist/latest-v8.x/docs/api/crypto.html#crypto_crypto_randombytes_size_callback) which is a secure, if slow, source of random information. | ||
3) The character set used by the base-62 encoding algorithm differs from other algorithms I've seen to ensure that it sorts correctly in a CouchDB `_id` field. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17143
9
77
71