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

feat(helpers): new object get attribute sentence #94

Merged
merged 1 commit into from
Mar 5, 2024
Merged
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
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)
});
Loading