timdown / jshashtable Goto Github PK
View Code? Open in Web Editor NEWA standalone JavaScript implementation of hash table with arbitrary keys
License: Apache License 2.0
A standalone JavaScript implementation of hash table with arbitrary keys
License: Apache License 2.0
What steps will reproduce the problem?
1. Execute the following code:
var hashTable = new Hashtable();
var xmlStr = '<root><child attrName="attrValue">nodeValue</child></root>';
var xmlDoc = null;
if (window['DOMParser']) // Firefox, Chrome, Safari, etc.
{
xmlDoc = new DOMParser().parseFromString(xmlStr, 'text/xml');
}
else // For IE
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.loadXML(xmlStr);
}
var key = new Array(xmlDoc);
hashTable.put(key, 10);
var value = hashTable.get(key);
What is the expected output? What do you see instead?
In this case the value of value should 10, you will get the following exception
on IE 8:
TypeError: Object doesn't support this property or method
What version of the product are you using? On what operating system?
2.1 on Windows 7
Please provide any additional information below.
This happens because internally the function hashcode(obj) ends up executing
the following code block:
} else if (typeof obj.toString == FUNCTION) {
return obj.toString();
There are 2 problems here:
1. For arrays, Array.toString() method internally calls toString() on each
element of the array, and concates the comma separated values.
ActiveXObject('Microsoft.XMLDOM') does not have a toString() method, hence it
throws the TypeError exception.
2. On FireFox etc., the corresponding type is XMLDocument.
XMLDocument.toString() returns something like [Object Document], which clearly
is a bad hash function.
Fix:
The ideal fix is to add the right toString() methods to these objects. However
that is something that browsers need to handle. The alternative fix is to
update the hashObject(obj) code as follows:
function hashObject(obj) {
var hashCode;
if (typeof obj == "string") {
return obj;
} else if (typeof obj.hashCode == FUNCTION) {
// Check the hashCode method really has returned a string
hashCode = obj.hashCode();
return (typeof hashCode == "string") ? hashCode : hashObject(hashCode);
} else if (obj instanceof Array) {
return arrayToString(obj);
} else if (typeof obj.toString == FUNCTION) {
return obj.toString();
} else {
try {
return String(obj);
} catch (ex) {
// For host objects (such as ActiveObjects in IE) that have no toString() method and throw an error when
// passed to String()
return Object.prototype.toString.call(obj);
}
}
}
/**
* Patched Array.toString() that for XML data types, applies to correct toString transformation on it
* @param arrayObj Array object whose toString need to be computed
* @return Stringified contents of the Array
*/
function arrayToString(arrayObj)
{
// TODO: Handle Array.toString() where arrayObj is null ...
var strOut = "";
for (var i = 0; i < arrayObj.length; i++)
{
if (i != 0)
strOut += ",";
var obj = arrayObj[i];
if (window['ActiveXObject'] && obj instanceof ActiveXObject && obj['xml'] != undefined) // XML on IE
strOut += obj.xml;
else if (window['Document'] && obj instanceof Document && window['DOMParser']) // XML on Firefox, Chrome, Safari etc.
strOut += (new XMLSerializer()).serializeToString(obj);
else if (obj instanceof Array)
strOut += arrayToString(obj);
else
strOut += obj;
}
return strOut;
}
Original issue reported on code.google.com by [email protected]
on 27 Sep 2011 at 7:35
Put a non null value with key that is an array of a single date object.
Get with a key that is another array containing the same date, the value is
null.
Happens on Windows IE8 and Firefox 26.0
Original issue reported on code.google.com by [email protected]
on 27 Feb 2014 at 4:33
I made a node.js application that stores a https request
and response
object In jshashtable. After retreving the response
object from the table, whenever I try to use It, I get errors complaining that response
is not an object.
A better discription of the problem and the code can be found at: http://stackoverflow.com/questions/30469358/can-i-store-a-response-object-in-a-hashtable
Below are the contents of an extern file I made for Closure Compiler's Advanced
Compilation. These are starting to show up in projects now.
/**
* @constructor
* @param {function(Object)=} hashingFunctionParam
* @param {function(Object, Object)=} equalityFunctionParam
*/
function Hashtable(hashingFunctionParam, equalityFunctionParam) { };
/**
* @param {Object} key
* @param {Object} value
*/
Hashtable.prototype.put = function (key, value) { };
/**
* @param {Object} key
* @returns {Object?}
*/
Hashtable.prototype.get = function (key) { };
/**
* @param {Object} key
* @returns {Boolean?}
*/
Hashtable.prototype.containsKey = function (key) { };
/**
* @param {Object} value
* @returns {Boolean?}
*/
Hashtable.prototype.containsValue = function (value) { };
Hashtable.prototype.clear = function () { };
/**
* @returns {Boolean?}
*/
Hashtable.prototype.isEmpty = function () { };
/**
* @returns {Array.<Objects>}
*/
Hashtable.prototype.keys = function () { };
/**
* @returns {Array.<Objects>}
*/
Hashtable.prototype.values = function () { };
/**
* @returns {Array.<Array.<Objects>>}
*/
Hashtable.prototype.entries = function () { };
/**
* @param {Object} key
*/
Hashtable.prototype.remove = function (key) { };
/**
* @returns {number}
*/
Hashtable.prototype.size = function () { };
/**
* @param {function(Object, Object)} callback
*/
Hashtable.prototype.each = function (callback) { };
/**
* @param {Hashtable} hashtable
* @param {function(Object, Object, Object)} conflictCallback
*/
Hashtable.prototype.putAll = function (hashtable, conflictCallback) { };
/**
* @returns {Hashtable}
*/
Hashtable.prototype.clone = function() { };
Original issue reported on code.google.com by [email protected]
on 21 Jan 2011 at 4:33
What steps will reproduce the problem?
1. var s = new HashSet(undefined, function(a, b) { return a.id === b.id; }), o1
= {id: 1, value: "x"}, o2 = {id: 1, value: "y"};
2. s.add(o1); alert(s.values()[0].value); // alerts "x"
3. s.add(o2); alert(s.values()[0].value); // I would like/expect it to alert
"y" but it alerts "x" instead
Original issue reported on code.google.com by [email protected]
on 17 Oct 2010 at 4:12
The documented complement function is not implemented right?
Original issue reported on code.google.com by [email protected]
on 5 Jun 2012 at 3:10
When you force evaluation based on object equality, hashset entries are not
replaced. In jshashtable.js,
this:
if (bucketEntry) {
// This bucket entry is the current mapping of key to value, so replace old value and we're done.
oldValue = bucketEntry[1];
bucketEntry[1] = value;
really needs to be:
if (bucketEntry) {
// This bucket entry is the current mapping of key to value, so replace old value and we're done.
bucketEntry[0] = key;
oldValue = bucketEntry[1];
bucketEntry[1] = value;
Please see the attached patch (includes an additional test that exposes the
problem. All tests are green).
-Steve
Original issue reported on code.google.com by [email protected]
on 24 Feb 2011 at 9:03
Attachments:
What steps will reproduce the problem?
1. var options = {
replaceDuplicateKey: false
};
var typesHash = new Hashtable(options);
typesHash.put("A", "string");
typesHash.put("A", "string1");
typesHash.put("A", "string2");
typesHash.put(1, "number");
2. console.log(typesHash.get("A"))
3.
What is the expected output? What do you see instead?
string,string1,string2
What version of the product are you using? On what operating system?
3. Windows 7
Please provide any additional information below.
replaceDuplicateKey: false should hold all the values associated with the key.
But I failed to see those stored values. I can only see the latest value. So it
seems to me that replaceDuplicateKey is not working in the new release.
Original issue reported on code.google.com by [email protected]
on 13 Feb 2015 at 5:37
we have tried , object.assign and clone method , but it does not work
Nice work, all fundamental functions are included. :)
Maybe it would be possible to extend jshashtable with an additional function
'toQueryString' in the API? (in Prototype
http://www.prototypejs.org/api/hash/toQueryString)
Cheers,
Berny
For now, I'm using the following code:
var result="";
var keys = hashtable.keys();
for (var i=0; i<keys.length; i++) {
result+= encodeURIComponent(keys[i])+"="+encodeURIComponent(hashtable.get(keys[i]))+"&";
}
return result.substring(0, result.length-1);
Original issue reported on code.google.com by Bernhard.Riedl
on 4 Sep 2010 at 3:51
Would you consider publishing in bower and/or npm?
I saw that there is a separate repo for a npm package but it's unclear to me if it is the same or different from this repo.
Thanks!
The following code in IE gives an error when it should create an entry in
the hash table:
var h = new Hashtable();
var x = new ActiveXObject("Msxml2.XMLHTTP.6.0");
h.put(x, "Some value");
// Error thrown: Object doesn't support this property or method
Original issue reported on code.google.com by [email protected]
on 21 Mar 2010 at 11:53
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.