Skip to content

Commit

Permalink
closes #14 - the /undefined padding bug (#15)
Browse files Browse the repository at this point in the history
* fix bug in `pad(id)`

* add tests

* doc the injectable  + test it

* simpler english
  • Loading branch information
osher authored Apr 26, 2020
1 parent eca6f80 commit 4540fa0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ that is 33 characters in length, by default `fixedLength` is `false`.
If `{ fixedLength: true }` is passed in, the function will always generate an id
that is 33 characters in length, by default `fixedLength` is `false`.
If `{ urlSafe: true }` is passed in, the function will generate url safe ids.
If `{ startFrom: <int> }` is passed in, the first counter will start from that
number, which must be beteen 0 and 2147483647. Fractions are discarded, only the
integer part matters.

### instance()

Expand Down
20 changes: 14 additions & 6 deletions hyperid.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ function hyperid (opts) {
fixedLength = !!opts.fixedLength
}

var count = 0

generate.uuid = uuid()
generate.decode = decode

var id = baseId(generate.uuid, urlSafe)
var count = Math.floor(opts.startFrom || 0)

if (isNaN(count) || !(maxInt > count && count >= 0)) {
throw new Error([
`when passed, opts.startFrom must be a number between 0 and ${maxInt}.`,
'Only the integer part matters.',
`- got: ${opts.startFrom}`
].join('\n'))
}

return generate

function generate () {
var result = fixedLength
Expand All @@ -34,10 +45,6 @@ function hyperid (opts) {

return result
}

generate.decode = decode

return generate
}

function pad (count) {
Expand All @@ -50,6 +57,7 @@ function pad (count) {
if (count < 10000000) return '000' + count
if (count < 100000000) return '00' + count
if (count < 1000000000) return '0' + count
return count
}

function baseId (id, urlSafe) {
Expand Down
36 changes: 36 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,39 @@ test('decode url safe ids', function (t) {
count: 0
}, 'decode')
})

test('injecting opts.startFrom', function (t) {
t.plan(1)

const instance = hyperid({ startFrom: 999999999 })
const id = instance()

id.endsWith('999999999')
? t.pass('generated as expected')
: t.fail('did not use injected id')
})

test('opts.fixedLength - passed 999999999 - pads correctly', function (t) {
t.plan(1)

const instance = hyperid({ startFrom: 999999999 })
instance()
const id = instance()

id.endsWith('1000000000')
? t.pass('generated as expected')
: t.fail('did not use injected id')
})

test('opts.fixedLength - passed invalid value - throws a friendly error', function (t) {
t.plan(1)

try {
hyperid({ startFrom: 'not a number' })
t.fail('did not throw an expected error')
} catch (e) {
e.message.match(/startFrom must be a number/)
? t.pass('thrown as expected')
: t.fail('this is not the error you\'re looking for')
}
})

0 comments on commit 4540fa0

Please sign in to comment.