es6-weak-map
Advanced tools
Comparing version 0.1.4 to 1.0.0
'use strict'; | ||
module.exports = require('./is-implemented')() ? | ||
WeakMap : require('./polyfill'); | ||
module.exports = require('./is-implemented')() ? WeakMap : require('./polyfill'); |
'use strict'; | ||
module.exports = function () { | ||
var map; | ||
var weakMap, x; | ||
if (typeof WeakMap !== 'function') return false; | ||
map = new WeakMap(); | ||
if (typeof map.set !== 'function') return false; | ||
if (map.set({}, 1) !== map) return false; | ||
if (typeof map.clear !== 'function') return false; | ||
if (typeof map.delete !== 'function') return false; | ||
if (typeof map.has !== 'function') return false; | ||
if (String(WeakMap.prototype) !== '[object WeakMap]') return false; | ||
try { | ||
// WebKit doesn't support arguments and crashes | ||
weakMap = new WeakMap([[x = {}, 'one'], [{}, 'two'], [{}, 'three']]); | ||
} catch (e) { | ||
return false; | ||
} | ||
if (typeof weakMap.set !== 'function') return false; | ||
if (weakMap.set({}, 1) !== weakMap) return false; | ||
if (typeof weakMap.delete !== 'function') return false; | ||
if (typeof weakMap.has !== 'function') return false; | ||
if (weakMap.get(x) !== 'one') return false; | ||
return true; | ||
}; |
{ | ||
"name": "es6-weak-map", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"description": "ECMAScript6 WeakMap polyfill", | ||
@@ -21,11 +21,15 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", | ||
"dependencies": { | ||
"d": "~0.1.1", | ||
"es5-ext": "~0.10.6", | ||
"es6-iterator": "~0.1.3", | ||
"es6-symbol": "~2.0.1" | ||
"d": "^0.1.1", | ||
"es5-ext": "^0.10.6", | ||
"es6-iterator": "^0.1.3", | ||
"es6-symbol": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"tad": "~0.2.2" | ||
"tad": "^0.2.2", | ||
"xlint": "^0.2.2", | ||
"xlint-jslint-medikoo": "^0.1.2" | ||
}, | ||
"scripts": { | ||
"lint": "node node_modules/xlint/bin/xlint --linter=node_modules/xlint-jslint-medikoo/index.js --no-cache --no-stream", | ||
"lint-console": "node node_modules/xlint/bin/xlint --linter=node_modules/xlint-jslint-medikoo/index.js --watch", | ||
"test": "node ./node_modules/tad/bin/tad" | ||
@@ -32,0 +36,0 @@ }, |
@@ -6,2 +6,3 @@ 'use strict'; | ||
, value = require('es5-ext/object/valid-value') | ||
, randomUniq = require('es5-ext/string/random-uniq') | ||
, d = require('d') | ||
@@ -13,31 +14,21 @@ , getIterator = require('es6-iterator/get') | ||
, isArray = Array.isArray, defineProperty = Object.defineProperty, random = Math.random | ||
, hasOwnProperty = Object.prototype.hasOwnProperty | ||
, genId, WeakMapPoly; | ||
, isArray = Array.isArray, defineProperty = Object.defineProperty | ||
, hasOwnProperty = Object.prototype.hasOwnProperty, getPrototypeOf = Object.getPrototypeOf | ||
, WeakMapPoly; | ||
genId = (function () { | ||
var generated = Object.create(null); | ||
return function () { | ||
var id; | ||
do { id = random().toString(36).slice(2); } while (generated[id]); | ||
generated[id] = true; | ||
return id; | ||
}; | ||
}()); | ||
module.exports = WeakMapPoly = function (/*iterable*/) { | ||
var iterable = arguments[0]; | ||
if (!(this instanceof WeakMapPoly)) return new WeakMapPoly(iterable); | ||
if (this.__weakMapData__ !== undefined) { | ||
throw new TypeError(this + " cannot be reinitialized"); | ||
} | ||
var iterable = arguments[0], self; | ||
if (!(this instanceof WeakMapPoly)) throw new TypeError('Constructor requires \'new\''); | ||
if (isNative && setPrototypeOf) self = setPrototypeOf(new WeakMap(), getPrototypeOf(this)); | ||
else self = this; | ||
if (iterable != null) { | ||
if (!isArray(iterable)) iterable = getIterator(iterable); | ||
} | ||
defineProperty(this, '__weakMapData__', d('c', '$weakMap$' + genId())); | ||
if (!iterable) return; | ||
defineProperty(self, '__weakMapData__', d('c', '$weakMap$' + randomUniq())); | ||
if (!iterable) return self; | ||
forOf(iterable, function (val) { | ||
value(val); | ||
this.set(val[0], val[1]); | ||
}, this); | ||
self.set(val[0], val[1]); | ||
}); | ||
return self; | ||
}; | ||
@@ -53,5 +44,2 @@ | ||
Object.defineProperties(WeakMapPoly.prototype, { | ||
clear: d(function () { | ||
defineProperty(this, '__weakMapData__', d('c', '$weakMap$' + genId())); | ||
}), | ||
delete: d(function (key) { | ||
@@ -58,0 +46,0 @@ if (hasOwnProperty.call(object(key), this.__weakMapData__)) { |
@@ -14,3 +14,2 @@ # es6-weak-map | ||
- Will fail on non extensible objects provided as keys | ||
- While `clear` method is provided, it's not perfectly spec compliant. If some objects were saved as _values_, they need to be removed via `delete`. Otherwise they'll remain infinitely attached to _key_ object (that means, they'll be free for GC only if _key_ object was collected as well). | ||
@@ -60,8 +59,7 @@ ### Installation | ||
map.set(obj, 'bar'); // map | ||
map.clear(); // undefined | ||
map.has(obj); // false | ||
``` | ||
## Tests [![Build Status](https://travis-ci.org/medikoo/es6-weak-map.png)](https://travis-ci.org/medikoo/es6-weak-map) | ||
## Tests [![Build Status](https://travis-ci.org/medikoo/es6-weak-map.svg)](https://travis-ci.org/medikoo/es6-weak-map) | ||
$ npm test |
'use strict'; | ||
module.exports = function (t, a) { a(typeof t(), 'boolean'); }; | ||
var global = require('es5-ext/global') | ||
, polyfill = require('../polyfill'); | ||
module.exports = function (t, a) { | ||
var cache; | ||
a(typeof t(), 'boolean'); | ||
cache = global.WeakMap; | ||
global.WeakMap = polyfill; | ||
a(t(), true); | ||
if (cache === undefined) delete global.WeakMap; | ||
else global.WeakMap = cache; | ||
}; |
@@ -19,5 +19,6 @@ 'use strict'; | ||
a(map.has(y), true, "Has: pre clear"); | ||
map.clear(); | ||
a(map.has(y), false, "Has: after clear"); | ||
a.h1("Empty initialization"); | ||
map = new T(); | ||
map.set(x, 'bar'); | ||
a(map.get(x), 'bar'); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11191
179
0
3
64
Updatedd@^0.1.1
Updatedes5-ext@^0.10.6
Updatedes6-iterator@^0.1.3
Updatedes6-symbol@^2.0.1