immutable-tuple
Advanced tools
Comparing version 0.4.5 to 0.4.6
@@ -113,2 +113,11 @@ 'use strict'; | ||
// See [`util.js`](util.html). | ||
// If this package is installed multiple times, there could be mutiple | ||
// implementations of the `tuple` function with distinct `tuple.prototype` | ||
// objects, but the shared pool of `tuple` objects must be the same across | ||
// all implementations. While it would be ideal to use the `global` | ||
// object, there's no reliable way to get the global object across all JS | ||
// environments without using the `Function` constructor, so instead we | ||
// use the global `Array` constructor as a shared namespace. | ||
var root = Array[globalKey] || def(Array, globalKey, new UniversalWeakMap, false); | ||
// When called with any number of arguments, this function returns an | ||
@@ -122,18 +131,4 @@ // object that inherits from `tuple.prototype` and is guaranteed to be | ||
function tuple() { | ||
var items = [], len = arguments.length; | ||
while ( len-- ) items[ len ] = arguments[ len ]; | ||
var arguments$1 = arguments; | ||
return intern(items); | ||
} | ||
// If this package is installed multiple times, there could be mutiple | ||
// implementations of the `tuple` function with distinct `tuple.prototype` | ||
// objects, but the shared pool of `tuple` objects must be the same across | ||
// all implementations. While it would be ideal to use the `global` | ||
// object, there's no reliable way to get the global object across all JS | ||
// environments without using the `Function` constructor, so instead we | ||
// use the global `Array` constructor as a shared namespace. | ||
var root = Array[globalKey] || def(Array, globalKey, new UniversalWeakMap, false); | ||
function intern(array) { | ||
var node = root; | ||
@@ -146,5 +141,7 @@ | ||
// property is a key advantage of the `immutable-tuple` package. | ||
array.forEach(function (item) { | ||
var argc = arguments.length; | ||
for (var i = 0; i < argc; ++i) { | ||
var item = arguments$1[i]; | ||
node = node.get(item) || node.set(item, new UniversalWeakMap); | ||
}); | ||
} | ||
@@ -157,12 +154,20 @@ // If a `tuple` object has already been created for exactly these items, | ||
var t = Object.create(tuple.prototype); | ||
var t = Object.create(tuple.prototype, { | ||
length: { | ||
value: argc, | ||
enumerable: false, | ||
writable: false, | ||
configurable: false | ||
} | ||
}); | ||
// Define immutable items with numeric indexes, and permanently fix the | ||
// `.length` property. | ||
array.forEach(function (item, i) { return def(t, i, item, true); }); | ||
def(t, "length", array.length, false); | ||
for (var i$1 = 0; i$1 < argc; ++i$1) { | ||
t[i$1] = arguments$1[i$1]; | ||
} | ||
// Remember this new `tuple` object so that we can return the same object | ||
// earlier next time. | ||
return node.tuple = t; | ||
return Object.freeze(node.tuple = t); | ||
} | ||
@@ -186,2 +191,3 @@ | ||
// `tuple.isTuple(value)`. | ||
def(tuple.prototype, brand, true, false); | ||
function isTuple(that) { | ||
@@ -191,4 +197,2 @@ return !! (that && that[brand] === true); | ||
def(tuple.prototype, brand, true, false); | ||
tuple.isTuple = isTuple; | ||
@@ -219,3 +223,3 @@ | ||
// not a new `Array`. | ||
return Array.isArray(result) ? intern(result) : result; | ||
return Array.isArray(result) ? tuple.apply(void 0, result) : result; | ||
}; | ||
@@ -237,3 +241,3 @@ Object.defineProperty(tuple.prototype, name, desc); | ||
return intern(concat.apply(toArray(this), args.map( | ||
return tuple.apply(void 0, concat.apply(toArray(this), args.map( | ||
function (item) { return isTuple(item) ? toArray(item) : item; } | ||
@@ -240,0 +244,0 @@ ))); |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){this._weakMap=null,this._strongMap=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){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")}function tuple(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return i(t)}var u=Array[o]||p(Array,o,new t,!1);function i(e){var r=u;if(e.forEach(function(e){r=r.get(e)||r.set(e,new t)}),r.tuple)return r.tuple;var n=Object.create(tuple.prototype);return e.forEach(function(t,e){return p(n,e,t,!0)}),p(n,"length",e.length,!1),r.tuple=n}function p(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!!n,writable:!1,configurable:!1}),r}function f(t){return!(!t||!0!==t[n])}function c(tuple){for(var t=[],e=tuple.length;e--;)t[e]=tuple[e];return t}p(tuple.prototype,n,!0,!1),tuple.isTuple=f,a(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?c(this):this,t);return Array.isArray(o)?i(o):o},Object.defineProperty(tuple.prototype,t,e))});var l=Array.prototype,s=l.concat;tuple.prototype.concat=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return i(s.apply(c(this),t.map(function(t){return f(t)?c(t):t})))},exports.default=tuple,exports.tuple=tuple; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){this._weakMap=null,this._strongMap=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){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 i=Array[o]||u(Array,o,new t,!1);function tuple(){for(var e=arguments,r=i,n=arguments.length,o=0;o<n;++o){var a=e[o];r=r.get(a)||r.set(a,new t)}if(r.tuple)return r.tuple;for(var u=Object.create(tuple.prototype,{length:{value:n,enumerable:!1,writable:!1,configurable:!1}}),p=0;p<n;++p)u[p]=e[p];return Object.freeze(r.tuple=u)}function u(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!!n,writable:!1,configurable:!1}),r}function p(t){return!(!t||!0!==t[n])}function l(tuple){for(var t=[],e=tuple.length;e--;)t[e]=tuple[e];return t}u(tuple.prototype,n,!0,!1),tuple.isTuple=p,a(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?l(this):this,t);return Array.isArray(o)?tuple.apply(void 0,o):o},Object.defineProperty(tuple.prototype,t,e))});var f=Array.prototype,c=f.concat;tuple.prototype.concat=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return tuple.apply(void 0,c.apply(l(this),t.map(function(t){return p(t)?l(t):t})))},exports.default=tuple,exports.tuple=tuple; |
{ | ||
"name": "immutable-tuple", | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"description": "Immutable finite list objects with constant-time equality testing (===) and no memory leaks", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -11,16 +11,2 @@ // See [`universal-weak-map.js`](universal-weak-map.html). | ||
// When called with any number of arguments, this function returns an | ||
// object that inherits from `tuple.prototype` and is guaranteed to be | ||
// `===` any other `tuple` object that has exactly the same items. In | ||
// computer science jargon, `tuple` instances are "internalized" or just | ||
// "interned," which allows for constant-time equality checking, and makes | ||
// it possible for tuple objects to be used as `Map` or `WeakMap` keys, or | ||
// stored in a `Set`. | ||
export default function tuple(...items) { | ||
return intern(items); | ||
} | ||
// Named imports work as well as `default` imports. | ||
export { tuple }; | ||
// If this package is installed multiple times, there could be mutiple | ||
@@ -35,3 +21,10 @@ // implementations of the `tuple` function with distinct `tuple.prototype` | ||
function intern(array) { | ||
// When called with any number of arguments, this function returns an | ||
// object that inherits from `tuple.prototype` and is guaranteed to be | ||
// `===` any other `tuple` object that has exactly the same items. In | ||
// computer science jargon, `tuple` instances are "internalized" or just | ||
// "interned," which allows for constant-time equality checking, and makes | ||
// it possible for tuple objects to be used as `Map` or `WeakMap` keys, or | ||
// stored in a `Set`. | ||
export default function tuple() { | ||
let node = root; | ||
@@ -44,5 +37,7 @@ | ||
// property is a key advantage of the `immutable-tuple` package. | ||
array.forEach(item => { | ||
const argc = arguments.length; | ||
for (let i = 0; i < argc; ++i) { | ||
const item = arguments[i]; | ||
node = node.get(item) || node.set(item, new UniversalWeakMap); | ||
}); | ||
} | ||
@@ -55,14 +50,25 @@ // If a `tuple` object has already been created for exactly these items, | ||
const t = Object.create(tuple.prototype); | ||
const t = Object.create(tuple.prototype, { | ||
length: { | ||
value: argc, | ||
enumerable: false, | ||
writable: false, | ||
configurable: false | ||
} | ||
}); | ||
// Define immutable items with numeric indexes, and permanently fix the | ||
// `.length` property. | ||
array.forEach((item, i) => def(t, i, item, true)); | ||
def(t, "length", array.length, false); | ||
for (let i = 0; i < argc; ++i) { | ||
t[i] = arguments[i]; | ||
} | ||
// Remember this new `tuple` object so that we can return the same object | ||
// earlier next time. | ||
return node.tuple = t; | ||
return Object.freeze(node.tuple = t); | ||
} | ||
// Named imports work as well as `default` imports. | ||
export { tuple }; | ||
// Convenient helper for defining hidden immutable properties. | ||
@@ -84,2 +90,3 @@ function def(obj, name, value, enumerable) { | ||
// `tuple.isTuple(value)`. | ||
def(tuple.prototype, brand, true, false); | ||
function isTuple(that) { | ||
@@ -89,4 +96,2 @@ return !! (that && that[brand] === true); | ||
def(tuple.prototype, brand, true, false); | ||
tuple.isTuple = isTuple; | ||
@@ -114,3 +119,3 @@ | ||
// not a new `Array`. | ||
return Array.isArray(result) ? intern(result) : result; | ||
return Array.isArray(result) ? tuple(...result) : result; | ||
}; | ||
@@ -128,5 +133,5 @@ Object.defineProperty(tuple.prototype, name, desc); | ||
tuple.prototype.concat = function (...args) { | ||
return intern(concat.apply(toArray(this), args.map( | ||
return tuple(...concat.apply(toArray(this), args.map( | ||
item => isTuple(item) ? toArray(item) : item | ||
))); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
44315
11
634