Skip to content

Commit

Permalink
finished initial implementation on getPath. #26
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar1989 committed Jul 6, 2015
1 parent 6daf6ea commit b0bb899
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
6 changes: 6 additions & 0 deletions tests/data/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ module.exports = [
"id": "b_39562",
"questions": [],
"subblocks": [],
"randomize": false
},
{
"id": "b_99223",
"questions": [],
"subblocks": [],
"randomize": true
}
],
Expand Down
43 changes: 30 additions & 13 deletions tests/testSurvey.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,39 @@ var survey = Immutable.fromJS(data);
var _blockMap = Immutable.fromJS({
"b_41444": "b_90914",
"b_53209": "b_10101",
"b_39562": "b_53209"
"b_39562": "b_53209",
"b_99223": "b_53209"
});

function getBlockIndex(blockId, parentBlock = survey, path = []) {
var isSubblock = _blockMap.has(blockId);
if (!isSubblock) {
let index = parentBlock.findIndex(b => b.get('id') === blockId);
return path.concat(index);
}
function getPath(blockID) {
var getIDsList = (id, path = []) => {
if (!_blockMap.has(id)) {
return path.concat([id]).reverse()
}
return getIDsList(_blockMap.get(id), path.concat([id]));
};

var [rootID, ...restIDs] = getIDsList(blockID);
var path = [survey.findIndex(b => b.get('id') === rootID)];

return restIDs.reduce((path, id) => {
let index = survey.getIn(path.concat(['subblocks']))
.findIndex(b => b.get('id') === id);
return path.concat(['subblocks', index]);
}, path);
}

// test cases
describe('Immutable', function() {
describe("get()", function() {
it("should return the block index of top block correctly", function() {
assert.equal(survey.getIn([0, 'id']), "b_10101");
});
describe("getPath", function() {
it("should return the index paths correctly", function() {
assert.deepEqual(getPath('b_10101'), [0]);
assert.deepEqual(getPath('b_90914'), [1]);
assert.deepEqual(getPath('b_41444'), [1, 'subblocks', 0]);
assert.deepEqual(getPath('b_53209'), [0, 'subblocks', 0]);
assert.deepEqual(getPath('b_39562'), [0, 'subblocks', 0, 'subblocks', 0]);
assert.deepEqual(getPath('b_99223'), [0, 'subblocks', 0, 'subblocks', 1]);
});
it("should get the correct block", function() {
var block = survey.getIn(getPath('b_39562'))
assert(!block.get('randomize'));
});
});

0 comments on commit b0bb899

Please sign in to comment.