Socket
Socket
Sign inDemoInstall

short-uuid

Package Overview
Dependencies
2
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 2.0.0

CHANGELOG.md

102

index.js

@@ -5,50 +5,82 @@ /**

*/
module.exports = (function(){
var anyBase = require('any-base');
var anyBase = require('any-base');
var uuid = require('node-uuid');
var flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
var cookieBase90 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~";
var flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
var cookieBase90 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~";
function shortenUUID (longId, translator) {
return translator(longId.replace(/-/g,''));
}
/**
* Takes a UUID, strips the dashes, and translates.
* @param {string} longId
* @param {function(string)} translator
* @returns {string}
*/
function shortenUUID (longId, translator) {
return translator(longId.replace(/-/g,''));
}
function enlargeUUID(shortId, translator) {
var uu1 = translator(shortId);
var uuReg = /(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/;
/**
* Translate back to hex and turn back into UUID format, with dashes
* @param {string} shortId
* @param {function(string)} translator
* @returns {string}
*/
function enlargeUUID(shortId, translator) {
var uu1 = translator(shortId);
var leftPad = "";
var m;
var leftPad = "";
// 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";
}
// 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
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('-');
}
var uuPad = leftPad + uu1;
/**
* @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) {
var m = uuPad.match(uuReg);
// Default to Flickr 58
var useAlphabet = toAlphabet || flickrBase58;
return [m[1], m[2], m[3], m[4], m[5]].join('-');
}
// UUIDs are in hex, so we translate to and from.
var fromHex = anyBase(anyBase.HEX, useAlphabet);
var toHex = anyBase(useAlphabet, anyBase.HEX);
function MakeConvertor(toAlphabet) {
return {
new: function() { return shortenUUID(uuid.v4(), fromHex); },
uuid: uuid.v4,
fromUUID: function(uuid) { return shortenUUID(uuid, fromHex); },
toUUID: function(shortUuid) { return enlargeUUID(shortUuid, toHex); },
alphabet: useAlphabet
};
}
var fromHex = anyBase(anyBase.HEX, toAlphabet);
var toHex = anyBase(toAlphabet, anyBase.HEX);
return {
fromUUID: function(uuid) { return shortenUUID(uuid, fromHex); },
toUUID: function(shortUuid) { return enlargeUUID(shortUuid, toHex); },
fromHex: fromHex,
toHex: toHex
// Expose the constants for other purposes.
MakeConvertor.constants = {
flickrBase58: flickrBase58,
cookieBase90: cookieBase90
};
}
module.exports = {
new: MakeConvertor,
b58: MakeConvertor(flickrBase58),
constants: {
flickrBase58: flickrBase58,
cookieBase90: cookieBase90
}
};
// Expose the generic v4 UUID generator for convenience
MakeConvertor.uuid = uuid.v4;
return MakeConvertor;
}());
{
"name": "short-uuid",
"version": "1.0.0",
"description": "Translate standard UUID to shorter formats and back.",
"version": "2.0.0",
"description": "Create and translate standard UUIDs with shorter formats.",
"main": "index.js",

@@ -25,3 +25,4 @@ "scripts": {

"dependencies": {
"any-base": "^1.0.0"
"any-base": "^1.0.0",
"node-uuid": "^1.4.7"
},

@@ -28,0 +29,0 @@ "devDependencies": {

@@ -5,23 +5,39 @@ # 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)
Translate standard UUIDs into shorter formats and back.
Generate and translate standard UUIDs into shorter - or just *different* - formats and back.
var shortUUID = require('short-uuid')
## v2.0.0
short-uuid returns an object with a property "new". This is a constructor that takes a base as a parameter and returns to/from functions:
2.0 is a major rework to make the library more capable and useful. It now provides RFC4122 v4-compliant UUIDs,
thanks to [`node-uuid`](https://github.com/broofa/node-uuid).
{
fromUUID: function(){..},
toUUID: function(){..}
fromHex: function(){..},
toHex: function(){..},
}
The 'UUID' Functions handle the dashes and left-filled zeros needed for proper UUID support. The 'Hex' functions provide the translation functions from `any-base`.
```javascript
var 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
The short-uuid object also incldues a `constants` object, with base58 and base90 sets.
// Generate a shortened v4 UUID
translator.new();
* The base58 set `flickrBase58` originates with Flickr and reduces human transcription errors.
* The base90 set `cookieBase90` is the set of allowable characters in a cookie value, and is intended to provide the shortest storage of UUIDs in a browser cookie.
// Generate plain UUIDs
short.uuid(); // From the constructor without creating a translator
translator.uuid(); // Each translator provides the uuid.v4() function
Finally, the Base58 translator is automatically generated and included as `b58` for convenience.
// Translate UUIDs
translator.toUUID(shortId);
translator.from UUID(regularUUID);
// See the alphabet used by a translator
translator.alphabet
// View the constants
short.constants.flickrBase58;
short.constants.cookieBase90;
```
v2.0.0 is around 700 bytes when compressed with UglifyJS2 (702) or Closure Compiler (714 on Simple, 642 on Advanced).
Please see [Revisions](revisions.md) for information on previous versions.

@@ -7,21 +7,33 @@ /**

var short = require('../index');
var uuid = require('node-uuid');
var b90;
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;
describe('short-uuid', function(){
describe('new', function(){
it('should be a constructor function', function(){
it('should contain a "new" function', function(){
var b90;
assert.ok(typeof short === 'function');
var b90;
assert.ok(short.hasOwnProperty('new') && typeof short.new === 'function');
assert.doesNotThrow(function(){
b90 = short(short.constants.cookieBase90);
}, "Calling does not throw an error");
assert.doesNotThrow(function(){
b90 = short.new(short.constants.cookieBase90);
}, "Calling new does not throw an error");
assert.equal(typeof b90, 'object', "constructor returns an object");
});
assert.equal(typeof b90, 'object', "new returns an object");
it('should use the b58 argument as default', function(){
var b58default;
assert.doesNotThrow(function(){
b58default = short();
});
assert.equal(b58default.alphabet, short.constants.flickrBase58, 'Default provides the flickrBase58 alphabet');
var new58short = b58default.new();
var new58long = b58default.toUUID(new58short);
assert.ok(validUUIDRegex.test(new58long), 'default produces valid output');
});

@@ -43,4 +55,4 @@

var b90 = short.new(short.constants.cookieBase90);
var b58 = short.b58;
var b90 = short(short.constants.cookieBase90);
var b58 = short(short.constants.flickrBase58);

@@ -50,4 +62,8 @@ var uu, f58, f90;

for (var i = 0; i < 10; i++) {
uu = uuid.v4();
uu = short.uuid();
it('should generate valid UUIDs', function(){
assert.ok(validUUIDRegex.test(uu), 'UUID is valid');
});
f58 = b58.fromUUID(uu);

@@ -59,5 +75,11 @@ f90 = b90.fromUUID(uu);

assert.equal(b58.toUUID(f58), uu, 'Translated b58 matches original');
assert.ok(validUUIDRegex.test(b58.toUUID(f58)), 'Translated UUID is valid');
assert.equal(b90.toUUID(f90), uu, 'Translated b90 matches original');
assert.ok(validUUIDRegex.test(b90.toUUID(f90)), 'Translated UUID is valid');
});
it('should return a standard v4 uuid from instance.uuid()', function(){
assert.ok(validUUIDRegex.test(b58.uuid()), '.uuid() is a valid UUID');
});
}

@@ -67,2 +89,4 @@

var someZeros = '00000000-a70c-4ebd-8f2b-540f7e709092';
// Support even invalid UUIDs, for completeness
var allZeros = '00000000-0000-0000-0000-000000000000';

@@ -78,3 +102,19 @@

});
describe('new', function(){
it('should create a shortened UUID', function(){
var b58 = short(short.constants.flickrBase58);
var shorter = b58.new();
var expanded = b58.toUUID(shorter);
var shortened = b58.fromUUID(expanded);
assert.equal(shorter, shortened, 'Generated Short ID is the same as re-shortened ID');
assert.ok(validUUIDRegex.test(expanded), 'UUID is valid');
})
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc