Created
May 8, 2015 02:17
-
-
Save dg3feiko/2ccb8c3735309a2f8e75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 search = module.exports.search = function search(object, path, acc_path) { | |
if (typeof path === "string") { | |
path = path.split("."); | |
} | |
if (!(path instanceof Array) || path.length === 0) { | |
return; | |
} | |
path = path.slice(); | |
var key = path.shift(); | |
if (typeof object !== "object" || object === null) { | |
return; | |
} | |
if (key === "*") { | |
key = ".*"; | |
} | |
if (typeof key === "string") { | |
key = new RegExp(key); | |
} | |
if (path.length === 0) { | |
return Object.keys(object).filter(key.test.bind(key)).map(function (k) { | |
return {obj: object[k], path: acc_path ? acc_path + "." + k : k}; | |
}); | |
} else { | |
return Array.prototype.concat.apply([], Object.keys(object).filter(key.test.bind(key)).map(function (k) { | |
return search(object[k], path, acc_path ? acc_path + "." + k : k); | |
})); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment