Comparing version 0.0.4 to 1.0.0
@@ -0,1 +1,11 @@ | ||
# 1.0.0 - August 24, 2021 | ||
- Switch from `crypto.pseudoRandomBytes()` back to `crypto.randomBytes()` (Both are equivalent in Node 4.x and above, and `pseudoRandomBytes()` is deprecated in Node 11.x and above) | ||
- Add `engines` field to _package.json_ | ||
- Fix character distribution non-uniformity issue by adding back `-` and `_` to generated ids | ||
- Add character distribution test | ||
- Enable strict mode | ||
- Fix JSDoc optional parameter syntax | ||
- Add `Promise`-based API | ||
# 0.0.4 - August 24, 2021 | ||
@@ -6,3 +16,3 @@ | ||
- Add `license` field to _package.json_ | ||
- Removed unused var, added param documentation | ||
- Remove unused var, added param documentation | ||
@@ -9,0 +19,0 @@ # 0.0.3 - July 6, 2013 |
25
index.js
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
/** | ||
@@ -8,6 +9,6 @@ * Module dependencies | ||
/** | ||
* 62 characters in the ascii range that can be used in URLs without special | ||
* 64 characters in the ascii range that can be used in URLs without special | ||
* encoding. | ||
*/ | ||
var UIDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
var UIDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; | ||
@@ -22,10 +23,9 @@ /** | ||
function tostr(bytes) { | ||
var r, i; | ||
var r = '', i; | ||
r = []; | ||
for (i = 0; i < bytes.length; i++) { | ||
r.push(UIDCHARS[bytes[i] % UIDCHARS.length]); | ||
r += UIDCHARS[bytes[i] % 64]; | ||
} | ||
return r.join(''); | ||
return r; | ||
} | ||
@@ -37,3 +37,3 @@ | ||
* @param {Number} length The number of chars of the uid | ||
* @param {Number} cb (optional) Callback for async uid generation | ||
* @param {Number} [cb] Callback for async uid generation | ||
* @api public | ||
@@ -43,10 +43,9 @@ */ | ||
function uid(length, cb) { | ||
if (typeof cb === 'undefined') { | ||
return tostr(crypto.pseudoRandomBytes(length)); | ||
return tostr(crypto.randomBytes(length)); | ||
} else { | ||
crypto.pseudoRandomBytes(length, function(err, bytes) { | ||
if (err) return cb(err); | ||
cb(null, tostr(bytes)); | ||
}) | ||
crypto.randomBytes(length, function (err, bytes) { | ||
if (err) return cb(err); | ||
cb(null, tostr(bytes)); | ||
}); | ||
} | ||
@@ -53,0 +52,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"tags": ["uid"], | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"repository": { | ||
@@ -11,4 +11,10 @@ "type": "git", | ||
}, | ||
"engines": { | ||
"node": ">= 4.0.0" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"dependencies": {} | ||
} | ||
} |
@@ -17,4 +17,6 @@ # uid2 | ||
```js | ||
uid(10) | ||
// => "hbswt489ts" | ||
const uid = require('uid2'); | ||
const id = uid(10); | ||
// id => "hbswt489ts" | ||
``` | ||
@@ -25,2 +27,4 @@ | ||
```js | ||
const uid = require('uid2'); | ||
uid(10, function (err, id) { | ||
@@ -32,4 +36,15 @@ if (err) throw err; | ||
Imported via `uid2/promises` it returns a `Promise`: | ||
```js | ||
const uid = require('uid2/promises'); | ||
async function foo() { | ||
const id = await uid(10); | ||
// id => "hbswt489ts" | ||
} | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5573
7
92
1
48