Skip to content

Commit

Permalink
add support for inline lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
f5io committed Oct 11, 2016
1 parent 6c2bb76 commit 358fb2f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@f5io/jsont",
"version": "1.0.1",
"version": "1.0.2",
"description": "Transform JSON Objects with ES6/2015 template strings",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -49,6 +49,8 @@ function transform(path) {
result = Array.isArray(ctx) ?
`[${ctx.map(x => k(x, root)).join(',')}]` :
k(ctx, root);
} else if (typeof k === 'function') {
result = JSON.stringify(k(context, root)) || null;
} else {
result = JSON.stringify(query(k, getContext(k))) || null;
}
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Expand Up @@ -34,6 +34,21 @@ test('simple transform', t => {
t.deepEquals(transform(data), expected, 'should be equal');
});

test('simple transform with lambda', t => {
const transform = $('store')`{
"cheapBooks": ${ (ctx, root) => {
return ctx.book.filter(x => x.price < 9)
} },
"testNull": ${ () => {} }
}`;
const expected = {
cheapBooks: data.store.book.filter(x => x.price < 9),
testNull: null
};
t.plan(1);
t.deepEquals(transform(data), expected, 'should be equal');
});

test('simple filter transform', t => {
const transform = $`{
"cheapBooks": ${ $('store.book[?(@.price < 9)]')`` }
Expand Down

0 comments on commit 358fb2f

Please sign in to comment.