Giter Site home page Giter Site logo

jshashtable's People

Contributors

criztovyl avatar timdown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jshashtable's Issues

Hashtable does not accept array objects containing XML document types as keys

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

Hashtable key array of one date

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

Extern File for Closure Compiler

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

Suggestion: HashSet.add should always update/set key value, regardless of equality

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

  • Merged into: #5

Complemented

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

(bug report + fix) jshashset entries are not replaced when using equalityFunction

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:

replaceDuplicateKey:false is not working

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

API function toQueryString

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

package manager

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.