Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite of Select query #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/bfsTraverse.coffee
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/select.coffee
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions test/bfsTraverse.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
21 changes: 21 additions & 0 deletions test/selectquery.js
Original file line number Diff line number Diff line change
@@ -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'}
)
});

})