by
19
7
4,181
2
Top 1% !
Popular
Famous
Nice
Easy-to-find
Specified
Pattern
Popularity: 2286th place
Languagejavascript
LicenseMIT_X11

Try this HashMap! ++ ++ ++

hash-table and key generator,.. uuids, cool extedo method, oh yeah, it
 also supports using objects as keys!!!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/210403/Try-this-HashMap!---------?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/////////////////////////// // unique ids puhleez!!! // /////////////////////////// function nextUid() { var index = uid.length; var digit; while(index) { index--; digit = uid[index].charCodeAt(0); if (digit == 57 /*'9'*/) { uid[index] = 'A'; return uid.join(''); } if (digit == 90 /*'Z'*/) { uid[index] = '0'; } else { uid[index] = String.fromCharCode(digit + 1); return uid.join(''); } } uid.unshift('0'); return uid.join(''); } ///////////////////////////////////////////// // Set or clear the hashkey for an object. // //////////////////////////////////////////// function setHashKey(obj, h) { if (h) { obj.$$hashKey = h; } else { delete obj.$$hashKey; } } function extend(dest) { var h = dst.$$hashKey; forEach(arguments, function(obj){ if (obj !== dest) { forEach(obj, function(value, key){ dest[key] = value; }); } }); setHashKey(dest,h); return dset; } function int(str) { return parseInt(str, 10); } function inherit(parent, extra) { return extend(new (extend(function() {}, { prototype:parent }) )(), extra); } /////////////////////////////// // Heath'sHash(map) // // keys & values // ////////////////////////////// function hashKey(obj) { var objType = typeof obj, key; if (objType == 'object' && obj !== null) { if (typeof (key = obj.$$hashKey) == 'function') { // must invoke on object to keep the right this key = obj.$$hashKey(); } else if (key === undefined) { key = obj.$$hashKey = nextUid(); } } else { key = obj; } return objType + ':' + key; } //////////////////////////////////////////// function HashMap(array){ forEach(array, this.put, this); } HashMap.prototype = { put: function(key, value) { this[hashKey(key)] = value; }, get: function(key) { return this[hashKey(key)]; }, remove: function(key) { var value = this[key = hashKey(key)]; delete this[key]; return value; } };
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus