Last active
October 15, 2022 14:17
-
-
Save Quaese/5b75307a312c3c1c7fbb to your computer and use it in GitHub Desktop.
Tipps + Tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hashtable = (function() { | |
var hashCode = function(value) { | |
var hash = 0, | |
i, chr, len; | |
value = value.toString(); | |
if (value.length === 0) { | |
return hash; | |
} | |
for (i = 0, len = value.length; i < len; i++) { | |
chr = value.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; // Convert to 32bit integer | |
} | |
return hash; | |
}, | |
hashtable = function(opts) { | |
return new hashObj(opts); | |
}, | |
hashObj = function(opts) { | |
var storage = {}; | |
// this.settings = { | |
// name: 'default' | |
// }; | |
// this.extend(opts); | |
// this.public.count++; | |
return { | |
set: function(key, val) { | |
if (arguments.length < 2) { | |
throw new Error('Missing Parameter'); | |
} | |
/* Task: add errorhandling | |
if (typeof key !== 'string' && typeof key !== 'number') { | |
throw new Error('Invalid Key Type'); | |
} | |
*/ | |
// Task: Hashtable for every filetype | |
key = hashCode(key); | |
// Task: Add the ability to store multiple values on a single key | |
if (Object.prototype.toString.call(storage[key]) === '[object Array]') { | |
storage[key].push(val); | |
} else { | |
storage[key] = [val]; | |
} | |
return hashtable; | |
}, | |
get: function(key) { | |
// Task: add feature to getter for returning object of storage | |
if (key === undefined) { | |
var Copy = function () {}; | |
Copy.prototype = storage; | |
return new Copy(); | |
} | |
key = hashCode(key); | |
return (storage[key].length === 1) ? storage[key][0] : storage[key]; | |
} | |
}; | |
}; | |
// Klassenvariable (private) | |
// hashtable._private = { | |
// }; | |
// hashtable.fn = hashObj.prototype = { | |
// // Klassenvariable (public) | |
// public: { | |
// }, | |
// myTester : function() { | |
// return this; | |
// }, | |
// extend : function(hash){ | |
// for(var key in hash) | |
// this.settings[key] = hash[key]; | |
// | |
// return this; | |
// }, | |
// addEvent : function(objListener, strEvtType, fnFunction, blnCaption){ | |
// if(objListener.addEventListener){ | |
// objListener.addEventListener(strEvtType, fnFunction, blnCaption); | |
// }else if(objListener.attachEvent){ | |
// objListener.attachEvent("on" + strEvtType, fnFunction); | |
// }else{ | |
// var objFn = objListener["on" + strEvtType]; | |
// objListener["on" + strEvtType] = function(){ | |
// objFn(); | |
// fnFunction(); | |
// } | |
// } | |
// return this; | |
// }, | |
// getPi: function(){ | |
// return qpObj._private.pi; | |
// }, | |
// setPi: function(pi){ | |
// qpObj._private.pi = pi; | |
// return this; | |
// } | |
// } | |
return hashtable; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hashTable = function () { | |
// Task: Hashtable for every filetype | |
var hashCode = function (value) { | |
var hash = 0, i, chr, len; | |
value = value.toString(); | |
if (value.length === 0) { | |
return hash; | |
} | |
for (i = 0, len = value.length; i < len; i++) { | |
chr = value.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; // Convert to 32bit integer | |
} | |
return hash; | |
}, | |
fnHash = function () { | |
var storage = {}, | |
methods = { | |
set: function (key, val) { | |
if (arguments.length < 2) { | |
throw new Error('Missing Parameter'); | |
} | |
/* Task: add errorhandling | |
if (typeof key !== 'string' && typeof key !== 'number') { | |
throw new Error('Invalid Key Type'); | |
} | |
*/ | |
// Task: Hashtable for every filetype | |
key = hashCode(key); | |
// Task: Add the ability to store multiple values on a single key | |
if (Object.prototype.toString.call(storage[key]) === '[object Array]') { | |
storage[key].push(val); | |
} else { | |
storage[key] = [val]; | |
} | |
return methods; | |
}, | |
get: function (key) { | |
// Task: add feature to getter for returning object of storage | |
if (key === undefined) { | |
var Copy = function () {}; | |
Copy.prototype = storage; | |
return new Copy(); | |
} | |
key = hashCode(key); | |
return (storage[key].length === 1) ? storage[key][0] : storage[key]; | |
} | |
}; | |
return methods; | |
}; | |
return fnHash(); | |
}; | |
/*var test = hashTable(), | |
myObject = {foo: 'bar'}, | |
myFunction = function () { | |
console.log('Hello World'); | |
}; | |
test.set(myObject, 10).set(myFunction, 20).get();*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment