UNPKG

domain-objects

Version:

A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.

35 lines 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUniqueIdentifierSlug = void 0; const uuid_fns_1 = require("uuid-fns"); const getUniqueIdentifier_1 = require("./getUniqueIdentifier"); /** * get a uri safe slug which uniquely identifies this domain object * * features * - human scannable, for easy debugging * - file path and uri safe, for broad usage * * usecases * - persisting domain objects to a cache * * strategy * - define the human readable portion of the slug by concatenating the unique identifier values and omitting non-safe characters (safe = `\w`, `.`, `-`, `_`) * - define the identity uniqueness guarantee by using a sha1-uuid to guarantee that the total string will no have collisions due to excluding non-safe characters */ const getUniqueIdentifierSlug = (dobj) => { const identifier = (0, getUniqueIdentifier_1.getUniqueIdentifier)(dobj); // define the human scanable part so users can debug more easily const humanPart = `${dobj.constructor.name}.${Object.entries(identifier) .sort((a, b) => (a[0] < b[0] ? -1 : 1)) // sort by key name .map((val) => JSON.stringify(val[1])) .join('.')}` .replace(/[^\w\-_.]/g, '') // strip non-safe characters .slice(0, 128); // limit the characters // define the uniqueness guarantee part, to ensure that there's no collisions in uniqueness due to removing non-safe characters const uniquePart = (0, uuid_fns_1.getHash)(JSON.stringify(identifier)).replace(/-/g, ''); // return the combination return [humanPart, uniquePart].join('.'); }; exports.getUniqueIdentifierSlug = getUniqueIdentifierSlug; //# sourceMappingURL=getUniqueIdentifierSlug.js.map