You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
620 B
JavaScript

3 years ago
"use strict";
// This generates stable (for the lifetime of the process) identifiers for individual values, by identity - it's useful for correlating together eg. debug logs where pretty-printing the full values is impractical
// FIXME: Move this out into its own dedicated package
const nanoid = require("nanoid/non-secure").customAlphabet("1234567890abcdefghklmpqrsvxyz", 6);
let knownValues = new WeakMap();
module.exports = function valueID(value) {
3 years ago
if (value == null) {
return value;
} else {
if (!knownValues.has(value)) {
knownValues.set(value, nanoid());
}
3 years ago
3 years ago
return knownValues.get(value);
}
3 years ago
};