CARVIEW |
Select Language
HTTP/2 301
location: https://raw.githubusercontent.com/flesler/hashmap/master/hashmap.js
accept-ranges: bytes
age: 0
date: Sun, 27 Jul 2025 08:11:55 GMT
via: 1.1 varnish
x-served-by: cache-bom-vanm7210066-BOM
x-cache: MISS
x-cache-hits: 0
x-timer: S1753603914.037511,VS0,VE1200
vary: Accept-Encoding
x-fastly-request-id: ea06a786b8552149708c9ec26d7d89cac85e9080
content-length: 0
HTTP/2 200
cache-control: max-age=300
content-security-policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
content-type: text/plain; charset=utf-8
etag: W/"9291653729f6e0b4afc191367684f6df836d29033e208ecd93f4447449387743"
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
x-frame-options: deny
x-xss-protection: 1; mode=block
x-github-request-id: 0DB7:2CB7B7:1B7B4D:486E2F:6885DF47
content-encoding: gzip
accept-ranges: bytes
date: Sun, 27 Jul 2025 08:11:55 GMT
via: 1.1 varnish
x-served-by: cache-bom-vanm7210069-BOM
x-cache: MISS
x-cache-hits: 0
x-timer: S1753603915.288251,VS0,VE262
vary: Authorization,Accept-Encoding
access-control-allow-origin: *
cross-origin-resource-policy: cross-origin
x-fastly-request-id: ff6c0762eea047bbe853d61dfbff0da089a37187
expires: Sun, 27 Jul 2025 08:16:55 GMT
source-age: 0
content-length: 1866
/**
* HashMap - HashMap Class for JavaScript
* @author Ariel Flesler
* @version 2.4.0
* Homepage: https://github.com/flesler/hashmap
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object') {
// Node js environment
var HashMap = module.exports = factory();
// Keep it backwards compatible
HashMap.HashMap = HashMap;
} else {
// Browser globals (this is window)
this.HashMap = factory();
}
}(function() {
function HashMap(other) {
this.clear();
switch (arguments.length) {
case 0: break;
case 1: {
if ('length' in other) {
// Flatten 2D array to alternating key-value array
multi(this, Array.prototype.concat.apply([], other));
} else { // Assumed to be a HashMap instance
this.copy(other);
}
break;
}
default: multi(this, arguments); break;
}
}
var proto = HashMap.prototype = {
constructor:HashMap,
get:function(key) {
var data = this._data[this.hash(key)];
return data && data[1];
},
set:function(key, value) {
// Store original key as well (for iteration)
var hash = this.hash(key);
if ( !(hash in this._data) ) {
this.size++;
}
this._data[hash] = [key, value];
},
multi:function() {
multi(this, arguments);
},
copy:function(other) {
for (var hash in other._data) {
if ( !(hash in this._data) ) {
this.size++;
}
this._data[hash] = other._data[hash];
}
},
has:function(key) {
return this.hash(key) in this._data;
},
search:function(value) {
for (var key in this._data) {
if (this._data[key][1] === value) {
return this._data[key][0];
}
}
return null;
},
delete:function(key) {
var hash = this.hash(key);
if ( hash in this._data ) {
this.size--;
delete this._data[hash];
}
},
type:function(key) {
var str = Object.prototype.toString.call(key);
var type = str.slice(8, -1).toLowerCase();
// Some browsers yield DOMWindow or Window for null and undefined, works fine on Node
if (!key && (type === 'domwindow' || type === 'window')) {
return key + '';
}
return type;
},
keys:function() {
var keys = [];
this.forEach(function(_, key) { keys.push(key); });
return keys;
},
values:function() {
var values = [];
this.forEach(function(value) { values.push(value); });
return values;
},
entries:function() {
var entries = [];
this.forEach(function(value, key) { entries.push([key, value]); });
return entries;
},
// TODO: This is deprecated and will be deleted in a future version
count:function() {
return this.size;
},
clear:function() {
// TODO: Would Object.create(null) make any difference
this._data = {};
this.size = 0;
},
clone:function() {
return new HashMap(this);
},
hash:function(key) {
switch (this.type(key)) {
case 'undefined':
case 'null':
case 'boolean':
case 'number':
case 'regexp':
return key + '';
case 'date':
return '♣' + key.getTime();
case 'string':
return '♠' + key;
case 'array':
var hashes = [];
for (var i = 0; i < key.length; i++) {
hashes[i] = this.hash(key[i]);
}
return '♥' + hashes.join('⁞');
default:
// TODO: Don't use expandos when Object.defineProperty is not available?
if (!key.hasOwnProperty('_hmuid_')) {
key._hmuid_ = ++HashMap.uid;
hide(key, '_hmuid_');
}
return '♦' + key._hmuid_;
}
},
forEach:function(func, ctx) {
for (var key in this._data) {
var data = this._data[key];
func.call(ctx || this, data[1], data[0]);
}
}
};
HashMap.uid = 0;
// Iterator protocol for ES6
if (typeof Symbol !== 'undefined' && typeof Symbol.iterator !== 'undefined') {
proto[Symbol.iterator] = function() {
var entries = this.entries();
var i = 0;
return {
next:function() {
if (i === entries.length) { return { done: true }; }
var currentEntry = entries[i++];
return {
value: { key: currentEntry[0], value: currentEntry[1] },
done: false
};
}
};
};
}
//- Add chaining to all methods that don't return something
['set','multi','copy','delete','clear','forEach'].forEach(function(method) {
var fn = proto[method];
proto[method] = function() {
fn.apply(this, arguments);
return this;
};
});
//- Backwards compatibility
// TODO: remove() is deprecated and will be deleted in a future version
HashMap.prototype.remove = HashMap.prototype.delete;
//- Utils
function multi(map, args) {
for (var i = 0; i < args.length; i += 2) {
map.set(args[i], args[i+1]);
}
}
function hide(obj, prop) {
// Make non iterable if supported
if (Object.defineProperty) {
Object.defineProperty(obj, prop, {enumerable:false});
}
}
return HashMap;
}));