1 | /*
|
2 | * Copyright 2013 the original author or authors
|
3 | * @license MIT, see LICENSE.txt for details
|
4 | *
|
5 | * @author Scott Andrews
|
6 | */
|
7 |
|
8 | (function (define) {
|
9 | ;
|
10 |
|
11 | define(function (/* require */) {
|
12 |
|
13 | return {
|
14 |
|
15 | /**
|
16 | * Find objects within a graph the contain a property of a certain name.
|
17 | *
|
18 | * NOTE: this method will not discover object graph cycles.
|
19 | *
|
20 | * @param {*} obj object to search on
|
21 | * @param {string} prop name of the property to search for
|
22 | * @param {Function} callback function to receive the found properties and their parent
|
23 | */
|
24 | findProperties: function findProperties(obj, prop, callback) {
|
25 | if (typeof obj !== 'object' || obj === null) { return; }
|
26 | if (prop in obj) {
|
27 | callback(obj[prop], obj, prop);
|
28 | }
|
29 | Object.keys(obj).forEach(function (key) {
|
30 | findProperties(obj[key], prop, callback);
|
31 | });
|
32 | }
|
33 |
|
34 | };
|
35 |
|
36 | });
|
37 |
|
38 | }(
|
39 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
|
40 | // Boilerplate for AMD and Node
|
41 | ));
|