1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/eslint-module-utils/hash.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-12-18 13:12:25 +08:00
/**
* utilities for hashing config objects.
* basically iteratively updates hash with a JSON-like format
*/
2023-12-28 23:41:32 +08:00
'use strict';
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
exports.__esModule = true;
const createHash = require('crypto').createHash;
const stringify = JSON.stringify;
2023-12-18 13:12:25 +08:00
function hashify(value, hash) {
2023-12-28 23:41:32 +08:00
if (!hash) { hash = createHash('sha256'); }
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
if (Array.isArray(value)) {
hashArray(value, hash);
2023-12-18 13:12:25 +08:00
} else if (value instanceof Object) {
2023-12-28 23:41:32 +08:00
hashObject(value, hash);
2023-12-18 13:12:25 +08:00
} else {
2023-12-28 23:41:32 +08:00
hash.update(stringify(value) || 'undefined');
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
return hash;
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
exports.default = hashify;
2023-12-18 13:12:25 +08:00
function hashArray(array, hash) {
2023-12-28 23:41:32 +08:00
if (!hash) { hash = createHash('sha256'); }
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
hash.update('[');
2023-12-18 13:12:25 +08:00
for (let i = 0; i < array.length; i++) {
2023-12-28 23:41:32 +08:00
hashify(array[i], hash);
hash.update(',');
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
hash.update(']');
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
return hash;
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
hashify.array = hashArray;
exports.hashArray = hashArray;
2023-12-18 13:12:25 +08:00
function hashObject(object, hash) {
2023-12-28 23:41:32 +08:00
if (!hash) { hash = createHash('sha256'); }
hash.update('{');
Object.keys(object).sort().forEach((key) => {
hash.update(stringify(key));
hash.update(':');
hashify(object[key], hash);
hash.update(',');
});
hash.update('}');
return hash;
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
hashify.object = hashObject;
exports.hashObject = hashObject;
2023-12-18 13:12:25 +08:00