Skip to content

Instantly share code, notes, and snippets.

@JLChnToZ
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JLChnToZ/b05f5290f590116de6f5 to your computer and use it in GitHub Desktop.
Save JLChnToZ/b05f5290f590116de6f5 to your computer and use it in GitHub Desktop.
I don't know when it will be used, but just write it out...
Object::seek = (tree) ->
tree = tree() if typeof tree is "function"
tree = tree.split "." if typeof tree is "string"
tree = [tree] unless tree instanceof Array
iterate = @
iterate = iterate[i] for i in tree
iterate
Object::override = (tree, newStuff, noExec) ->
tree = tree() if typeof tree is "function"
tree = tree.split "." if typeof tree is "string"
tree = [tree] unless tree instanceof Array
noExec = noExec() if typeof noExec is "function"
iterate = @
iterate = iterate[i] for i in tree[0..-2]
lastFragment = tree[tree.length - 1]
if not noExec and typeof newStuff is "function"
original = iterate[lastFragment]
if typeof original is "function"
iterate[lastFragment] = ->
args = Array::slice.call arguments
args.unshift original
newStuff.apply @, args
else
iterate[lastFragment] = newStuff original
else
iterate[lastFragment] = newStuff
@
Function::wrap = (fn) ->
original = @
->
args = Array::slice.call arguments
args.unshift original
fn.apply @, args
Object.prototype.seek = function(tree) {
if(typeof tree === "function") tree = tree();
if(typeof tree === "string") tree = tree.split(".");
if(!(tree instanceof Array)) tree = [tree];
var iterate = this;
for(var i = 0; i < tree.length; i++)
iterate = iterate[tree[i]];
return iterate;
};
Object.prototype.override = function(tree, newStuff, noExec) {
if(typeof tree === "function") tree = tree();
if(typeof tree === "string") tree = tree.split(".");
if(!(tree instanceof Array)) tree = [tree];
if(typeof noExec === "function") noExec = noExec();
var iterate = this;
for(var i = 0; i < tree.length - 2; i++)
iterate = iterate[tree[i]];
var lastFragment = tree[tree.length - 1];
if(!noExec && typeof newStuff === "function") {
var original = iterate[lastFragment];
if(typeof original === "function")
iterate[lastFragment] = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(original);
return newStuff.apply(this, args);
};
else
iterate[lastFragment] = newStuff(original);
} else
iterate[lastFragment] = newStuff;
return this;
};
Function.prototype.wrap = function(fn) {
var original = this;
return function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(original);
return fn.apply(this, args);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment