@lusito/require-libs
Version:
Some libraries to setup require hooks
44 lines (43 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getScopedNameGenerator = getScopedNameGenerator;
function getScopedNameGenerator(code, filepath, generateScopedName) {
if (generateScopedName === true) {
const suffix = encode(hashStr(code));
return (name) => `${name}_${suffix}`;
}
if (generateScopedName) {
return generateScopedName(code, filepath);
}
return undefined;
}
/**
* djb2 string hash implementation based on string-hash module:
* https://github.com/darkskyapp/string-hash
*/
function hashStr(str) {
let hash = 5381;
let i = str.length;
while (i) {
// eslint-disable-next-line no-bitwise
hash = (hash * 33) ^ str.charCodeAt(--i);
}
// eslint-disable-next-line no-bitwise
return hash >>> 0;
}
/**
* base62 encode implementation based on base62 module:
* https://github.com/andrew/base62.js
*/
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
function encode(integer) {
if (integer === 0) {
return "0";
}
let str = "";
while (integer > 0) {
str = CHARS[integer % 62] + str;
integer = Math.floor(integer / 62);
}
return str;
}