@configurator/ravendb
Version:
RavenDB client for Node.js
55 lines • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HashCalculator = void 0;
const md5 = require("md5");
const TypeUtil_1 = require("../../Utility/TypeUtil");
const typeSignatures = {
bigint: Buffer.from([1]),
boolean: Buffer.from([2]),
function: Buffer.from([3]),
number: Buffer.from([4]),
object: Buffer.from([5]),
string: Buffer.from([6]),
symbol: Buffer.from([7]),
undefined: Buffer.from([8]),
};
class HashCalculator {
constructor() {
this._buffers = [];
}
getHash() {
return md5(Buffer.concat(this._buffers));
}
write(o, mapper) {
if (TypeUtil_1.TypeUtil.isNullOrUndefined(o)) {
this._buffers.push(Buffer.from("null"));
return;
}
this._buffers.push(typeSignatures[typeof o] || typeSignatures.undefined);
if (typeof o === "number") {
this._buffers.push(Buffer.from(String(o)));
}
else if (typeof o === "string") {
this._buffers.push(Buffer.from(o));
}
else if (typeof o === "boolean") {
this._buffers.push(Buffer.from(o ? [1] : [2]));
}
else if (Array.isArray(o)) {
for (const item of o) {
this.write(item, mapper);
}
}
else if (typeof o === "object") {
for (const key of Object.keys(o)) {
this.write(key, mapper);
this.write(o[key], mapper);
}
}
else {
this.write(mapper.toObjectLiteral(o), mapper);
}
}
}
exports.HashCalculator = HashCalculator;
//# sourceMappingURL=HashCalculator.js.map