voided-data
Version:
Proper DS for TS
24 lines (23 loc) • 940 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const hash_1 = require("../math/hash");
const hash = (hashable, seed = 0) => {
const _hash = (0, hash_1.cyrb53)(seed);
if (hashable === null)
return 17;
if (hashable === undefined)
return 31;
if (typeof hashable === 'string')
return _hash(hashable);
if (typeof hashable === 'bigint' || typeof hashable === 'number')
return _hash(`${hashable}`);
if (typeof hashable === 'boolean')
return _hash(`${+hashable}`); // +true = 1; +false = 0;
if (Array.isArray(hashable)) {
return hashable.reduce((agr, next) => 17 * agr + hash(next, _hash(`${seed}`)), 17);
}
const values = Object.entries(hashable);
values.sort(([l], [r]) => l.localeCompare(r));
return values.reduce((agr, [, v]) => 17 * agr + hash(v, _hash(`${seed}`)), 17);
};
exports.default = hash;