immutable-tuple
Advanced tools
Comparing version 0.4.8 to 0.4.9
@@ -5,54 +5,2 @@ 'use strict'; | ||
// A map data structure that holds object keys weakly, yet can also hold | ||
// non-object keys, unlike the native `WeakMap`. | ||
var UniversalWeakMap = function UniversalWeakMap() { | ||
// Since a `WeakMap` cannot hold primitive values as keys, we need a | ||
// backup `Map` instance to hold primitive keys. Both `this._weakMap` | ||
// and `this._strongMap` are lazily initialized. | ||
this._weakMap = null; | ||
this._strongMap = null; | ||
this.data = null; | ||
}; | ||
// Since `get` and `set` are the only methods used, that's all I've | ||
// implemented here. | ||
UniversalWeakMap.prototype.get = function get (key) { | ||
var map = this._getMap(key, false); | ||
if (map) { | ||
return map.get(key); | ||
} | ||
}; | ||
UniversalWeakMap.prototype.set = function set (key, value) { | ||
this._getMap(key, true).set(key, value); | ||
// An actual `Map` or `WeakMap` would return `this` here, but | ||
// returning the `value` is more convenient for the `tuple` | ||
// implementation. | ||
return value; | ||
}; | ||
UniversalWeakMap.prototype._getMap = function _getMap (key, canCreate) { | ||
if (! canCreate) { | ||
return isObjRef(key) ? this._weakMap : this._strongMap; | ||
} | ||
if (isObjRef(key)) { | ||
return this._weakMap || (this._weakMap = new WeakMap); | ||
} | ||
return this._strongMap || (this._strongMap = new Map); | ||
}; | ||
function isObjRef(value) { | ||
switch (typeof value) { | ||
case "object": | ||
if (value === null) { | ||
return false; | ||
} | ||
case "function": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
// Although `Symbol` is widely supported these days, we can safely fall | ||
@@ -91,2 +39,15 @@ // back to using a non-enumerable string property without violating any | ||
function isObjRef(value) { | ||
switch (typeof value) { | ||
case "object": | ||
if (value === null) { | ||
return false; | ||
} | ||
case "function": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
// The `mustConvertThisToArray` value is true when the corresponding | ||
@@ -129,2 +90,41 @@ // `Array` method does not attempt to modify `this`, which means we can | ||
// A map data structure that holds object keys weakly, yet can also hold | ||
// non-object keys, unlike the native `WeakMap`. | ||
var UniversalWeakMap = function UniversalWeakMap() { | ||
// Since a `WeakMap` cannot hold primitive values as keys, we need a | ||
// backup `Map` instance to hold primitive keys. Both `this._weakMap` | ||
// and `this._strongMap` are lazily initialized. | ||
this._weakMap = null; | ||
this._strongMap = null; | ||
this.data = null; | ||
}; | ||
// Since `get` and `set` are the only methods used, that's all I've | ||
// implemented here. | ||
UniversalWeakMap.prototype.get = function get (key) { | ||
var map = this._getMap(key, false); | ||
if (map) { | ||
return map.get(key); | ||
} | ||
}; | ||
UniversalWeakMap.prototype.set = function set (key, value) { | ||
this._getMap(key, true).set(key, value); | ||
// An actual `Map` or `WeakMap` would return `this` here, but | ||
// returning the `value` is more convenient for the `tuple` | ||
// implementation. | ||
return value; | ||
}; | ||
UniversalWeakMap.prototype._getMap = function _getMap (key, canCreate) { | ||
if (! canCreate) { | ||
return isObjRef(key) ? this._weakMap : this._strongMap; | ||
} | ||
if (isObjRef(key)) { | ||
return this._weakMap || (this._weakMap = new WeakMap); | ||
} | ||
return this._strongMap || (this._strongMap = new Map); | ||
}; | ||
// See [`universal-weak-map.js`](universal-weak-map.html). | ||
@@ -142,4 +142,6 @@ // See [`util.js`](util.html). | ||
function lookup() { | ||
var arguments$1 = arguments; | ||
return lookupArray(arguments); | ||
} | ||
function lookupArray(array) { | ||
var node = root; | ||
@@ -152,5 +154,5 @@ | ||
// property is a key advantage of the `immutable-tuple` package. | ||
var argc = arguments.length; | ||
for (var i = 0; i < argc; ++i) { | ||
var item = arguments$1[i]; | ||
var len = array.length; | ||
for (var i = 0; i < len; ++i) { | ||
var item = array[i]; | ||
node = node.get(item) || node.set(item, new UniversalWeakMap); | ||
@@ -258,1 +260,2 @@ } | ||
exports.lookup = lookup; | ||
exports.lookupArray = lookupArray; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){this._weakMap=null,this._strongMap=null,this.data=null};function e(t){switch(typeof t){case"object":if(null===t)return!1;case"function":return!0;default:return!1}}t.prototype.get=function(t){var e=this._getMap(t,!1);if(e)return e.get(t)},t.prototype.set=function(t,e){return this._getMap(t,!0).set(t,e),e},t.prototype._getMap=function(t,r){return r?e(t)?this._weakMap||(this._weakMap=new WeakMap):this._strongMap||(this._strongMap=new Map):e(t)?this._weakMap:this._strongMap};var r="function"==typeof Symbol,n=r?Symbol.for("immutable-tuple"):"@@__IMMUTABLE_TUPLE__@@",o=r?Symbol.for("immutable-tuple-root"):"@@__IMMUTABLE_TUPLE_ROOT__@@";function a(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!!n,writable:!1,configurable:!1}),r}var u=Object.freeze||function(t){return t};function i(t){function e(e,r){var n=Object.getOwnPropertyDescriptor(Array.prototype,e);t(e,n,!!r)}e("every"),e("filter"),e("find"),e("findIndex"),e("forEach"),e("includes"),e("indexOf"),e("join"),e("lastIndexOf"),e("map"),e("reduce"),e("reduceRight"),e("slice"),e("some"),e("toLocaleString"),e("toString"),e("reverse",!0),e("sort",!0),e(r&&Symbol.iterator||"@@iterator")}var p=Array[o]||a(Array,o,new t,!1);function l(){for(var e=arguments,r=p,n=arguments.length,o=0;o<n;++o){var a=e[o];r=r.get(a)||r.set(a,new t)}return r.data||(r.data=Object.create(null))}function tuple(){var t=arguments,e=l.apply(null,arguments);if(e.tuple)return e.tuple;for(var r=Object.create(tuple.prototype),n=arguments.length,o=0;o<n;++o)r[o]=t[o];return a(r,"length",n,!1),u(e.tuple=r)}function c(t){return!(!t||!0!==t[n])}function f(tuple){for(var t=[],e=tuple.length;e--;)t[e]=tuple[e];return t}a(tuple.prototype,n,!0,!1),tuple.isTuple=c,i(function(t,e,r){var n=e&&e.value;"function"==typeof n&&(e.value=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];var o=n.apply(r?f(this):this,t);return Array.isArray(o)?tuple.apply(void 0,o):o},Object.defineProperty(tuple.prototype,t,e))});var s=Array.prototype,y=s.concat;tuple.prototype.concat=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return tuple.apply(void 0,y.apply(f(this),t.map(function(t){return c(t)?f(t):t})))},exports.default=tuple,exports.tuple=tuple,exports.lookup=l; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t="function"==typeof Symbol,e=t?Symbol.for("immutable-tuple"):"@@__IMMUTABLE_TUPLE__@@",r=t?Symbol.for("immutable-tuple-root"):"@@__IMMUTABLE_TUPLE_ROOT__@@";function n(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!!n,writable:!1,configurable:!1}),r}var o=Object.freeze||function(t){return t};function a(t){switch(typeof t){case"object":if(null===t)return!1;case"function":return!0;default:return!1}}function u(e){function r(t,r){var n=Object.getOwnPropertyDescriptor(Array.prototype,t);e(t,n,!!r)}r("every"),r("filter"),r("find"),r("findIndex"),r("forEach"),r("includes"),r("indexOf"),r("join"),r("lastIndexOf"),r("map"),r("reduce"),r("reduceRight"),r("slice"),r("some"),r("toLocaleString"),r("toString"),r("reverse",!0),r("sort",!0),r(t&&Symbol.iterator||"@@iterator")}var p=function(){this._weakMap=null,this._strongMap=null,this.data=null};p.prototype.get=function(t){var e=this._getMap(t,!1);if(e)return e.get(t)},p.prototype.set=function(t,e){return this._getMap(t,!0).set(t,e),e},p.prototype._getMap=function(t,e){return e?a(t)?this._weakMap||(this._weakMap=new WeakMap):this._strongMap||(this._strongMap=new Map):a(t)?this._weakMap:this._strongMap};var i=Array[r]||n(Array,r,new p,!1);function l(){return c(arguments)}function c(t){for(var e=i,r=t.length,n=0;n<r;++n){var o=t[n];e=e.get(o)||e.set(o,new p)}return e.data||(e.data=Object.create(null))}function tuple(){var t=arguments,e=l.apply(null,arguments);if(e.tuple)return e.tuple;for(var r=Object.create(tuple.prototype),a=arguments.length,u=0;u<a;++u)r[u]=t[u];return n(r,"length",a,!1),o(e.tuple=r)}function f(t){return!(!t||!0!==t[e])}function s(tuple){for(var t=[],e=tuple.length;e--;)t[e]=tuple[e];return t}n(tuple.prototype,e,!0,!1),tuple.isTuple=f,u(function(t,e,r){var n=e&&e.value;"function"==typeof n&&(e.value=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];var o=n.apply(r?s(this):this,t);return Array.isArray(o)?tuple.apply(void 0,o):o},Object.defineProperty(tuple.prototype,t,e))});var y=Array.prototype,v=y.concat;tuple.prototype.concat=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return tuple.apply(void 0,v.apply(s(this),t.map(function(t){return f(t)?s(t):t})))},exports.default=tuple,exports.tuple=tuple,exports.lookup=l,exports.lookupArray=c; |
{ | ||
"name": "immutable-tuple", | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"description": "Immutable finite list objects with constant-time equality testing (===) and no memory leaks", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -17,2 +17,6 @@ // See [`universal-weak-map.js`](universal-weak-map.html). | ||
export function lookup() { | ||
return lookupArray(arguments); | ||
} | ||
export function lookupArray(array) { | ||
let node = root; | ||
@@ -25,5 +29,5 @@ | ||
// property is a key advantage of the `immutable-tuple` package. | ||
const argc = arguments.length; | ||
for (let i = 0; i < argc; ++i) { | ||
const item = arguments[i]; | ||
const len = array.length; | ||
for (let i = 0; i < len; ++i) { | ||
const item = array[i]; | ||
node = node.get(item) || node.set(item, new UniversalWeakMap); | ||
@@ -30,0 +34,0 @@ } |
// See [`lookup.js`](lookup.html). | ||
import { lookup } from "./lookup.js"; | ||
import { lookup, lookupArray } from "./lookup.js"; | ||
@@ -43,3 +43,3 @@ // See [`util.js`](util.html). | ||
// Named imports work as well as `default` imports. | ||
export { tuple, lookup }; | ||
export { tuple, lookup, lookupArray }; | ||
@@ -46,0 +46,0 @@ // Since the `immutable-tuple` package could be installed multiple times |
@@ -0,1 +1,3 @@ | ||
import { isObjRef } from "./util.js"; | ||
// A map data structure that holds object keys weakly, yet can also hold | ||
@@ -41,14 +43,1 @@ // non-object keys, unlike the native `WeakMap`. | ||
} | ||
function isObjRef(value) { | ||
switch (typeof value) { | ||
case "object": | ||
if (value === null) { | ||
return false; | ||
} | ||
case "function": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} |
@@ -34,2 +34,15 @@ // Although `Symbol` is widely supported these days, we can safely fall | ||
export function isObjRef(value) { | ||
switch (typeof value) { | ||
case "object": | ||
if (value === null) { | ||
return false; | ||
} | ||
case "function": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
// The `mustConvertThisToArray` value is true when the corresponding | ||
@@ -36,0 +49,0 @@ // `Array` method does not attempt to modify `this`, which means we can |
Sorry, the diff of this file is not supported yet
45554
665