Skip to content

Commit

Permalink
Merge pull request #24 from oculus42/housekeeping
Browse files Browse the repository at this point in the history
3.1.0 - add generate
  • Loading branch information
oculus42 authored Dec 8, 2018
2 parents df54d85 + 0139e13 commit 3110a86
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "11"
- "10"
- "9"
- "8"
- "6"
- "4"
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [3.0.0] - 2018-23-24
## [3.1.0] - 2018-12-08
### Changed
- Updated dev packages for vulnerability fixes
- Added top-level `generate` in [index.js]
- Added `generate` alias on translators in [index.js]
- Added tests for `generate`
- Updated [index.d.ts] definitions
- Update [README.md] to help clarify the functionality

## [3.0.1] - 2018-10-18
### Changed
- Build dependencies update

## [3.0.0] - 2018-03-24
### Changed
- Refactored export in [gruntfile.js] for CodeClimate
- Refactored export in [index.js] for CodeClimate
Expand Down
67 changes: 42 additions & 25 deletions README.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ declare module 'short-uuid' {
export const constants: {
flickrBase58: string;
cookieBase90: string;
}
};

/** Generate a new regular UUID. */
export function uuid(): string;

/** Generate a base 58 short uuid */
export function generate(): string;

export interface Translator {
/** The alphabet used for encoding UUIDs. */
alphabet: string;

/** Generate a new short UUID using this translator's alphabet. */
new: () => string;

/** Generate a new short UUID using this translator's alphabet. */
generate: () => string;

/** Generate a new regular UUID. */
uuid(): string;

Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var uuidV4 = require('uuid/v4');
var flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
var cookieBase90 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~";

var toFlickr;

/**
* Takes a UUID, strips the dashes, and translates.
* @param {string} longId
Expand Down Expand Up @@ -61,9 +63,11 @@ module.exports = (function(){
// 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); };

return {
new: function() { return shortenUUID(uuidV4(), fromHex); },
new: generate,
generate: generate,
uuid: uuidV4,
fromUUID: function(uuid) { return shortenUUID(uuid, fromHex); },
toUUID: function(shortUuid) { return enlargeUUID(shortUuid, toHex); },
Expand All @@ -80,5 +84,14 @@ module.exports = (function(){
// Expose the generic v4 UUID generator for convenience
MakeConvertor.uuid = uuidV4;

// Provide a generic generator
MakeConvertor.generate = function() {
if (!toFlickr) {
// Generate on first use;
toFlickr = anyBase(anyBase.HEX, flickrBase58);
}
return shortenUUID(uuidV4(), toFlickr);
};

return MakeConvertor;
}());
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "short-uuid",
"version": "3.0.1",
"version": "3.1.0",
"description": "Create and translate standard UUIDs with shorter formats.",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
70 changes: 69 additions & 1 deletion revisions.md

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,25 @@ describe('short-uuid', function(){

});

describe('generate', function(){
it('should generate an ID with the Flickr set', function(){
var val = short.generate();

var b58 = short(short.constants.flickrBase58);

var expanded = b58.toUUID(val);
var shortened = b58.fromUUID(expanded);

assert.equal(val, shortened, 'Generated Short ID is the same as re-shortened ID');

assert.ok(validUUIDRegex.test(expanded), 'UUID is valid');
});

it('should reuse the translator', function() {
var val = short.generate();
// No complex test, this is validating the second-time code path
assert.ok(val);
})
});

});

0 comments on commit 3110a86

Please sign in to comment.