Skip to content

Instantly share code, notes, and snippets.

@elycruz
Created February 18, 2014 01:10
Show Gist options
  • Save elycruz/9062639 to your computer and use it in GitHub Desktop.
Save elycruz/9062639 to your computer and use it in GitHub Desktop.
/**
* Created by edelacruz on 2/17/14.
* A shim for generating map objects for hash maps and array maps
* (arrays of 2 item arrays [0] 'key', [1] 'value')
*/
define(['es6-shim', 'checkjs'], function (_namespace) {
function fromArray (arrayMap, recursive) {
recursive = recursive || false;
var map = new Map(), value;
arrayMap.forEach(function(x) {
value = classOfIs(x[1], 'Array') && recursive ? fromArray(x[1]) : x[1];
map.set(x[0], value);
});
return map;
}
function fromHashMap (hashMap, recursive) {
recursive = recursive || false;
var map = new Map(), value;
Object.keys(hashMap).forEach(function(x) {
value = classOfIs(x[1], 'Object') && recursive ? fromHashMap(x[1]) : x[1];
map.set(x[0], value);
});
return map;
}
Map.prototype.fromArrayMap = fromArray;
Map.prototype.fromHashMap = fromHashMap;
Map.prototype._namespace = _namespace;
Set.prototype.fromArray = function () {
var arg0 = arguments[0],
_set = new Set();
if (classOfIs(arg0, 'Array')) {
arg0.forEach(function (x) { _set.add(x); });
}
else {
argsToArray(arguments).forEach(function (x) {_set.add(x)});
}
return _set;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment