From bd86e90c63e1e6ed281ead40557484e03f09a2cf Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Tue, 5 Mar 2024 18:23:12 +0100 Subject: [PATCH] feat(helpers): new object get attribute sentence --- pkg/helpers/src/plugin.ts | 10 ++++++++++ pkg/helpers/test/get.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 pkg/helpers/test/get.ts diff --git a/pkg/helpers/src/plugin.ts b/pkg/helpers/src/plugin.ts index 9ea5229e..854ff2cc 100644 --- a/pkg/helpers/src/plugin.ts +++ b/pkg/helpers/src/plugin.ts @@ -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'); diff --git a/pkg/helpers/test/get.ts b/pkg/helpers/test/get.ts new file mode 100644 index 00000000..b747c282 --- /dev/null +++ b/pkg/helpers/test/get.ts @@ -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) +});