memoization-registry
Version:
A generalized multi-key memoization solution that does not leak memory.
49 lines (48 loc) • 1.12 kB
JavaScript
/* IMPORT */
import MildMap from 'mild-map';
/* MAIN */
const isObject = (value) => {
if (value === null)
return false;
const type = typeof value;
return type === 'object' || type === 'function';
};
const isUndefined = (value) => {
return value === undefined;
};
const isWeakRef = (value) => {
return value instanceof WeakRef;
};
const last = (values) => {
return values[values.length - 1];
};
const traverse = (node, keys) => {
return keys.map(key => {
const next = node.get(key) || new MildMap();
node.set(key, next);
node = next;
return node;
});
};
const weakize = (values) => {
return values.map(value => {
if (isObject(value)) {
return new WeakRef(value);
}
else {
return value;
}
});
};
const unweakize = (values) => {
return values.map(value => {
if (isWeakRef(value)) {
return value.deref();
}
else {
return value;
}
});
};
/* EXPORT */
export { isObject, isUndefined, last, traverse, weakize, unweakize };