Skip to content

Commit

Permalink
Add maxLength property to translators
Browse files Browse the repository at this point in the history
  • Loading branch information
oculus42 committed Sep 7, 2020
1 parent 6627003 commit ac80a5d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.0.3] - 2020.-9-07
## [4.1.0] - 2020-09-07
### Added
- maxLength property to translators

### Changed
- Translator objects are now frozen

## [4.0.3] - 2020-09-07
### Changed
- Fixed default generate length inconsistency identified by [thadeucity](https://github.com/thadeucity)
- Added additional test.
- Added additional test

## [4.0.2] - 2020-09-07
### Changed
Expand Down
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Generate and translate standard UUIDs into shorter - or just *different* - formats and back.

## v4.0.3
## v4.1.0

### Major Changes in 4.0.0

Expand Down Expand Up @@ -63,14 +63,17 @@ translator.uuid(); // 3023b0f5-ec55-4e75-9cd8-104700698052
// See the alphabet used by a translator
translator.alphabet;

// The maximum length a translated uuid will be with its alphabet.
translator.maxLength;

// View the constants
short.constants.flickrBase58; // Avoids similar characters (0/O, 1/I/l, etc.)
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.
short-uuid 4.0.0 adds support for options when creating a translator.
This will support additional configuration in the future.

```javascript
Expand All @@ -92,8 +95,10 @@ translator.new(); // mhvXdrZT4jP5T8vBxuvm75
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.
short-uuid [3.1.1](https://github.com/oculus42/short-uuid/blob/v3.1.1/README.md)
and lower is confirmed to work on Node 0.10.x and later,
and browsers with a precompiled library proposed
by [voronianski](https://github.com/voronianski).

## Notes

Expand All @@ -104,11 +109,10 @@ TypeScript definitions are included, thanks to
[alexturek](https://github.com/alexturek).

## Recent Release Notes

4.0.0 adds consistent length translation and throws an error if provided an invalid alphabet.
4.1.0 adds a maxLength value to translators for reference
4.0.3 fixes default generate
4.0.1 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. Last Browserify distribution version temporarily.
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.

Please see [Revisions](revisions.md) for information on previous versions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ module.exports = (() => {
throw new Error('The provided Alphabet has duplicate characters resulting in unreliable results');
}

const shortIdLength = getShortIdLength(useAlphabet.length);

// Padding Params
const paddingParams = {
shortIdLength,
consistentLength: selectedOptions.consistentLength,
shortIdLength: getShortIdLength(useAlphabet.length),
paddingChar: useAlphabet[0],
};

Expand All @@ -89,14 +91,19 @@ module.exports = (() => {
const toHex = anyBase(useAlphabet, anyBase.HEX);
const generate = () => shortenUUID(uuidV4(), fromHex, paddingParams);

return {
const translator = {
new: generate,
generate,
uuid: uuidV4,
fromUUID: (uuid) => shortenUUID(uuid, fromHex, paddingParams),
toUUID: (shortUuid) => enlargeUUID(shortUuid, toHex),
alphabet: useAlphabet,
maxLength: shortIdLength,
};

Object.freeze(translator);

return translator;
};

// Expose the constants for other purposes.
Expand Down
7 changes: 4 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const cycle = (testcb) => {
};

test('short-uuid setup', (t) => {
t.plan(6);
t.plan(7);
let b90test;

t.ok(typeof short === 'function', 'should be a constructor function');
Expand All @@ -36,6 +36,7 @@ test('short-uuid setup', (t) => {
}, 'does not throw error with no options');

t.equal(b58default.alphabet, short.constants.flickrBase58, 'Default provides the flickrBase58 alphabet');
t.equal(b58default.maxLength, 22, 'Translators provide maxLength');

const new58short = b58default.new();
const new58long = b58default.toUUID(new58short);
Expand Down Expand Up @@ -251,7 +252,7 @@ test('generate should generate an ID with the Flickr set', (t) => {
t.ok(val2, 'Generate should reuse the default translator successfully');
});

test('quantity tests', (t) => {
test('Default generate quantity tests', (t) => {
t.plan(1);
let underLength = 0;
for (let i = 0; i < 10000; i += 1) {
Expand All @@ -261,5 +262,5 @@ test('quantity tests', (t) => {
}
}

t.equal(underLength, 0, 'ensure default is padded to');
t.equal(underLength, 0, 'Ensure default is padded');
});

0 comments on commit ac80a5d

Please sign in to comment.