|
1 | 1 | 'use strict'; |
2 | | -/* globals Symbol: true, Uint8Array: true, WeakMap: true */ |
| 2 | +/* globals Symbol: false, Uint8Array: false, WeakMap: false */ |
3 | 3 | /*! |
4 | 4 | * deep-eql |
5 | 5 | * Copyright(c) 2013 Jake Luer <jake@alogicalparadox.com> |
6 | 6 | * MIT Licensed |
7 | 7 | */ |
8 | 8 |
|
9 | | -/*! |
10 | | - * Module dependencies |
11 | | - */ |
12 | | - |
13 | 9 | var type = require('type-detect'); |
14 | 10 | function FakeMap() { |
15 | | - this.clear(); |
| 11 | + this._key = 'chai/deep-eql__' + Math.random() + Date.now(); |
16 | 12 | } |
| 13 | + |
17 | 14 | FakeMap.prototype = { |
18 | | - clear: function clearMap() { |
19 | | - this.keys = []; |
20 | | - this.values = []; |
21 | | - return this; |
22 | | - }, |
23 | | - set: function setMap(key, value) { |
24 | | - var index = this.keys.indexOf(key); |
25 | | - if (index >= 0) { |
26 | | - this.values[index] = value; |
27 | | - } else { |
28 | | - this.keys.push(key); |
29 | | - this.values.push(value); |
30 | | - } |
31 | | - return this; |
32 | | - }, |
33 | 15 | get: function getMap(key) { |
34 | | - return this.values[this.keys.indexOf(key)]; |
| 16 | + return key[this._key]; |
35 | 17 | }, |
36 | | - delete: function deleteMap(key) { |
37 | | - var index = this.keys.indexOf(key); |
38 | | - if (index >= 0) { |
39 | | - this.values = this.values.slice(0, index).concat(this.values.slice(index + 1)); |
40 | | - this.keys = this.keys.slice(0, index).concat(this.keys.slice(index + 1)); |
| 18 | + set: function setMap(key, value) { |
| 19 | + if (!Object.isFrozen(key)) { |
| 20 | + Object.defineProperty(key, this._key, { |
| 21 | + value: value, |
| 22 | + configurable: true, |
| 23 | + }); |
41 | 24 | } |
42 | | - return this; |
43 | 25 | }, |
44 | 26 | }; |
45 | 27 |
|
46 | | -var MemoizeMap = null; |
47 | | -if (typeof WeakMap === 'function') { |
48 | | - MemoizeMap = WeakMap; |
49 | | -} else { |
50 | | - MemoizeMap = FakeMap; |
51 | | -} |
52 | | - |
| 28 | +var MemoizeMap = typeof WeakMap === 'function' ? WeakMap : FakeMap; |
53 | 29 | /*! |
54 | 30 | * Check to see if the MemoizeMap has recorded a result of the two operands |
55 | 31 | * |
|
0 commit comments