From a9a8d8ff04e1383769cc8ee0b52bb5315872c642 Mon Sep 17 00:00:00 2001 From: dpchn Date: Fri, 5 Feb 2016 13:17:12 +0530 Subject: [PATCH 1/2] Issue solved of Rewrite of Select query --- src/select.coffee | 15 +++++++++++++++ test/selectquery.js | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/select.coffee create mode 100644 test/selectquery.js diff --git a/src/select.coffee b/src/select.coffee new file mode 100644 index 0000000..608920e --- /dev/null +++ b/src/select.coffee @@ -0,0 +1,15 @@ + + + _ = require 'underscore' + jequel={} + + jequel.query = (obj,path) -> + + destPath = 'obj' + path = _.values(path) + for i in path[0].split '/' + destPath += "['#{i}']" if i isnt " " + + return eval destPath + + module.exports = jequel diff --git a/test/selectquery.js b/test/selectquery.js new file mode 100644 index 0000000..c0fb394 --- /dev/null +++ b/test/selectquery.js @@ -0,0 +1,21 @@ + var chai = require('chai'); + var expect = require('chai').expect; + var should = require('chai').should; + var jequel = require("../src/select"); + + + + describe("select", function() { + var obj = [{ + a: [{ + b: 'c' + }] + }]; + it("Should return a value", function() { + + expect(jequel.query(obj,{select:"0/a/0"})).to.deep.equal( + {b:'c'} + ) + }); + + }) From e751d791ac4824521e189a84ebea7c1370acf185 Mon Sep 17 00:00:00 2001 From: dpchn Date: Sun, 7 Feb 2016 14:49:31 +0530 Subject: [PATCH 2/2] Issue solved #3 --- src/bfsTraverse.coffee | 46 ++++++++++++++++++++++++++++++++++++++++++ test/bfsTraverse.js | 16 +++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/bfsTraverse.coffee create mode 100644 test/bfsTraverse.js diff --git a/src/bfsTraverse.coffee b/src/bfsTraverse.coffee new file mode 100644 index 0000000..b746425 --- /dev/null +++ b/src/bfsTraverse.coffee @@ -0,0 +1,46 @@ + + + # bfsTraverse + # + # It will traverse all nodes of object + # + # Arguments of functionToCall like that + # + # i.e... objectToTraverse = [{a:[{b:1}]}] + # first time + # { value:[{a:[{b:1}]}] + # path:"" + # } + # + # second time + # {value:{a:[{b:1}]} + # path:0 + # } + # + # third time + # {value:[{b:1}] + # path:0/a + # } + + + + + + + bfsTraverse = (objectToTraverse, functionToCall) -> + q = [objectToTraverse] + while q.length isnt 0 + + # This is the parent node being popped out of the Queue. + parentNode = {value: q.shift(), path:''} + + + if typeof parentNode.value is 'object' + for key,nodeValue of parentNode.value + parentNode.path = if parentNode.path then parentNode.path + '/' + key else key + parentNode.value = nodeValue + functionToCall nodeValue, parentNode + q.push nodeValue + + + module.exports = bfsTraverse diff --git a/test/bfsTraverse.js b/test/bfsTraverse.js new file mode 100644 index 0000000..311186b --- /dev/null +++ b/test/bfsTraverse.js @@ -0,0 +1,16 @@ +var chai = require('chai'); +var expect = require('chai').expect; +var should = require('chai').should; +var bfsTraverse = require("../src/bfsTraverse"); + + + +describe("bfsTraverse()", function() { + bfsTraverse([{ + a: [{ + b: 'c' + }] + }],function(node, parent){ + console.log('node', node, 'parent', parent) + }) +})