nanoid
Advanced tools
Comparing version 0.1.1 to 0.2.0
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 0.2 | ||
* Add `size` argument to `nanoid()`. | ||
* Improve performance by 50%. | ||
* Reduce library size by 26% (by Vsevolod Rodionov and Oleg Mokhov). | ||
## 0.1.1 | ||
@@ -5,0 +10,0 @@ * Reduce library size by 5%. |
@@ -0,1 +1,3 @@ | ||
var masks = [15, 31, 63, 127, 255] | ||
/** | ||
@@ -27,11 +29,13 @@ * Secure random string generator with custom alphabet. | ||
module.exports = function (random, alphabet, size) { | ||
var step = Math.ceil(310 / alphabet.length * size) | ||
var mask = masks.find(function (i) { | ||
return i >= alphabet.length - 1 | ||
}) | ||
var step = Math.ceil(1.6 * mask * size / alphabet.length) | ||
var bytes, byte | ||
var id = '' | ||
while (true) { | ||
bytes = random(step) | ||
for (var i = 0; i < bytes.length; i++) { | ||
byte = bytes[i] | ||
if (byte < alphabet.length) { | ||
var bytes = random(step) | ||
for (var i = 0; i < step; i++) { | ||
var byte = bytes[i] & mask | ||
if (alphabet[byte]) { | ||
id += alphabet[byte] | ||
@@ -38,0 +42,0 @@ if (id.length === size) return id |
21
index.js
@@ -1,10 +0,15 @@ | ||
var format = require('./format') | ||
var random = require('./random') | ||
var url = require('./url') | ||
var url = '_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
/** | ||
* Generate secure URL-friendly unique ID. | ||
* | ||
* @return {string} Random string with 22 URL-friendly symbols. | ||
* By default, ID will have 22 symbols to have same collisions probability | ||
* as UUID v4. | ||
* | ||
* @param {number} [size=22] The number of symbols in ID. | ||
* | ||
* @return {string} Random string. | ||
* | ||
* @example | ||
@@ -16,4 +21,10 @@ * var nanoid = require('nanoid') | ||
*/ | ||
module.exports = function () { | ||
return format(random, url, 22) | ||
module.exports = function (size) { | ||
size = size || 22 | ||
var id = '' | ||
var bytes = random(size) | ||
for (var i = 0; i < size; i++) { | ||
id += url[bytes[i] & 63] | ||
} | ||
return id | ||
} |
{ | ||
"name": "nanoid", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A tiny, secure URL-friendly unique string ID generator", | ||
@@ -18,2 +18,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"chalk": "^2.1.0", | ||
"docdash": "^0.4.0", | ||
@@ -34,4 +36,7 @@ "eslint": "^4.4.1", | ||
"lint-staged": "^4.0.3", | ||
"microtime": "^2.1.6", | ||
"pre-commit": "^1.2.2", | ||
"size-limit": "^0.8.1", | ||
"shortid": "^2.2.8", | ||
"size-limit": "^0.8.2", | ||
"uuid": "^3.1.0", | ||
"webpack-dev-server": "^2.7.1", | ||
@@ -61,3 +66,7 @@ "yaspeller-ci": "^0.6.0" | ||
"path": "index.js", | ||
"limit": "246 B" | ||
"limit": "181 B" | ||
}, | ||
{ | ||
"path": "generate.js", | ||
"limit": "197 B" | ||
} | ||
@@ -64,0 +73,0 @@ ], |
@@ -13,3 +13,3 @@ # Nano ID | ||
**Small.** Only 246 bytes (minified and gzipped). No dependencies. | ||
**Small.** Only 181 bytes (minified and gzipped). No dependencies. | ||
It uses [Size Limit] to control size. | ||
@@ -67,3 +67,3 @@ | ||
2. Code of Nano ID has 2 times smaller size compare to `uuid/v4` package: | ||
246 bytes instead of 435. | ||
181 bytes instead of 435. | ||
@@ -75,3 +75,3 @@ ## Usage | ||
The main module uses URL-friendly symbols (`A-Za-z0-9_~`) and returns an ID | ||
with 22 characters (to have the same uniqueness as UUID v4). | ||
with 22 characters (to have the same collisions probability as UUID v4). | ||
@@ -86,2 +86,9 @@ ```js | ||
If you want to reduce ID length (and increase collisions probability), | ||
you can pass length as argument: | ||
```js | ||
nanoid(10) //=> "IRFa~VaY2b" | ||
``` | ||
### Custom Alphabet or Length | ||
@@ -97,10 +104,4 @@ | ||
If you want to use the same URL-friendly symbols and just change the length, | ||
you can get default alphabet from the `url` module: | ||
Alphabet must contain less than 256 symbols. | ||
```js | ||
var url = require('nanoid/url') | ||
model.id = generate(url, 10) //=> "Uakgb_J5m9" | ||
``` | ||
### Custom Random Bytes Generator | ||
@@ -125,1 +126,9 @@ | ||
with random numbers. | ||
If you want to use the same URL-friendly symbols with `format`, | ||
you can get default alphabet from the `url` module: | ||
```js | ||
var url = require('nanoid/url') | ||
format(random, url, 10) //=> "93ce_Ltuub" | ||
``` |
10050
119
129
24