diff --git a/index.js b/index.js index 0f289f8..01e0b3c 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,31 @@ try { nodeInspect = false } -const constructorMap = new WeakMap() +function FakeMap() { + // eslint-disable-next-line prefer-template + this.key = 'chai/loupe__' + Math.random() + Date.now() +} +FakeMap.prototype = { + // eslint-disable-next-line object-shorthand + get: function get(key) { + return key[this.key] + }, + // eslint-disable-next-line object-shorthand + has: function has(key) { + return this.key in key + }, + // eslint-disable-next-line object-shorthand + set: function set(key, value) { + if (Object.isExtensible(key)) { + Object.defineProperty(key, this.key, { + // eslint-disable-next-line object-shorthand + value: value, + configurable: true, + }) + } + }, +} +const constructorMap = new (typeof WeakMap === 'function' ? WeakMap : FakeMap)() const stringTagMap = {} const baseTypesMap = { undefined: (value, options) => options.stylize('undefined', 'undefined'), @@ -178,7 +202,7 @@ export function registerConstructor(constructor, inspector) { if (constructorMap.has(constructor)) { return false } - constructorMap.add(constructor, inspector) + constructorMap.set(constructor, inspector) return true }