{"dist":{"shasum":"bd2b186db808e32182121f3400abd16f2208b32a","tarball":"https://registry.npmjs.org/harmony-collections/-/harmony-collections-0.3.8.tgz","integrity":"sha512-2b5WkpPhYFz9JMAcJZTBEDClDoicR483zW21wEQWPR16agtXAHAZYW8XLr8yUjTO3Vnow0h1PDPSmyCHxwNuDQ==","signatures":[{"sig":"MEYCIQCwYFJOK+uoMxqhKbeX36Ea9mZTitKBXvvUFjf1nOPtnwIhALAfIGyADirOLUmf0BwXD5KxWMfQd9DCHaZBI4yVWY4O","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"harmony-collections.js","name":"harmony-collections","author":{"name":"Brandon Benvie","email":"http://bbenvie.com"},"readme":"# Harmony Collections Shim\n\nUse the new __Map__, __Set__, and __WeakMap__ from the upcoming ES6 standard right now! This shim provides full functionality for these collections and delivers the benefits of using them.\n\n## Compatability\n\nWorks with IE9+, Chrome, Firefox, Safari, untested in Opera. __IE8 support has been recently added but is experimental.__\n\n## Install/Use\n\nIf using node, install via:\n\n    npm install harmony-collections\n\nIn the browser, include __harmony-collection.js__ or __harmony-collections.min.js__ and Map, WeakMap, Set, and HashMap will be exposed on the window. (you can also define `window.exports` which will cause them to end up there).\n\n## Overview\n\nES6 Collections provide a new core weapon to your JS arsenal: objects as keys. This allows you to do the following awesome things: store private data \"on\" public objects, private properties, secretly \"tag\" objects, namespace properties, access controlled properties, check object uniqueness in `O(1)` time complexity.\n\n### WeakMap Garbage Collection Semantics\n\nThe benefit of using WeakMaps is enhanced garbage collection. In a WeakMap, the only reference created is key -> value, so it's possible for a key/value in a WeakMap to be garbage collected while the WeakMap they're in still exists! Compare this to an Array, where all items in the Array will not be garbage collected as long as the Array isn't. This forces either explicit management of  object lifespans or, more commonly, simply results in memory leaks.\n\nFor example, data stored using jQuery.data can never be garbage collected unless explicitly nulled out, because it is stored in a container that strongly references the items held inside. Using a WeakMap, it's possible to associate data with an element and have the data destroyed when the element is -- without memory leaking the element; i.e. `weakmap.set(element, { myData: 'gc safe!' })`. jQuery.data (every library has similar functionality) prevents the *element* from memory leaking by using a numeric id, but this does nothing for the __data__ that is stored.\n\n## Detailed Examples\n\n### Map/WeakMap\n```javascript\n// reusable storage creator\nfunction createStorage(){\n  var store = new WeakMap;\n  return function(o){\n    var v = store.get(o);\n    if (!v) store.set(o, v = {});\n    return v;\n  };\n}\n\n// allows private/namespaced properties for the objects\nvar _ = createStorage();\n\nfunctioon Wrapper(element){\n  var _element = _(element);\n  if (_element.wrapper)\n    return _element.wrapper;\n\n  _element.wrapper = this;\n  _(this).element = element;\n}\n\nWrapper.prototype = {\n  get classes(){\n    return [].slice.call(_(this).element.classList);\n  },\n  set classes(v){\n    _(this).element.className = [].concat(v).join(' ');\n  }\n};\n```\n\n### Set\nA Set is similar to an Array in what it stores, but different in how. A Set's values are unique. Determining whether an item is in a Set is `O(1)` but `O(n)` for an Array. An example of where this is useful is in implementing `Array.prototype.unique` that works with objects.\n\nBoth of the following will output the same result, however the Set version is `O(n)` and the one using indexOf is `O(n^2)`. For an array taking 30 seconds using the set, an __*hour*__ is required for indexOf.\n\n```javascript\nfunction uniqueUsingIndexOf(array){\n  return array.filter(function(item, index){\n    return array.lastIndexOf(item) > index;\n  });\n}\n\nfunction uniqueUsingSet(array){\n  var seen = new Set;\n  return array.filter(function(item){\n    if (!seen.has(item)) {\n      seen.add(item);\n      return true;\n    }\n  });\n}\n```\n\n\n## API Reference\n\n* Collections may be inherited from. Initialize objects via `[WeakMap|Map|Set].call(obj)`.\n* Iteration is insertion ordered.\n\n\n### WeakMap\n\n__Non-primitives__ are valid keys. Objects, functions, DOM nodes, etc.\n\nWeakMaps require the use of objects as keys; primitives are not valid keys. WeakMaps have no way to enumerate their keys or values. Because of this, the only way to retrieve a value from a WeakMap is to have access to both the WeakMap itself as well as an object used as a key.\n\n* `new WeakMap(iterable)` Create a new WeakMap populated with the iterable. Accepts *[[Key, Value]...]*, *Array*, *Iterable*.\n* `WeakMap#set(key, value)` Key must be non-primitive. Returns undefined.\n* `WeakMap#get(key)` Returns the value that key corresponds to the key or undefined.\n* `WeakMap#has(key)` Returns boolean.\n* `WeakMap#delete(key)` Removes the value from the collection and returns boolean indicating if there was a value to delete.\n\n\n### HashMap\n\n__Primitives__ are valid keys. Exact value is used; e.g. `'0'/-0/0` are all different keys.\n\nHashMap is not standard, but is used to implement Map and is exported as a bonus. Has the same API as Map except it only allows primitive keys.\n\n* `new HashMap(iterable)` Create a new HashMap populated with the iterable. Accepts *[[Key, Value]...]*, *Iterable*.\n* `HashMap#set(key, value)` Key must be primitive. Returns undefined.\n* `HashMap#get(key)` Returns the value the key corresponds to or undefined.\n* `HashMap#has(key)` Returns boolean.\n* `HashMap#delete(key)` Removes the value from the collection and returns boolean indicating if there was a value to delete.\n* `HashMap#size()` Returns the number of items in the collection.\n* `HashMap#forEach(callback, context)` Loop through the collection raising callback for each.\n\n\n### Map\n\n__All possible values__ are valid keys, including -0, undefined, null, and NaN.\n\nMaps do not have the same garbage collection benefits that WeakMaps do, but instead are iterable and also accept primitive keys. This means any value can be a Map key.\n\n* `new Map(iterable)` Create a new Map populated with the iterable. Accepts *[[Key, Value]...]*, *Array*, *Iterable*.\n* `Map#set(key, value)` Key is any value including objects. Returns undefined.\n* `Map#get(key)` Returns the value the key maps to or undefined.\n* `Map#has(key)` Returns boolean.\n* `Map#delete(key)` Removes the key and value from the collection if found. Returns true.\n* `Map#size()` Returns the number of items in the collection.\n* `Map#forEach(callback, context)` Loop through the collection raising callback for each.\n\n\n### Set\n\nSets are similar to arrays but enforce uniqueness of values. Adding the same value twice will only result in one being added to the set.\n\n* `new Set(iterable)` Create a new Set populated with the iterable. Accepts *Array*, *Iterable*.\n* `Set#add(value)` Inserts a value of any type into the set if it's not already in the set.\n* `Set#has(value)` Returns boolean.\n* `Set#delete(value)` Removes the value from the collection and returns boolean indicating if there was a value to delete.\n* `Set#size()` Returns the number of items in the collection.\n* `Set#forEach(callback, context)` Loop through the collection raising callback for each.\n\n\n\n## License\n\n(The MIT License)\nCopyright (c) 2012 Brandon Benvie <http://bbenvie.com>\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files\n(the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included with all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE\nFOR ANY  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH\nTHE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","license":"MIT","_npmUser":{"name":"benvie","email":"brandon@bbenvie.com"},"homepage":"http://benvie.github.com/harmony-collections","keywords":["harmony","collection","weakmap","map","set","ecmascript","es6","shim","garbage collection","gc"],"repository":{"url":"https://github.com/Benvie/harmony-collections","type":"git"},"_npmVersion":"1.1.61","description":"Shim to provide Map, Set, and WeakMap if they're not available, with non-leaky O(1) lookup WeakMaps","maintainers":[{"name":"benvie","email":"brandon@bbenvie.com"}],"dependencies":{},"devDependencies":{"tap":"*"},"_id":"harmony-collections@0.3.8","version":"0.3.8"}