Skip to content

Instantly share code, notes, and snippets.

@dg3feiko
Created May 8, 2015 02:17
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 dg3feiko/2ccb8c3735309a2f8e75 to your computer and use it in GitHub Desktop.
Save dg3feiko/2ccb8c3735309a2f8e75 to your computer and use it in GitHub Desktop.
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