Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial thing with broken example

  • Loading branch information...
commit 37b173803c9b457c09ebf20c0370457ece9433ed 0 parents
@substack authored
Showing with 43 additions and 0 deletions.
  1. +11 −0 example/path.js
  2. +32 −0 index.js
11 example/path.js
@@ -0,0 +1,11 @@
+var pathway = require('../');
+var xs = [
+ { x : { y : { z : 555 } } },
+ { beep : 'boop' },
+ { x : { y : { z : 444 } }, w : 4 },
+ { x : { y : 'zzz' } },
+ { x : { y : { z : 333 } } },
+ { X : { y : { z : 222 } } }
+];
+var ys = pathway(xs, [ /./, /x/i, 'y', 'z' ]);
+console.dir(ys);
32 index.js
@@ -0,0 +1,32 @@
+var concatMap = require('concat-map');
+
+module.exports = function pathway (obj, path) {
+ return path.reduce(function (nodes, p, ip) {
+ if (isRegExp(p)) {
+ return concatMap(nodes, function (node, ix) {
+ var xs = Object.keys(node).filter(function (key) {
+ return p.test(key);
+ });
+ if (xs[0]) nodes[ix] = xs[0];
+ else return [];
+
+ xs.slice(1).forEach(function (x) {
+
+ });
+ return true;
+ });
+ }
+ else {
+ return concatMap(nodes, function (node, ix) {
+ if (pi === nodes.length - 1) return node[p];
+ if (!node[p]) return [];
+ if (typeof node[p] !== 'object') return [];
+ return node[p];
+ });
+ }
+ }, [ obj ]);
+};
+
+function isRegExp (x) {
+ return Object.prototype.toString.call(x);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.