Skip to content

Commit

Permalink
feat: support slicing in projections
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Nov 16, 2018
1 parent a4fe538 commit e39984f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ exports[`interpreter correct target code $?[3] 1`] = `"(function(_) { return (fu

exports[`interpreter correct target code $?[3] 2`] = `"(function(_) { return (function(input) { return _.projection(input, [3], true)})})"`;

exports[`interpreter correct target code $[1:3] 1`] = `"(function(_) { return (function(input) { return _.projection(input, [[1,3]], false)})})"`;

exports[`interpreter correct target code ($x + $foobar | [$, $foobar]) where $x = 3 $foobar = ($x + 1) 1`] = `"(function(_) { return (function(input) { return ((function() {_ = _.assign('x', (3), _); _ = _.assign('foobar', ((_.get('x')+1)), _); return (((function (input) {return [input, _.get('foobar')]})(_.get('x')+_.get('foobar'))))})())})})"`;

exports[`interpreter correct target code ([1, 2] | foo) where $foo = $reverse 1`] = `"(function(_) { return (function(input) { return ((function() {_ = _.assign('foo', (_.get('reverse')), _); return (((function (input) {return _.foo(input)})([1, 2])))})())})})"`;
Expand Down Expand Up @@ -774,6 +776,65 @@ Object {
}
`;

exports[`interpreter correct target tree $[1:3] 1`] = `
Object {
"status": true,
"value": Object {
"name": "projection",
"value": Object {
"left": Object {
"name": "variable",
"value": "$",
},
"optional": false,
"right": Object {
"end": Object {
"column": 7,
"line": 1,
"offset": 6,
},
"name": "list",
"start": Object {
"column": 2,
"line": 1,
"offset": 1,
},
"value": Array [
Object {
"end": Object {
"column": 6,
"line": 1,
"offset": 5,
},
"name": "simpleList",
"start": Object {
"column": 3,
"line": 1,
"offset": 2,
},
"value": Array [
Object {
"name": "tuple",
"value": Array [
Object {
"name": "primitive",
"value": "1",
},
Object {
"name": "primitive",
"value": "3",
},
],
},
],
},
],
},
},
},
}
`;

exports[`interpreter correct target tree ($x + $foobar | [$, $foobar]) where $x = 3 $foobar = ($x + 1) 1`] = `
Object {
"status": true,
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ describe('built ins', () => {
it('handles list of numbers', () => {
expect(projection([1, 2, 3], [-1, 0], false)).toEqual([3, 1])
})
it('handles sliceing of numbers', () => {
expect(projection([1, 2, 3, 4], [[1, 3]], false)).toEqual([2, 3])
})
it('handles empty projection', () => {
expect(projection([1, 2, 3], [], false)).toEqual([])
})
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ const tests = [
sourceCode: `{each $: (length) in $}`,
input: ['bo', 'boo'],
output: { bo: 2, boo: 3 }
},
{
sourceCode: `$[1:3]`,
input: [1, 2, 3, 4],
output: [2, 3]
}
]

Expand Down
4 changes: 3 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const handleProjectionItem = (
): mixed =>
Number.isInteger(projectionRule)
? convertUndefined(projectable.slice(projectionRule)[0])
: convertUndefined(projectable[projectionRule])
: Array.isArray(projectionRule)
? convertUndefined(projectable.slice(...projectionRule))
: convertUndefined(projectable[projectionRule])

const handleProjection = (
projectable: ProjectableType
Expand Down

0 comments on commit e39984f

Please sign in to comment.