New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

kuuid

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kuuid - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

14

index.d.ts

@@ -17,2 +17,9 @@

/**
* Generate the 8-character prefix - reverse mode
* @param t ISO-8601 date string or number of milliseconds since 1970
* @return 8-character prefix string
*/
function prefix(t? : string | number): string;
/**
* Generate 128-bits of random date encoded as 24 character string

@@ -29,2 +36,9 @@ * @return 24-character prefix string

function id(t? : string | number): string;
/**
* Generate a reverse date/time sortable 32 character unique identifier
* @param t ISO-8601 date string or number of milliseconds since 1970
* @return 32-character id
*/
function id(t? : string | number): string;
}

@@ -31,0 +45,0 @@

32

index.js
// standard Node.js crypto library
const crypto = require('crypto')
// the maximum timestamp achievable (8 digits of base 62)
const maxTS = Math.pow(62, 8) - 1
// padStart polyfill
if (!String.prototype.padStart) {
String.prototype.padStart = require('./lib/padStart.js')
String.prototype.padStart = require('./lib/padStart.js') // eslint-disable-line
}

@@ -15,3 +18,5 @@

let prefix = function(t) {
// calculate an 8-digit prefix for the timestamp 't'
// that is base62 encoded and sorts in time order
let prefix = function (t) {
// get time stamp for now

@@ -24,3 +29,13 @@ const timestamp = ts(t)

let rand = function() {
// calculate an 8-digit prefix for the timestamp 't'
// that is base62 encoded and sorts in reverse time order
let prefixReverse = function (t) {
// get time stamp for now
const timestamp = maxTS - ts(t)
// turn timestamp into 8-digit, base-62 encoded string
return base62Encode(timestamp).padStart(8, '0')
}
let rand = function () {
// we want 128-bits of random data. To do this we

@@ -41,6 +56,13 @@ // add 4 batches of 4 random bytes encoded as 6-digit, base-62 encoded strings

// generate a kuuid (reverse mode)
let idr = function (t) {
return prefixReverse(t) + rand()
}
module.exports = {
id: id,
idr: idr,
rand: rand,
prefix: prefix
}
prefix: prefix,
prefixReverse: prefixReverse
}

2

lib/base62.js

@@ -20,2 +20,2 @@ // the number of characters to encode by

module.exports = base62Encode
module.exports = base62Encode
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
let padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0 //truncate if number or convert non-number to 0;
let padStart = function padStart (targetLength, padString) {
targetLength = targetLength >> 0 // truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '))
if (this.length > targetLength) {
return String(this)
}
else {
} else {
targetLength = targetLength - this.length
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length) //append to original to ensure we are longer than needed
padString += padString.repeat(targetLength / padString.length) // append to original to ensure we are longer than needed
}

@@ -18,2 +17,2 @@ return padString.slice(0, targetLength) + String(this)

module.exports = padStart
module.exports = padStart

@@ -7,2 +7,2 @@ // generate a timestamp - number of seconds since the epoch

module.exports = ts
module.exports = ts
{
"name": "kuuid",
"version": "0.3.0",
"version": "0.4.0",
"description": "K-sortable UUID - roughly time-sortable unique id generator",

@@ -8,3 +8,4 @@ "main": "index.js",

"scripts": {
"test": "mocha"
"standard": "standard --fix",
"test": "standard; mocha"
},

@@ -29,4 +30,10 @@ "repository": {

"devDependencies": {
"mocha": "^5.2.0"
"mocha": "^6.0.2",
"standard": "^12.0.1"
},
"standard": {
"env": [
"mocha"
]
}
}

@@ -11,3 +11,3 @@ # kuuid

If a `kuuid`-generated id were used as a database's unique identifier, it would sort roughly in time order.
If a `kuuid`-generated id were used as a database's unique identifier, it would sort roughly in time order (`kuuid.id()`), or reverse time order (`kuuid.idr()`)

@@ -63,2 +63,16 @@ ## Installation

## Reverse mode
If you want your data to sort into "newest first" order, then `kuuid.idr()` returns an id that sorts in the opposite order:
```js
// 'now'
kuuid.idr()
// zzyIy6DZ2SKTqh2WpV6D0DTbkK0kbn5u
// Epoch
kuuid.idr('1970-01-01T00:00:00Z')
// zzzzzzzz2v3VKT4Sl9yV2f6v673SDt5v
```
## Generating a prefix

@@ -79,2 +93,8 @@

or for a reverse-mode prefix:
```js
kuuid.prefixReverse()
```
## How does it work?

@@ -84,3 +104,3 @@

- 8 charact.ers representing the number of seconds since 1st January 1970.
- 8 characters representing the number of seconds since 1st January 1970.
- 24 characters containing random data.

@@ -87,0 +107,0 @@

@@ -8,6 +8,6 @@ let kuuid = require('.')

let doc = {
_id: kuuid(),
_id: kuuid.id(),
date: new Date().toString()
}
db.insert(doc, function(err,data) { console.log(data)});
}, 5000)
}, 1000)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc