Skip to content

Commit

Permalink
feat: ulid generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinkrustev committed Sep 18, 2024
1 parent cac98ca commit 86b4203
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
25 changes: 21 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"rc": "1.2.8",
"shins": "2.6.0",
"uuid4": "2.0.3",
"ulidx": "2.4.1",
"widdershins": "^4.0.1",
"yaml": "2.5.1"
},
Expand All @@ -80,7 +81,7 @@
"audit-ci": "^7.1.0",
"base64url": "3.0.1",
"chance": "1.1.12",
"npm-check-updates": "17.1.1",
"npm-check-updates": "17.1.2",
"nyc": "17.0.0",
"pre-commit": "1.2.2",
"proxyquire": "2.1.3",
Expand Down
5 changes: 4 additions & 1 deletion src/util/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
******/

const crypto = require('crypto')
const { monotonicFactory, ulid } = require('ulidx')

const generators = {
// generate UUID compliant with https://datatracker.ietf.org/doc/html/rfc9562
Expand All @@ -34,7 +35,9 @@ const generators = {
const random = crypto.randomUUID()
return `${timestamp.substring(0, 8)}-${timestamp.substring(8, 12)}-7${random.substring(15, 36)}`
}
})[version]
})[version],
// monotonic parameter ensures ULIDs are generated in order when called at the same or older seed millisecond
ulid: ({ monotonic = true }) => monotonic ? monotonicFactory() : ulid
}

module.exports = ({ type = 'uuid', ...config } = {}) => generators[type](config)
15 changes: 15 additions & 0 deletions test/unit/util/id.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Test = require('tapes')(require('tape'))
const Sinon = require('sinon')
const idGenerator = require('../../../src/util/id')
const uuidRegex = version => new RegExp(`[a-f0-9]{8}-[a-f0-9]{4}-${version}[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}`)
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i

Test('Id util', idTest => {
let sandbox
Expand All @@ -52,6 +53,20 @@ Test('Id util', idTest => {
test.match(uuid7(), uuidRegex(7))
test.end()
})
generateSha256Test.test('generate ULID monotonic by default', test => {
const ulid = idGenerator({ type: 'ulid' })
test.match(ulid(), ulidRegex)
const idList = Array.from({ length: 100 }, () => ulid())
test.ok(idList.slice().sort().join('') === idList.join(''), 'ULIDs should be monotonic')
test.end()
})
generateSha256Test.test('generate ULID non-monotonic', test => {
const ulid = idGenerator({ type: 'ulid', monotonic: false })
test.match(ulid(), ulidRegex)
const idList = Array.from({ length: 100 }, () => ulid())
test.ok(idList.slice().sort().join('') !== idList.join(''), 'ULIDs should not be monotonic')
test.end()
})
generateSha256Test.test('generate UUID v7 by default', test => {
const uuid = idGenerator()
test.match(uuid(), uuidRegex(7))
Expand Down

0 comments on commit 86b4203

Please sign in to comment.