uuid-encoder
Advanced tools
Comparing version 1.0.0 to 1.1.0
const bigInt = require('big-integer'); | ||
const knownBases = { | ||
base64: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/', | ||
base62: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | ||
base58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz', // Bitcoin base58 | ||
base36: '0123456789abcdefghijklmnopqrstuvwxyz', | ||
@@ -12,2 +15,13 @@ base32: '0123456789abcdefghjkmnpqrstvwxyz', // Crockford's base32 | ||
const caseSensitiveBases = { | ||
base62: true, | ||
base58: true, | ||
base36: false, | ||
base32: false, | ||
base16: false, | ||
base10: true, | ||
base2: true, | ||
}; | ||
class UuidEncoder { | ||
@@ -30,3 +44,5 @@ /** | ||
this.encStr = UuidEncoder.resolveEncodingStr(baseEncodingStr); | ||
this.isCaseSensitive = UuidEncoder.isCaseSensitiveBase(baseEncodingStr); | ||
this.base = this.encStr.length; | ||
} | ||
@@ -38,12 +54,19 @@ | ||
* @param {string} baseEncodingStr | ||
* @returns {string} | ||
*/ | ||
static resolveEncodingStr(baseEncodingStr) { | ||
if (knownBases[baseEncodingStr]) { | ||
return knownBases[baseEncodingStr]; | ||
} | ||
return Object.prototype.hasOwnProperty.call(knownBases, baseEncodingStr) ? | ||
knownBases[baseEncodingStr] : baseEncodingStr; | ||
} | ||
return baseEncodingStr; | ||
/** | ||
* @public | ||
* @param baseEncodingStr | ||
* @returns {boolean} | ||
*/ | ||
static isCaseSensitiveBase(baseEncodingStr) { | ||
return Object.prototype.hasOwnProperty.call(caseSensitiveBases, baseEncodingStr) ? | ||
caseSensitiveBases[baseEncodingStr] : true; | ||
} | ||
/** | ||
@@ -75,2 +98,3 @@ * Encode a UUID | ||
* @returns {string} Properly formatted UUID | ||
* @throws Throws an {Error} when encountering invalid data | ||
*/ | ||
@@ -82,8 +106,15 @@ decode(str) { | ||
const len = str.length; | ||
const finalStr = (this.isCaseSensitive) ? str : str.toLowerCase(); | ||
for (let pos = 0; pos < len; pos += 1) { | ||
const ch = str.substr(pos, 1); | ||
const ch = finalStr.substr(pos, 1); | ||
const encPos = encStr.indexOf(ch); | ||
iUuid = iUuid.add(encStr.indexOf(ch)); | ||
if (encPos < 0) { | ||
throw new Error('Invalid encoded data'); | ||
} | ||
iUuid = iUuid.add(encPos); | ||
if (pos < len - 1) { | ||
@@ -90,0 +121,0 @@ iUuid = iUuid.multiply(base); |
{ | ||
"name": "uuid-encoder", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Encode UUIDs into Base36 or any other system", | ||
@@ -22,3 +22,5 @@ "main": "lib/index.js", | ||
"base36", | ||
"base32" | ||
"base32", | ||
"base58", | ||
"base62" | ||
], | ||
@@ -25,0 +27,0 @@ "files": [ |
@@ -11,6 +11,6 @@ # UUID Encoder | ||
```js | ||
const UuidCompressor = require('uuid-compressor'); | ||
const UuidEncoder = require('uuid-encoder'); | ||
// Create Base 36 encoder | ||
const encoder = new UuidCompressor('base36'); | ||
const encoder = new UuidEncoder('base36'); | ||
@@ -49,4 +49,7 @@ // Encode an UUID | ||
| `'base16'` | 0-9, a-f | Hexadecimal encoding | | ||
| `'base32'` | _Custom_ | Crockford's Base32 | | ||
| `'base32'` | _Custom_ | Crockford's Base 32 | | ||
| `'base36'` | 0-9, a-z | Base 36 (default) | | ||
| `'base58'` | _Custom_ | Bitcoin Base 58 | | ||
| `'base62'` | 0-9, A-Z, a-z | Base 62 | | ||
| `'base64'` | 0-9, A-Z, a-z, +, / | Base 64 | | ||
@@ -58,6 +61,8 @@ | ||
To use a different set or count of encoding characters, simply pass a string | ||
containing every desired letter to the constructor: | ||
containing every desired letter to the constructor. | ||
All custom encoding sets are **case sensitive**. | ||
```js | ||
const encoder = new UuidCompressor('02468ACEGI'); // weird base10 | ||
const encoder = new UuidEncoder('02468ACEGI'); // weird base10 | ||
``` |
7567
102
66