Skip to content

Commit

Permalink
feat(helpers): new object get attribute sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-cristino committed Mar 5, 2024
1 parent 8e48806 commit bd86e90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/helpers/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class HelperError extends Error {
}
}

export const get = p.new(['object', 'path'], 'manipulate and get', async (ctx) => {
const object = ctx.fetch('object');
const path = ctx.fetch('path');
try {
return ctx.pass(_.get(object as any, path as string));
} catch (e) {
throw new HelperError(e);
}
});

export const set = p.new(['object', 'path', 'value'], 'manipulate and set', async (ctx) => {
const object = ctx.fetch('object');
const path = ctx.fetch('path');
Expand Down
28 changes: 28 additions & 0 deletions pkg/helpers/test/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Slangroom } from '@slangroom/core';
import { helpers } from '@slangroom/helpers';
import test from 'ava';

test('@slangroom/helpers 🎣 get path object', async (t) => {
const setter = `Rule unknown ignore
Given I send object 'the_object' and send path 'the_path' and manipulate and get and output into 'mimmo'
Given I have a 'string' named 'mimmo'
Then print 'mimmo'
`;

const slangroom = new Slangroom(helpers);
const res = await slangroom.execute(setter, {
data: {
"the_object": {
"a": "b",
"c": {
"d": "e"
}
},
"the_path": "c.d"
},
});
t.deepEqual(res.result, {
mimmo: 'e'
}, res.logs)
});

0 comments on commit bd86e90

Please sign in to comment.