Last active
August 29, 2015 14:18
-
-
Save andrewluetgers/7f0f043ea2224c315ad0 to your computer and use it in GitHub Desktop.
Map data structure with handy deepGet and deepSet methods https://jsfiddle.net/aq93v4g7/7/
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
function Map(keys, values) {return new _Map(keys, values)} | |
function _Map(keys, values) {this.clear(keys, values);} | |
_Map.prototype = { | |
set: function(key, value) { | |
var i = this.indexOf(key); | |
this._keys[i] = key; | |
this._values[i] = value; | |
return i !== this._keys.length; | |
}, | |
get: function(key) { | |
var i = this.indexOf(key); | |
return this._values[i]; | |
}, | |
remove: function(key) { | |
var i = this.indexOf(key); | |
if (i !== this._keys.length) { | |
this._keys.splice(i, 1); | |
this._values.splice(i, 1); | |
} | |
return this; | |
}, | |
clear: function(keys, values) { | |
this._keys = keys || []; | |
this._values = values || []; | |
return this; | |
}, | |
indexOf: function(key) { | |
var i = this._keys.indexOf(key); | |
return i === -1 ? this._keys.length : i; | |
}, | |
deepGet: function(keyPath) { | |
var current = this, | |
keyPath = keyPath || [], | |
key; | |
for(var i= 0, len=keyPath.length; i<len; i++) { | |
key = keyPath[i]; | |
current = current.get(key); | |
if (current === undefined) { | |
break; | |
} | |
} | |
return current; | |
}, | |
deepSet: function(keyPath, value) { | |
var current = this, | |
keyPath = keyPath || [], | |
_cur; | |
keyPath.forEach(function(key, index) { | |
if (index + 1 === keyPath.length) { | |
current.set(key, value); | |
} else { | |
_cur = current.get(key); | |
if (!_cur) { | |
_cur = Map(); | |
current.set(key, _cur); | |
} | |
current = _cur; | |
} | |
}); | |
return this; | |
} | |
}; |
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 keys = [{foo: 1}, "foo", [1,2,3], function(foo) {console.log(foo)}]; | |
var _pathListeners = Map( | |
[keys[0], keys[1]], | |
[ | |
// keyPath: [{foo: 1}] | |
Map( | |
["__listeners__"], | |
// keyPath: [{foo: 1}] callbacks | |
[[function() {console.log("w00t {foo: 1}")}]] | |
), | |
// keyPath: ["foo"] | |
Map( | |
["__listeners__", "bar", keys[2]], | |
[ | |
// keyPath: ["foo"] callbacks | |
[function() {console.log("w00t foo")}], | |
// keyPath: ["foo", "bar"] | |
Map( | |
["__listeners__"], | |
// keyPath: ["foo", "bar"] callbacks | |
[[function() {console.log("w00t foo bar")}]] | |
), | |
// keyPath: ["foo", [1,2,3]] | |
Map( | |
["__listeners__", keys[2]], | |
[ | |
// keyPath: ["foo", [1,2,3]] callbacks | |
[ | |
function() {console.log("w00t foo [1,2,3]")}, | |
function() {console.log("w00t foo [1,2,3] again")} | |
], | |
// keyPath: ["foo", [1,2,3], function(foo) {console.log(foo)}] | |
Map( | |
["__listeners__"], | |
// keyPath: ["foo", [1,2,3], function(foo) {console.log(foo)}] callbacks | |
[[function() {console.log("w00t for long keyPath")}]] | |
) | |
] | |
) | |
] | |
) | |
] | |
); | |
console.log(_pathListeners); | |
console.log(_pathListeners.deepGet(["foo"])); | |
var firstLevelListeners = _pathListeners.deepGet([keys[0], "__listeners__"]); | |
firstLevelListeners[0](); | |
var deepListeners = _pathListeners.deepGet([keys[1], keys[2], "__listeners__"]); | |
deepListeners[0](); | |
_pathListeners.deepSet([keys[1], keys[2], keys[0], "__listeners__"], [function() {console.log("new callback")}]); | |
var newListeners = _pathListeners.deepGet([keys[1], keys[2], keys[0], "__listeners__"]); | |
newListeners[0](); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment