What
JavaScript hashes are great, but they don't support non-string keys. And while there's Array
, there's no Set
data structure. ECMAScript 6 will rectify these. But until it's widely available, let's use some temporary stuff.
Install
npm install qdds
bower install qdds
Usages
Map
map = mapmap // throw Error if k doesn't existmapmapmapall // all [k, v] pairsmap // all keys
Set
set = setsetsetsetall
More methods
These methods are also available for both Map
and Set
: every
, filter
, find
, forEach
, map
, reduce
, reduceRight
, some
. This works by calling to the corresponding method of Array
, if one exists.
Extension
The default strategy does not require you to implement any sort of hashCode()
or equals()
methods, like in Java or C#. It just works, in its quick & dirty way.
If you ever need more than the default algorithm, you can extend both Map
and Set
by supplying an object with two methods: hash(k)
and equal(k)
.
map = customStrategyset = customStrategy
For example, this example (extracted from the tests) shows a super trivial Java/C# like algorithm. This is just to give you an idea, don't use it.
java_like_strategy = : khashCode : k1equalsk2preferences = java_like_strategy : : -> @name + @age : @name is othername and @age is otherage lang_prefs = languages: "coffee""js"p1 = "guy"30p2 = "guy"30preferencessetp1lang_prefsexpectpreferencesgetp2toequallang_prefs