Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ai/nanoid
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.1.5
Choose a base ref
...
head repository: ai/nanoid
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.1.6
Choose a head ref
  • 11 commits
  • 11 files changed
  • 2 contributors

Commits on Oct 23, 2019

  1. Copy the full SHA
    cbd5803 View commit details
  2. golf: optimize loop

    CyberAP committed Oct 23, 2019
    Copy the full SHA
    61d2a04 View commit details
  3. revert

    CyberAP committed Oct 23, 2019
    Copy the full SHA
    49d707b View commit details
  4. improve format tests

    CyberAP committed Oct 23, 2019
    Copy the full SHA
    6347096 View commit details

Commits on Oct 24, 2019

  1. Merge pull request #156 from CyberAP/tests

    test: Improve format tests
    ai authored Oct 24, 2019
    Copy the full SHA
    c7e1c7e View commit details
  2. Merge pull request #155 from CyberAP/golf-2

    golf: Remove extra branching, reduce generate size by 3 bytes
    ai authored Oct 24, 2019
    Copy the full SHA
    fe3faf5 View commit details
  3. Update sharc config

    ai committed Oct 24, 2019
    Copy the full SHA
    5ff2a47 View commit details
  4. Revert fast mask for Node.js

    ai committed Oct 24, 2019
    Copy the full SHA
    7db8ab1 View commit details
  5. Improve tests

    ai committed Oct 24, 2019
    Copy the full SHA
    3489e1e View commit details
  6. Reduce async format size too

    ai committed Oct 24, 2019
    Copy the full SHA
    7666d23 View commit details
  7. Release 2.1.6 version

    ai committed Oct 24, 2019
    Copy the full SHA
    13e3c44 View commit details
Showing with 113 additions and 28 deletions.
  1. +1 −0 .travis.yml
  2. +4 −0 CHANGELOG.md
  3. +17 −0 async/format.browser.js
  4. +2 −5 async/format.js
  5. +14 −0 format.browser.js
  6. +3 −6 format.js
  7. +6 −4 package.json
  8. +12 −6 test/async-format.test.js
  9. +40 −0 test/browser.test.js
  10. +10 −3 test/format.test.js
  11. +4 −4 yarn.lock
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
cache: yarn
node_js:
- node
- "12"
- "10"
- "8"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).

## 2.1.6
* Reduce size (by Stas Lashmanov).
* Return fast mask for Node.js.

## 2.1.5
* Reduce size (by Max Graey).
* Fix IE support.
17 changes: 17 additions & 0 deletions async/format.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function (random, alphabet, size) {
var mask = (2 << 31 - Math.clz32((alphabet.length - 1) | 1)) - 1
var step = Math.ceil(1.6 * mask * size / alphabet.length)

function tick (id) {
return random(step).then(function (bytes) {
var i = step
while (i--) {
id += alphabet[bytes[i] & mask] || ''
if (id.length === +size) return id
}
return tick(id)
})
}

return tick('')
}
7 changes: 2 additions & 5 deletions async/format.js
Original file line number Diff line number Diff line change
@@ -36,11 +36,8 @@ module.exports = function (random, alphabet, size) {
return random(step).then(function (bytes) {
var i = step
while (i--) {
var alpha = alphabet[bytes[i] & mask]
if (alpha) {
id += alpha
if (id.length === +size) return id
}
id += alphabet[bytes[i] & mask] || ''
if (id.length === +size) return id
}
return tick(id)
})
14 changes: 14 additions & 0 deletions format.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function (random, alphabet, size) {
var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1
var step = Math.ceil(1.6 * mask * size / alphabet.length)
var id = ''

while (true) {
var i = step
var bytes = random(i)
while (i--) {
id += alphabet[bytes[i] & mask] || ''
if (id.length === +size) return id
}
}
}
9 changes: 3 additions & 6 deletions format.js
Original file line number Diff line number Diff line change
@@ -27,19 +27,16 @@
* @function
*/
module.exports = function (random, alphabet, size) {
var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1
var mask = (2 << 31 - Math.clz32((alphabet.length - 1) | 1)) - 1
var step = Math.ceil(1.6 * mask * size / alphabet.length)
var id = ''

while (true) {
var i = step
var bytes = random(i)
while (i--) {
var alpha = alphabet[bytes[i] & mask]
if (alpha) {
id += alpha
if (id.length === +size) return id
}
id += alphabet[bytes[i] & mask] || ''
if (id.length === +size) return id
}
}
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nanoid",
"version": "2.1.5",
"version": "2.1.6",
"description": "A tiny (139 bytes), secure URL-friendly unique string ID generator",
"keywords": [
"uuid",
@@ -20,8 +20,10 @@
"repository": "ai/nanoid",
"browser": {
"./index.js": "./index.browser.js",
"./format.js": "./format.browser.js",
"./random.js": "./random.browser.js",
"./async/index.js": "./async/index.browser.js",
"./async/format.js": "./async/format.browser.js",
"./async/random.js": "./async/random.browser.js"
},
"react-native": {
@@ -30,7 +32,7 @@
"sideEffects": false,
"devDependencies": {
"@logux/eslint-config": "^33.0.0",
"@logux/sharec-config": "^0.5.4",
"@logux/sharec-config": "^0.5.5",
"@size-limit/preset-small-lib": "^2.1.6",
"benchmark": "^2.1.4",
"chalk": "^2.4.2",
@@ -70,7 +72,7 @@
},
{
"path": "generate.js",
"limit": "173 B"
"limit": "170 B"
},
{
"path": "url.js",
@@ -90,7 +92,7 @@
},
{
"path": "async/generate.js",
"limit": "198 B"
"limit": "194 B"
}
],
"eslintConfig": {
18 changes: 12 additions & 6 deletions test/async-format.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
let format = require('../async/format')
let random = require('../async/random')

it('generates random string', async () => {
it('supports generator', async () => {
let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]
async function random (size) {
async function customRandom (size) {
let bytes = []
for (let i = 0; i < size; i += sequence.length) {
bytes = bytes.concat(sequence.slice(0, size - i))
}
return bytes
}
let id = await format(random, 'abcde', 4)
expect(id).toEqual('adca')
expect(await format(customRandom, 'abcde', 4)).toEqual('adca')
expect(await format(customRandom, 'abcde', 14)).toEqual('cbadcbadcbadcc')
})

it('respects size', async () => {
expect(await format(random, 'abcde', 4)).toHaveLength(4)
expect(await format(random, 'abcde', 20)).toHaveLength(20)
})

it('is ready for errors', async () => {
let error = new Error('test')
async function random () {
async function brokenRandom () {
throw error
}

let catched
try {
await format(random, 'abc', 4)
await format(brokenRandom, 'abc', 4)
} catch (e) {
catched = e
}
40 changes: 40 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,11 @@ global.self = {
}

let nanoid = require('../index.browser')
let format = require('../format.browser')
let random = require('../random.browser')
let async = require('../async/index.browser')
let asyncFormat = require('../async/format.browser')
let asyncRandom = require('../async/random.browser')
let url = require('../url')

function times (size, callback) {
@@ -36,6 +40,24 @@ it('changes ID length', () => {
expect(nanoid(10)).toHaveLength(10)
})

it('supports generator', () => {
let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]
function customRandom (size) {
let bytes = []
for (let i = 0; i < size; i += sequence.length) {
bytes = bytes.concat(sequence.slice(0, size - i))
}
return bytes
}
expect(format(customRandom, 'abcde', 4)).toEqual('adca')
expect(format(customRandom, 'abcde', 20)).toEqual('dcbadcbadcbadcbadcdc')
})

it('respects size', () => {
expect(format(random, 'abcde', 4)).toHaveLength(4)
expect(format(random, 'abcde', 20)).toHaveLength(20)
})

it('generates IDs with Promise', async () => {
await Promise.all(times(100, async () => {
let id = await async()
@@ -51,3 +73,21 @@ it('changes ID length in async', async () => {
let id = await async(10)
expect(id).toHaveLength(10)
})

it('supports async generator', async () => {
let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]
async function customRandom (size) {
let bytes = []
for (let i = 0; i < size; i += sequence.length) {
bytes = bytes.concat(sequence.slice(0, size - i))
}
return bytes
}
expect(await asyncFormat(customRandom, 'abcde', 4)).toEqual('adca')
expect(await asyncFormat(customRandom, 'abcde', 14)).toEqual('cbadcbadcbadcc')
})

it('respects size for async', async () => {
expect(await asyncFormat(asyncRandom, 'abcde', 4)).toHaveLength(4)
expect(await asyncFormat(asyncRandom, 'abcde', 20)).toHaveLength(20)
})
13 changes: 10 additions & 3 deletions test/format.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
let format = require('../format')
let random = require('../random')

it('generates random string', () => {
it('supports generator', () => {
let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]
function random (size) {
function customRandom (size) {
let bytes = []
for (let i = 0; i < size; i += sequence.length) {
bytes = bytes.concat(sequence.slice(0, size - i))
}
return bytes
}
expect(format(random, 'abcde', 4)).toEqual('adca')
expect(format(customRandom, 'abcde', 4)).toEqual('adca')
expect(format(customRandom, 'abcde', 18)).toEqual('cbadcbadcbadcbadcc')
})

it('respects size', () => {
expect(format(random, 'abcde', 4)).toHaveLength(4)
expect(format(random, 'abcde', 20)).toHaveLength(20)
})
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1112,10 +1112,10 @@
dependencies:
globals "^12.1.1"

"@logux/sharec-config@^0.5.4":
version "0.5.4"
resolved "https://registry.yarnpkg.com/@logux/sharec-config/-/sharec-config-0.5.4.tgz#e1e80f279ec0cc927bfe90cfde7389f2799bbcce"
integrity sha512-7Jj8MkokBnniZZp/lyEfHsKCI934ERfYtSULzWcruhnSDyMtXpdViAvDroDR2+Oj7cETLhVhtlIsJT5UHVB+Fg==
"@logux/sharec-config@^0.5.5":
version "0.5.5"
resolved "https://registry.yarnpkg.com/@logux/sharec-config/-/sharec-config-0.5.5.tgz#61d097b8118f8aa2203aaddbf359af6c926df29e"
integrity sha512-BiUdXLRhNh8Nkbag3sLmOLbyjL6w1Kt1Z/D3T97sOhMWdvNj3mOODu4XADay3MJ/NmhAd5AKKwAGF3NKNe8wWg==
dependencies:
sharec "^2.12.1-beta"