Add coverage for WeakMap usage.

This commit is contained in:
John-David Dalton
2014-08-25 09:32:26 -07:00
parent 7f88a7c9bb
commit 87c21ba75e
4 changed files with 107 additions and 3 deletions

View File

@@ -21,7 +21,7 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a Set object to optimize linear searches of large arrays.
* Creates a `Set` object.
*/
function Set() {
this.__cache__ = {};
@@ -81,7 +81,7 @@
* Adds `value` to the set.
*
* @memberOf Set
* @param {*} value The value to add to the set.
* @param {*} value The value to add.
*/
function add(value) {
var cache = this.__cache__,

76
test/asset/weakmap.js Normal file
View File

@@ -0,0 +1,76 @@
;(function() {
/** Used to determine if values are of the language type Object */
var objectTypes = {
'function': true,
'object': true
};
/** Used as the `WeakMap#toString` return value */
var nativeString = String(Object.prototype.toString).replace(/toString/g, 'WeakMap');
/** Used as a reference to the global object */
var root = (objectTypes[typeof window] && window) || this;
/** Detect free variable `global` from Node.js or Browserified code and use it as `root` */
var freeGlobal = objectTypes[typeof global] && global;
if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) {
root = freeGlobal;
}
/*--------------------------------------------------------------------------*/
/**
* Creates a `WeakMap` object.
*/
function WeakMap() {
// no-op
}
/*--------------------------------------------------------------------------*/
/**
* Gets the value associated with the given key.
*
* @memberOf WeakMap
* @param {Object} key The key object.
* @returns {*} Returns the associated value, else `undefined`.
*/
function get(key) {
return key.__weakmap__;
}
/**
* Sets a value for the given key.
*
* @memberOf WeakMap
* @param {Object} key The key object.
* @param {*} value The value to set.
*/
function set(key, value) {
key.__weakmap__ = value;
return this;
}
/**
* Produces the `toString` result of `WeakMap`.
*
* @static
* @memberOf WeakMap
* @returns {string} Returns the string result.
*/
function toString() {
return nativeString;
}
/*--------------------------------------------------------------------------*/
WeakMap.toString = toString;
WeakMap.prototype.get = get;
WeakMap.prototype.set = set;
// expose `WeakMap`
if (!root.WeakMap) {
root.WeakMap = WeakMap;
}
}.call(this));