short-uuid
Advanced tools
Comparing version 3.1.1 to 4.0.1
@@ -5,2 +5,32 @@ # Change Log | ||
## [4.0.1] - 2020-09-06 | ||
### Changed | ||
- Fixed default `.generate()` to use padStart correctly. | ||
## [4.0.0] - 2020-09-06 (Unreleased) | ||
### MAJOR CHANGES | ||
- 🛑 short-uuid will now throw an error when provided an alphabet with duplicate characters. Duplicate characters will cause translation errors. | ||
- ℹ️ 4.0.0 is written in modern ECMAScript. It uses features through ES9/ES2018. | ||
- ℹ️ 4.x does not yet include the pre-built version for browsers. If needed, continue to use 3.1.1 in the meantime. | ||
- ℹ️ By default, short-uuid will pad shortened IDs to a consistent length. | ||
- Padding does not affect translation, and the values are compatible with previous releases. | ||
- Padding can be disabled with the option `consistentLength: false` when instantiating a translator. This is consistent with previous versions. | ||
### Added | ||
- eslint rules | ||
- tape testing library | ||
### Changed | ||
- Merged PR #44 from [qgerome](https://github.com/qgerome) to fix uuid version deprecation. | ||
- Merged PR #47 from [thadeucity](https://github.com/thadeucity) to resolve #39 for consistent length. | ||
- Switched tests to tape | ||
- Updated to ES6 | ||
- Switched from Greenkeeper to Snyk.io | ||
- Dropped support for Node prior to 8.x | ||
- Updated link to uuid repo in Notes in [README.md] | ||
### Removed | ||
- Removed browserify, grunt, & mocha | ||
- Removed built version for browser | ||
## [3.1.1] - 2019-05-01 | ||
@@ -7,0 +37,0 @@ ### Changed |
declare module 'short-uuid' { | ||
function shortUUID(alphabet?: string): shortUUID.Translator; | ||
interface Options{ | ||
consistentLength?: boolean; | ||
} | ||
function shortUUID(alphabet?: string, options?:Options): shortUUID.Translator; | ||
namespace shortUUID { | ||
@@ -5,0 +9,0 @@ export const constants: { |
145
index.js
@@ -6,10 +6,15 @@ /** | ||
var anyBase = require('any-base'); | ||
var uuidV4 = require('uuid/v4'); | ||
const anyBase = require('any-base'); | ||
const uuidV4 = require('uuid').v4; | ||
var flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; | ||
var cookieBase90 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~"; | ||
const flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; | ||
const cookieBase90 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~"; | ||
var toFlickr; | ||
const baseOptions = { | ||
consistentLength: true, | ||
}; | ||
// A default generator, instantiated only if used. | ||
let toFlickr; | ||
/** | ||
@@ -19,6 +24,14 @@ * Takes a UUID, strips the dashes, and translates. | ||
* @param {function(string)} translator | ||
* @param {Object} [paddingParams] | ||
* @returns {string} | ||
*/ | ||
function shortenUUID (longId, translator) { | ||
return translator(longId.toLowerCase().replace(/-/g,'')); | ||
const shortenUUID = (longId, translator, paddingParams) => { | ||
const translated = translator(longId.toLowerCase().replace(/-/g, "")); | ||
if (!paddingParams || !paddingParams.consistentLength) return translated; | ||
return translated.padStart( | ||
paddingParams.shortIdLength, | ||
paddingParams.paddingChar | ||
); | ||
} | ||
@@ -32,69 +45,83 @@ | ||
*/ | ||
function enlargeUUID(shortId, translator) { | ||
var uu1 = translator(shortId); | ||
var leftPad = ""; | ||
var m; | ||
const enlargeUUID = (shortId, translator) => { | ||
const uu1 = translator(shortId).padStart(32, '0'); | ||
// Pad out UUIDs beginning with zeros (any number shorter than 32 characters of hex) | ||
for (var i = 0, len = 32-uu1.length; i < len; ++i) { | ||
leftPad += "0"; | ||
} | ||
// Join the zero padding and the UUID and then slice it up with match | ||
const m = uu1.match(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/); | ||
// Join the zero padding and the UUID and then slice it up with match | ||
m = (leftPad + uu1).match(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/); | ||
// Accumulate the matches and join them. | ||
return [m[1], m[2], m[3], m[4], m[5]].join('-'); | ||
} | ||
// Accumulate the matches and join them. | ||
return [m[1], m[2], m[3], m[4], m[5]].join('-'); | ||
// Calculate length for the shortened ID | ||
const getShortIdLength = (alphabetLength) => { | ||
return Math.ceil(Math.log(2 ** 128) / Math.log(alphabetLength)); | ||
} | ||
module.exports = (function(){ | ||
module.exports = (() => { | ||
/** | ||
* @constructor | ||
* @param {string?} toAlphabet - Defaults to flickrBase58 if not provided | ||
* @returns {{new: (function()), | ||
* uuid: (function()), | ||
* fromUUID: (function(string)), | ||
* toUUID: (function(string)), | ||
* alphabet: (string)}} | ||
*/ | ||
function MakeConvertor(toAlphabet) { | ||
/** | ||
* @param {string} toAlphabet - Defaults to flickrBase58 if not provided | ||
* @param {Object} [options] | ||
* | ||
* @returns {{new: (function()), | ||
* uuid: (function()), | ||
* fromUUID: (function(string)), | ||
* toUUID: (function(string)), | ||
* alphabet: (string)}} | ||
*/ | ||
const makeConvertor = (toAlphabet, options) => { | ||
// Default to Flickr 58 | ||
var useAlphabet = toAlphabet || flickrBase58; | ||
// Default to Flickr 58 | ||
const useAlphabet = toAlphabet || flickrBase58; | ||
// UUIDs are in hex, so we translate to and from. | ||
var fromHex = anyBase(anyBase.HEX, useAlphabet); | ||
var toHex = anyBase(useAlphabet, anyBase.HEX); | ||
var generate = function() { return shortenUUID(uuidV4(), fromHex); }; | ||
// Default to baseOptions | ||
const selectedOptions = {...baseOptions, ...options}; | ||
return { | ||
new: generate, | ||
generate: generate, | ||
uuid: uuidV4, | ||
fromUUID: function(uuid) { return shortenUUID(uuid, fromHex); }, | ||
toUUID: function(shortUuid) { return enlargeUUID(shortUuid, toHex); }, | ||
alphabet: useAlphabet | ||
}; | ||
// Check alphabet for duplicate entries | ||
if ([...new Set(Array.from(useAlphabet))].length !== useAlphabet.length) { | ||
throw new Error('The provided Alphabet has duplicate characters resulting in unreliable results'); | ||
} | ||
// Expose the constants for other purposes. | ||
MakeConvertor.constants = { | ||
flickrBase58: flickrBase58, | ||
cookieBase90: cookieBase90 | ||
// Padding Params | ||
const paddingParams = { | ||
consistentLength: selectedOptions.consistentLength, | ||
shortIdLength: getShortIdLength(useAlphabet.length), | ||
paddingChar: useAlphabet[0], | ||
}; | ||
// Expose the generic v4 UUID generator for convenience | ||
MakeConvertor.uuid = uuidV4; | ||
// UUIDs are in hex, so we translate to and from. | ||
const fromHex = anyBase(anyBase.HEX, useAlphabet); | ||
const toHex = anyBase(useAlphabet, anyBase.HEX); | ||
const generate = () => shortenUUID(uuidV4(), fromHex, paddingParams); | ||
// Provide a generic generator | ||
MakeConvertor.generate = function() { | ||
if (!toFlickr) { | ||
// Generate on first use; | ||
toFlickr = anyBase(anyBase.HEX, flickrBase58); | ||
} | ||
return shortenUUID(uuidV4(), toFlickr); | ||
return { | ||
new: generate, | ||
generate: generate, | ||
uuid: uuidV4, | ||
fromUUID: (uuid) => shortenUUID(uuid, fromHex, paddingParams), | ||
toUUID: (shortUuid) => enlargeUUID(shortUuid, toHex), | ||
alphabet: useAlphabet | ||
}; | ||
} | ||
return MakeConvertor; | ||
}()); | ||
// Expose the constants for other purposes. | ||
makeConvertor.constants = { | ||
flickrBase58: flickrBase58, | ||
cookieBase90: cookieBase90 | ||
}; | ||
// Expose the generic v4 UUID generator for convenience | ||
makeConvertor.uuid = uuidV4; | ||
// Provide a generic generator | ||
makeConvertor.generate = () => { | ||
if (!toFlickr) { | ||
// Generate on first use; | ||
toFlickr = makeConvertor(flickrBase58).generate; | ||
} | ||
return shortenUUID(uuidV4(), toFlickr); | ||
}; | ||
return makeConvertor; | ||
})(); |
{ | ||
"name": "short-uuid", | ||
"version": "3.1.1", | ||
"version": "4.0.1", | ||
"description": "Create and translate standard UUIDs with shorter formats.", | ||
@@ -8,8 +8,11 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "snyk test && grunt test", | ||
"cover": "grunt cover", | ||
"build": "grunt build", | ||
"coverage": "nyc --reporter=lcov --reporter=text-summary npm run test", | ||
"test": "npx tape test/**/*.js", | ||
"snyk-test": "snyk test", | ||
"snyk-protect": "snyk protect", | ||
"prepublishOnly": "npm run snyk-protect" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"repository": { | ||
@@ -33,16 +36,13 @@ "type": "git", | ||
"any-base": "^1.1.0", | ||
"uuid": "^3.3.2" | ||
"uuid": "^8.3.0" | ||
}, | ||
"devDependencies": { | ||
"grunt": "^1.0.4", | ||
"grunt-browserify": "^5.3.0", | ||
"grunt-contrib-uglify": "^4.0.1", | ||
"grunt-mkdir": "^1.0.0", | ||
"grunt-mocha-nyc": "^1.0.3", | ||
"grunt-simple-mocha": "^0.4.1", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.0.0", | ||
"snyk": "^1.157.1" | ||
"eslint": "^7.8.1", | ||
"eslint-config-airbnb-base": "^14.2.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"nyc": "^15.0.1", | ||
"snyk": "^1.330.2", | ||
"tape": "^5.0.1" | ||
}, | ||
"snyk": true | ||
} |
# short-uuid | ||
[![npm](https://img.shields.io/npm/v/short-uuid.svg)](https://www.npmjs.com/package/short-uuid) | ||
[![Build Status](https://travis-ci.org/oculus42/short-uuid.svg?branch=master)](https://travis-ci.org/oculus42/short-uuid) | ||
[![Code Climate](https://codeclimate.com/github/oculus42/short-uuid/badges/gpa.svg)](https://codeclimate.com/github/oculus42/short-uuid) | ||
[![Test Coverage](https://codeclimate.com/github/oculus42/short-uuid/badges/coverage.svg)](https://codeclimate.com/github/oculus42/short-uuid/coverage) | ||
[![Dependencies](https://david-dm.org/oculus42/short-uuid.svg)](https://david-dm.org/oculus42/short-uuid) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/oculus42/short-uuid.svg)](https://greenkeeper.io/) | ||
[![npm](https://img.shields.io/npm/v/short-uuid.svg)](https://www.npmjs.com/package/short-uuid) | ||
[![Build Status](https://travis-ci.org/oculus42/short-uuid.svg?branch=master)](https://travis-ci.org/oculus42/short-uuid) | ||
[![Code Climate](https://codeclimate.com/github/oculus42/short-uuid/badges/gpa.svg)](https://codeclimate.com/github/oculus42/short-uuid) | ||
[![Test Coverage](https://codeclimate.com/github/oculus42/short-uuid/badges/coverage.svg)](https://codeclimate.com/github/oculus42/short-uuid/coverage) | ||
[![Dependencies](https://david-dm.org/oculus42/short-uuid.svg)](https://david-dm.org/oculus42/short-uuid) | ||
[![Known Vulnerabilities](https://snyk.io/test/oculus42/short-uuid/{repo}/badge.svg)](https://snyk.io/test/github/oculus42/short-uuid) | ||
Generate and translate standard UUIDs into shorter - or just *different* - formats and back. | ||
## v3.1.1 | ||
## v4.0.1 | ||
### Major Changes in 4.0.0 | ||
- 🛑 short-uuid will now throw an error when provided an alphabet with duplicate characters. Duplicate characters will cause translation errors. | ||
- ℹ️ 4.0.0 is written in modern ECMAScript. It uses features through ES9/ES2018. | ||
- ℹ️ 4.x does not yet include the pre-built version for browsers. If needed, continue to use 3.1.1 in the meantime. | ||
- ℹ️ By default, short-uuid will pad shortened IDs to a consistent length. | ||
- Padding does not affect translation, and the values are compatible with previous releases. | ||
- Padding can be disabled with the option `consistentLength: false` when instantiating a translator. This is consistent with previous versions. | ||
### Quick Start | ||
```javascript | ||
var short = require('short-uuid'); | ||
const short = require('short-uuid'); | ||
@@ -27,10 +36,14 @@ // Quick start with flickrBase58 format | ||
into other, usually shorter formats. It also provides translators | ||
to convert back and forth from RFC complaint UUIDs to the shorter formats. | ||
to convert back and forth from RFC compliant UUIDs to the shorter formats. | ||
As of 4.0.0, formats return consistent-length values unless specifically requested. | ||
This is done by padding the start with the first (`[0]`) character in the alphabet. | ||
Previous versions can translate padded formats back to UUID. | ||
```javascript | ||
var short = require('short-uuid'); | ||
const short = require('short-uuid'); | ||
var translator = short(); // Defaults to flickrBase58 | ||
var decimalTranslator = short("0123456789"); // Provide a specific alphabet for translation | ||
var cookieTranslator = short(short.constants.cookieBase90); // Use a constant for translation | ||
const translator = short(); // Defaults to flickrBase58 | ||
const decimalTranslator = short("0123456789"); // Provide a specific alphabet for translation | ||
const cookieTranslator = short(short.constants.cookieBase90); // Use a constant for translation | ||
@@ -42,3 +55,3 @@ // Generate a shortened v4 UUID | ||
// Translate UUIDs to and from the shortened format | ||
translator.toUUID(shortId);// a44521d0-0fb8-4ade-8002-3385545c3318 | ||
translator.toUUID(shortId); // a44521d0-0fb8-4ade-8002-3385545c3318 | ||
translator.fromUUID(regularUUID); // mhvXdrZT4jP5T8vBxuvm75 | ||
@@ -57,31 +70,48 @@ | ||
short.constants.flickrBase58; // Avoids similar characters (0/O, 1/I/l, etc.) | ||
short.constants.cookieBase90; // Safe for HTTP cookies values for smaller IDs. | ||
short.constants.cookieBase90; // Safe for HTTP cookies values for smaller IDs. | ||
``` | ||
### Options | ||
short-uuid 4.0.0 adds support for an options object when creating a translator. | ||
This will support additional configuration in the future. | ||
```javascript | ||
const short = require('short-uuid'); | ||
// By default shortened values are now padded for consistent length. | ||
// If you want to produce variable lengths, like in 3.1.1 | ||
const translator = short(short.constants.flickrBase58, { | ||
consistentLength: false, | ||
}); | ||
// Generate a shortened v4 UUID | ||
translator.new(); // mhvXdrZT4jP5T8vBxuvm75 | ||
``` | ||
* `consistentLength` - Controls padding on shortened values. Default is `true`. | ||
## Support | ||
short-uuid has been tested to work on Node 0.10.x and later. | ||
Builds run on Node 6.x and later. | ||
short-uuid 4.0.0 and later is confirmed to work on Node 8.x and later. | ||
- Pre-compiled browser version is planned for future release. | ||
short-uuid 3.1.1 and lower is confirmed to work on Node 0.10.x and later, | ||
and browsers. | ||
## Notes | ||
short-uuid provides RFC4122 v4-compliant UUIDs, | ||
thanks to [`uuid`](https://github.com/kelektiv/node-uuid). | ||
thanks to [`uuid`](https://github.com/uuidjs/uuid). | ||
It includes Browserify support for client-side use as proposed | ||
by [voronianski](https://github.com/voronianski), with compiled, | ||
browser-ready files in the npm package for convenience. | ||
The library is exposed as `ShortUUID`. | ||
TypeScript definitions are included, thanks to | ||
[alexturek](https://github.com/alexturek). | ||
short-uuid is under 1.2K when compressed. Using Browserify, the library and dependencies are ~3.6K. | ||
## Recent Release Notes | ||
4.0.0 adds consistent length translation and throws an error if provided an invalid alphabet. | ||
3.1.1 updated dev dependencies which required dropping Node 4.x build test. Browserify distribution version | ||
3.1.0 adds `generate()` for ease of use. Last version to build on Node 4.x. | ||
3.0.0 updates dependencies, includes a refactor for CodeClimate, and drops Node 0.x builds. | ||
2.3.4 corrects the behavior for UUIDs with uppercase letters. Last version to build on Node 0.x. | ||
3.0.0 updates dependencies, includes a refactor for CodeClimate, and drops Node 0.x builds. | ||
2.3.4 corrects the behavior for UUIDs with uppercase letters. Last version to build on Node 0.x. | ||
Please see [Revisions](revisions.md) for information on previous versions. |
@@ -5,175 +5,276 @@ /** | ||
var assert = require('assert'); | ||
var short = require('../index'); | ||
const test = require('tape'); | ||
const short = require('../index'); | ||
const uuid = require('uuid'); | ||
var validUUIDRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
test('short-uuid setup', (t) => { | ||
t.plan(6); | ||
let b90; | ||
describe('short-uuid', function(){ | ||
t.ok(typeof short === 'function', 'should be a constructor function'); | ||
it('should be a constructor function', function(){ | ||
t.doesNotThrow(() => { | ||
b90 = short(short.constants.cookieBase90); | ||
}, "Calling does not throw an error"); | ||
var b90; | ||
assert.ok(typeof short === 'function'); | ||
t.equal(typeof b90, 'object', "constructor returns an object"); | ||
assert.doesNotThrow(function(){ | ||
b90 = short(short.constants.cookieBase90); | ||
}, "Calling does not throw an error"); | ||
let b58default; | ||
assert.equal(typeof b90, 'object', "constructor returns an object"); | ||
}); | ||
t.doesNotThrow(() => { | ||
b58default = short(); | ||
}, 'does not throw error with no options'); | ||
it('should use the b58 argument as default', function(){ | ||
t.equal(b58default.alphabet, short.constants.flickrBase58, 'Default provides the flickrBase58 alphabet'); | ||
var b58default; | ||
const new58short = b58default.new(); | ||
const new58long = b58default.toUUID(new58short); | ||
assert.doesNotThrow(function(){ | ||
b58default = short(); | ||
}); | ||
t.ok(uuid.validate(new58long), 'default produces valid output'); | ||
}); | ||
assert.equal(b58default.alphabet, short.constants.flickrBase58, 'Default provides the flickrBase58 alphabet'); | ||
test('constants', (t) => { | ||
t.plan(3); | ||
t.ok(short.hasOwnProperty('constants') && typeof short.constants === 'object', 'should contain a "constants" object'); | ||
t.equal(short.constants.flickrBase58, '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', 'should contain flicker58 constant'); | ||
t.equal(short.constants.cookieBase90, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~", 'should contain cookie90 constant'); | ||
}); | ||
var new58short = b58default.new(); | ||
var new58long = b58default.toUUID(new58short); | ||
// Operations | ||
assert.ok(validUUIDRegex.test(new58long), 'default produces valid output'); | ||
}); | ||
test('should generate valid UUIDs', (t) => { | ||
t.plan(10); | ||
describe('constants', function(){ | ||
const b90 = short(short.constants.cookieBase90); | ||
const b58 = short(short.constants.flickrBase58); | ||
it('should contain a "constants" object', function(){ | ||
assert.ok(short.hasOwnProperty('constants') && typeof short.constants === 'object'); | ||
}); | ||
const cycle = (testcb) => { | ||
const uu = short.uuid(); | ||
const f58 = b58.fromUUID(uu); | ||
const f90 = b90.fromUUID(uu); | ||
it('should contain constant values', function(){ | ||
assert.equal(short.constants.flickrBase58, '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'); | ||
assert.equal(short.constants.cookieBase90, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~"); | ||
}); | ||
}); | ||
testcb(uu, f58, f90); | ||
}; | ||
describe('operation', function(){ | ||
const action = (uu) => { | ||
t.ok(uuid.validate(uu), 'UUID is valid'); | ||
}; | ||
var b90 = short(short.constants.cookieBase90); | ||
var b58 = short(short.constants.flickrBase58); | ||
for (let i = 0; i < 10; i += 1) { | ||
cycle(action); | ||
} | ||
}); | ||
var cycle = function(test) { | ||
uu = short.uuid(); | ||
test('should translate back from multiple bases', (t) => { | ||
t.plan(40); | ||
it('should generate valid UUIDs', function(){ | ||
assert.ok(validUUIDRegex.test(uu), 'UUID is valid'); | ||
}); | ||
const b90 = short(short.constants.cookieBase90); | ||
const b58 = short(short.constants.flickrBase58); | ||
f58 = b58.fromUUID(uu); | ||
f90 = b90.fromUUID(uu); | ||
const cycle = (testcb) => { | ||
const uu = short.uuid(); | ||
const f58 = b58.fromUUID(uu); | ||
const f90 = b90.fromUUID(uu); | ||
test(); | ||
}; | ||
testcb(uu, f58, f90); | ||
}; | ||
const action = function (uu, f58, f90) { | ||
t.equal(b58.toUUID(f58), uu, 'Translated b58 matches original'); | ||
t.ok(uuid.validate(b58.toUUID(f58)), 'Translated UUID is valid'); | ||
var uu, f58, f90, i, action; | ||
t.equal(b90.toUUID(f90), uu, 'Translated b90 matches original'); | ||
t.ok(uuid.validate(b90.toUUID(f90)), 'Translated UUID is valid'); | ||
}; | ||
it('should generate valid UUIDs', function(){ | ||
for (let i = 0; i < 10; i += 1) { | ||
cycle(action); | ||
} | ||
}); | ||
action = function() { | ||
assert.ok(validUUIDRegex.test(uu), 'UUID is valid'); | ||
}; | ||
test('should return a standard v4 uuid from instance.uuid()', (t) => { | ||
t.plan(10); | ||
for (i = 0; i < 10; i++) { | ||
cycle(action); | ||
} | ||
}); | ||
const b90 = short(short.constants.cookieBase90); | ||
const b58 = short(short.constants.flickrBase58); | ||
it('should translate back from multiple bases', function(){ | ||
const cycle = (testcb) => { | ||
const uu = short.uuid(); | ||
const f58 = b58.fromUUID(uu); | ||
const f90 = b90.fromUUID(uu); | ||
action = function() { | ||
assert.equal(b58.toUUID(f58), uu, 'Translated b58 matches original'); | ||
assert.ok(validUUIDRegex.test(b58.toUUID(f58)), 'Translated UUID is valid'); | ||
testcb(uu, f58, f90); | ||
}; | ||
assert.equal(b90.toUUID(f90), uu, 'Translated b90 matches original'); | ||
assert.ok(validUUIDRegex.test(b90.toUUID(f90)), 'Translated UUID is valid'); | ||
}; | ||
const action = function () { | ||
t.ok(uuid.validate(b58.uuid()), '.uuid() is a valid UUID'); | ||
}; | ||
for (i = 0; i < 10; i++) { | ||
cycle(action); | ||
} | ||
for (let i = 0; i < 10; i += 1) { | ||
cycle(action); | ||
} | ||
}); | ||
}); | ||
test('Handle UUIDs that begin with zeros', (t) => { | ||
t.plan(2); | ||
it('should return a standard v4 uuid from instance.uuid()', function(){ | ||
const b90 = short(short.constants.cookieBase90); | ||
const b58 = short(short.constants.flickrBase58); | ||
action = function() { | ||
assert.ok(validUUIDRegex.test(b58.uuid()), '.uuid() is a valid UUID'); | ||
}; | ||
const someZeros = '00000000-a70c-4ebd-8f2b-540f7e709092'; | ||
for (i = 0; i < 10; i++) { | ||
cycle(action); | ||
} | ||
}); | ||
it('should handle UUIDs that begin with zeros', function(){ | ||
var someZeros = '00000000-a70c-4ebd-8f2b-540f7e709092'; | ||
t.equal(someZeros, b58.toUUID(b58.fromUUID(someZeros)), 'Supports starting zeroes'); | ||
t.equal(someZeros, b90.toUUID(b90.fromUUID(someZeros)), 'Supports starting zeroes'); | ||
}); | ||
// Support even invalid UUIDs, for completeness | ||
var allZeros = '00000000-0000-0000-0000-000000000000'; | ||
test('Handle UUIDs with all zeros', (t) => { | ||
t.plan(2); | ||
assert.equal(someZeros, b58.toUUID(b58.fromUUID(someZeros)),'Supports starting zeroes'); | ||
assert.equal(someZeros, b90.toUUID(b90.fromUUID(someZeros)),'Supports starting zeroes'); | ||
const b90 = short(short.constants.cookieBase90); | ||
const b58 = short(short.constants.flickrBase58); | ||
// Support even invalid UUIDs, for completeness | ||
const allZeros = '00000000-0000-0000-0000-000000000000'; | ||
assert.equal(allZeros, b58.toUUID(b58.fromUUID(allZeros)),'Supports starting zeroes'); | ||
assert.equal(allZeros, b90.toUUID(b90.fromUUID(allZeros)),'Supports starting zeroes'); | ||
}); | ||
t.equal(allZeros, b58.toUUID(b58.fromUUID(allZeros)), 'Supports starting zeroes'); | ||
t.equal(allZeros, b90.toUUID(b90.fromUUID(allZeros)), 'Supports starting zeroes'); | ||
}); | ||
it('should handle UUID with uppercase letters', function(){ | ||
var uuidWithUpper = '00000013-0000-1000-8000-0026BB765291', | ||
uuidAllLower = uuidWithUpper.toLowerCase(), | ||
test('should handle UUID with uppercase letters', (t) => { | ||
t.plan(4); | ||
upperB58 = b58.fromUUID(uuidWithUpper), | ||
lowerB58 = b58.fromUUID(uuidAllLower), | ||
const b58 = short(short.constants.flickrBase58); | ||
upperBack = b58.toUUID(upperB58), | ||
lowerBack = b58.toUUID(lowerB58); | ||
const uuidWithUpper = '00000013-0000-1000-8000-0026BB765291' | ||
const uuidAllLower = uuidWithUpper.toLowerCase(); | ||
assert.equal(upperB58, lowerB58, 'Translates uppercase letters in UUIDs'); | ||
assert.equal(upperBack, lowerBack, 'Translates back to UUID correctly'); | ||
assert.equal(upperBack, uuidAllLower, 'From uppercase matches original lowercase'); | ||
assert.equal(lowerBack, uuidAllLower, 'From lower matches original lowercase'); | ||
}); | ||
const upperB58 = b58.fromUUID(uuidWithUpper); | ||
const lowerB58 = b58.fromUUID(uuidAllLower); | ||
}); | ||
describe('new', function(){ | ||
it('should create a shortened UUID', function(){ | ||
var b58 = short(short.constants.flickrBase58); | ||
const upperBack = b58.toUUID(upperB58); | ||
const lowerBack = b58.toUUID(lowerB58); | ||
var shorter = b58.new(); | ||
var expanded = b58.toUUID(shorter); | ||
var shortened = b58.fromUUID(expanded); | ||
t.equal(upperB58, lowerB58, 'Translates uppercase letters in UUIDs'); | ||
t.equal(upperBack, lowerBack, 'Translates back to UUID correctly'); | ||
t.equal(upperBack, uuidAllLower, 'From uppercase matches original lowercase'); | ||
t.equal(lowerBack, uuidAllLower, 'From lower matches original lowercase'); | ||
}); | ||
assert.equal(shorter, shortened, 'Generated Short ID is the same as re-shortened ID'); | ||
test('should not be able to use an Alphabet containing duplicated values', (t) => { | ||
t.plan(1); | ||
// Check if invalid alphabet throws error | ||
t.throws(() => short('001234567899aabcdef'), Error); | ||
}); | ||
assert.ok(validUUIDRegex.test(expanded), 'UUID is valid'); | ||
// Options | ||
test("should return consistent length shortened ids by default", (t) => { | ||
t.plan(3); | ||
}) | ||
const b58 = short(short.constants.flickrBase58); | ||
}); | ||
const uuidA = "01542709-aa56-ae25-5ad3-09237c6c3318"; | ||
const uuidB = "21b8b506-8cb2-79f1-89b3-d45c72ec3318"; | ||
const short58A = b58.fromUUID(uuidA); | ||
const short58B = b58.fromUUID(uuidB); | ||
const back58A = b58.toUUID(short58A); | ||
const back58B = b58.toUUID(short58B); | ||
describe('generate', function(){ | ||
it('should generate an ID with the Flickr set', function(){ | ||
var val = short.generate(); | ||
t.equal( | ||
short58A.length, | ||
short58B.length, | ||
"Translates to equal length string" | ||
); | ||
t.equal(back58A, uuidA, "Translates back to uuid"); | ||
t.equal(back58B, uuidB, "Translates back to uuid"); | ||
}); | ||
var b58 = short(short.constants.flickrBase58); | ||
test("should return consistent length shortened ids when flagged", (t) => { | ||
t.plan(3); | ||
var expanded = b58.toUUID(val); | ||
var shortened = b58.fromUUID(expanded); | ||
const b58 = short(short.constants.flickrBase58, { | ||
consistentLength: true, | ||
}); | ||
assert.equal(val, shortened, 'Generated Short ID is the same as re-shortened ID'); | ||
const uuidA = "01542709-aa56-ae25-5ad3-09237c6c3318"; | ||
const uuidB = "21b8b506-8cb2-79f1-89b3-d45c72ec3318"; | ||
const short58A = b58.fromUUID(uuidA); | ||
const short58B = b58.fromUUID(uuidB); | ||
const back58A = b58.toUUID(short58A); | ||
const back58B = b58.toUUID(short58B); | ||
assert.ok(validUUIDRegex.test(expanded), 'UUID is valid'); | ||
}); | ||
t.equal( | ||
short58A.length, | ||
short58B.length, | ||
"Translates to equal length string" | ||
); | ||
t.equal(back58A, uuidA, "Translates back to uuid"); | ||
t.equal(back58B, uuidB, "Translates back to uuid"); | ||
}); | ||
it('should reuse the translator', function() { | ||
var val = short.generate(); | ||
// No complex test, this is validating the second-time code path | ||
assert.ok(val); | ||
}) | ||
}); | ||
test("should return inconsistent length shortened ids when flagged", (t) => { | ||
t.plan(3); | ||
}); | ||
const b58 = short(short.constants.flickrBase58, { | ||
consistentLength: false, | ||
}); | ||
const uuidA = "01542709-aa56-ae25-5ad3-09237c6c3318"; | ||
const uuidB = "21b8b506-8cb2-79f1-89b3-d45c72ec3318"; | ||
const short58A = b58.fromUUID(uuidA); | ||
const short58B = b58.fromUUID(uuidB); | ||
const back58A = b58.toUUID(short58A); | ||
const back58B = b58.toUUID(short58B); | ||
t.notEqual( | ||
short58A.length, | ||
short58B.length, | ||
"Translates to equal length string" | ||
); | ||
t.equal(back58A, uuidA, "Translates back to uuid"); | ||
t.equal(back58B, uuidB, "Translates back to uuid"); | ||
}); | ||
test('padded and unpadded values should translate back consistently', (t) => { | ||
t.plan(4); | ||
const paddedShort = '12J9PLDMEfCf6da2LyAce5'; | ||
const unpaddedShort = '12J9PLDMEfCf6da2LyAce5'; | ||
const b58Padded = short(short.constants.flickrBase58, { | ||
consistentLength: true, | ||
}); | ||
const b58Vary = short(short.constants.flickrBase58, { | ||
consistentLength: false, | ||
}); | ||
t.equal(b58Padded.toUUID(paddedShort), b58Padded.toUUID(unpaddedShort), 'padded and unpadded provide the same uuid on a padded translator'); | ||
t.equal(b58Vary.toUUID(paddedShort), b58Vary.toUUID(unpaddedShort), 'padded and unpadded provide the same uuid on an unpadded translator'); | ||
t.equal(b58Padded.toUUID(paddedShort), b58Vary.toUUID(paddedShort), 'padded provides the same uuid on both translators'); | ||
t.equal(b58Padded.toUUID(unpaddedShort), b58Vary.toUUID(unpaddedShort), 'unpadded provides the same uuid on both translators'); | ||
}); | ||
test('new should create a shortened UUID', (t) => { | ||
t.plan(2); | ||
const b58 = short(short.constants.flickrBase58); | ||
const shorter = b58.new(); | ||
const expanded = b58.toUUID(shorter); | ||
const shortened = b58.fromUUID(expanded); | ||
t.equal(shorter, shortened, 'Generated Short ID is the same as re-shortened ID'); | ||
t.ok(uuid.validate(expanded), 'UUID is valid'); | ||
}); | ||
test('generate should generate an ID with the Flickr set', (t) => { | ||
t.plan(3); | ||
const val = short.generate(); | ||
const b58 = short(short.constants.flickrBase58); | ||
const expanded = b58.toUUID(val); | ||
const shortened = b58.fromUUID(expanded); | ||
t.equal(val, shortened, 'Generated Short ID is the same as re-shortened ID'); | ||
t.ok(uuid.validate(expanded), 'UUID is valid'); | ||
const val2 = short.generate(); | ||
t.ok(val, 'Generate should reuse the default translator successfully'); | ||
}); |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
6
0
115
0
54473
15
343
1
+ Addeduuid@8.3.2(transitive)
- Removeduuid@3.4.0(transitive)
Updateduuid@^8.3.0