@hiki9/rich-domain
Version:
Rich Domain is a library that provides a set of tools to help you build complex business logic in NodeJS using Domain Driven Design principles.
101 lines • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.shortId = void 0;
const crypto_1 = require("crypto");
const converter_any_base_1 = require("./converter-any-base");
const flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
function anyBase(srcAlphabet, dstAlphabet) {
var converter = new converter_any_base_1.ConverterAnyBase(srcAlphabet, dstAlphabet);
return function (number) {
return converter.convert(number);
};
}
;
anyBase.BIN = '01';
anyBase.OCT = '01234567';
anyBase.DEC = '0123456789';
anyBase.HEX = '0123456789abcdef';
const baseOptions = {
consistentLength: true,
};
// A default generator, instantiated only if used.
let toFlickr;
/**
* Takes a UUID, strips the dashes, and translates.
* @param {string} longId
* @param {function(string)} translator
* @param {Object} [paddingParams]
* @returns {string}
*/
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);
};
/**
* Translate back to hex and turn back into UUID format, with dashes
* @param {string} shortId
* @param {function(string)} translator
* @returns {string}
*/
const enlargeUUID = (shortId, translator) => {
const uu1 = translator(shortId).padStart(32, '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})/);
// 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) => (Math.ceil(Math.log(2 ** 128) / Math.log(alphabetLength)));
function shortId() {
const makeConvertor = (toAlphabet, options) => {
// Default to Flickr 58
const useAlphabet = toAlphabet || flickrBase58;
// Default to baseOptions
const selectedOptions = { ...baseOptions, ...options };
// 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');
}
const shortIdLength = getShortIdLength(useAlphabet.length);
// Padding Params
const paddingParams = {
shortIdLength,
consistentLength: selectedOptions.consistentLength,
paddingChar: useAlphabet[0],
};
// 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((0, crypto_1.randomUUID)(), fromHex, paddingParams);
const translator = {
new: generate,
generate,
uuid: crypto_1.randomUUID,
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.
makeConvertor.constants = {
flickrBase58,
};
// Expose the generic v4 UUID generator for convenience
makeConvertor.uuid = crypto_1.randomUUID;
// Provide a generic generator
makeConvertor.generate = () => {
if (!toFlickr) {
// Generate on first use;
toFlickr = makeConvertor(flickrBase58).generate;
}
return toFlickr();
};
return makeConvertor;
}
exports.shortId = shortId;
//# sourceMappingURL=short-id.js.map