Files
codeql-action/node_modules/eslint-module-utils/hash.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-09-14 10:42:37 +01:00
/**
* utilities for hashing config objects.
* basically iteratively updates hash with a JSON-like format
*/
2023-08-01 03:35:02 -07:00
2021-07-27 16:54:26 +00:00
'use strict';
2023-08-01 03:35:02 -07:00
2021-07-27 16:54:26 +00:00
exports.__esModule = true;
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
const createHash = require('crypto').createHash;
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
const stringify = JSON.stringify;
2020-09-14 10:42:37 +01:00
function hashify(value, hash) {
2023-08-01 03:35:02 -07:00
if (!hash) { hash = createHash('sha256'); }
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
if (Array.isArray(value)) {
hashArray(value, hash);
2020-09-14 10:42:37 +01:00
} else if (value instanceof Object) {
2021-07-27 16:54:26 +00:00
hashObject(value, hash);
2020-09-14 10:42:37 +01:00
} else {
2021-07-27 16:54:26 +00:00
hash.update(stringify(value) || 'undefined');
2020-09-14 10:42:37 +01:00
}
2021-07-27 16:54:26 +00:00
return hash;
2020-09-14 10:42:37 +01:00
}
2021-07-27 16:54:26 +00:00
exports.default = hashify;
2020-09-14 10:42:37 +01:00
function hashArray(array, hash) {
2023-08-01 03:35:02 -07:00
if (!hash) { hash = createHash('sha256'); }
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
hash.update('[');
2020-09-14 10:42:37 +01:00
for (let i = 0; i < array.length; i++) {
2021-07-27 16:54:26 +00:00
hashify(array[i], hash);
hash.update(',');
2020-09-14 10:42:37 +01:00
}
2021-07-27 16:54:26 +00:00
hash.update(']');
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
return hash;
2020-09-14 10:42:37 +01:00
}
2021-07-27 16:54:26 +00:00
hashify.array = hashArray;
exports.hashArray = hashArray;
2020-09-14 10:42:37 +01:00
function hashObject(object, hash) {
2023-08-01 03:35:02 -07:00
if (!hash) { hash = createHash('sha256'); }
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
hash.update('{');
2023-08-01 03:35:02 -07:00
Object.keys(object).sort().forEach((key) => {
2021-07-27 16:54:26 +00:00
hash.update(stringify(key));
hash.update(':');
hashify(object[key], hash);
hash.update(',');
});
hash.update('}');
return hash;
2020-09-14 10:42:37 +01:00
}
2021-07-27 16:54:26 +00:00
hashify.object = hashObject;
exports.hashObject = hashObject;
2020-09-14 10:42:37 +01:00