ts-json-schema-generator
Version:
Generate JSON schema from your Typescript sources
43 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hash = hash;
exports.getKey = getKey;
const tslib_1 = require("tslib");
const safe_stable_stringify_1 = tslib_1.__importDefault(require("safe-stable-stringify"));
function hash(a) {
if (typeof a === "number") {
return a;
}
const str = typeof a === "string" ? a : (0, safe_stable_stringify_1.default)(a);
if (str.length < 20) {
return str;
}
let h = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
h = (h << 5) - h + char;
h = h & h;
}
if (h < 0) {
return -h;
}
return h;
}
function getKey(node, context) {
const ids = [];
while (node) {
const source = node.getSourceFile();
if (!source) {
ids.push(Math.random());
}
else {
const filename = source.fileName.substring(process.cwd().length + 1).replace(/\//g, "_");
ids.push(hash(filename), node.pos, node.end);
}
node = node.parent;
}
const id = ids.join("-");
const args = context.getArguments();
return args.length ? `${id}<${args.map((arg) => arg?.getId()).join(",")}>` : id;
}
//# sourceMappingURL=nodeKey.js.map