Skip to content

Commit

Permalink
feat: implement merge/omit/concat/compact (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
puria authored Mar 1, 2024
1 parent f2da0ee commit 877fb8a
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 19 deletions.
57 changes: 38 additions & 19 deletions pkg/helpers/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,44 @@ class HelperError extends Error {
}
}

// export const assign = p.new(['target', 'source'], 'manipulate and assign', async (ctx) => {
// const target = ctx.fetch('target');
// const source = ctx.fetch('source');
// try {
// return ctx.pass(_.assign(target as {}, source as {}));
// } catch (e) {
// throw new HelperError(e);
// }
// });

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

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

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

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

export const pick = p.new(['object', 'properties'], 'manipulate and pick', async (ctx) => {
const properties = ctx.fetch('properties') as string[] | string;
Expand Down
28 changes: 28 additions & 0 deletions pkg/helpers/test/compact.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 πŸ”‡ compact arrays ', async (t) => {
const picked = `Rule unknown ignore
Given I send array 'the_array' and manipulate and compact and output into 'mimmo'
Given I have a 'string array' named 'the_array'
Given I have a 'string dictionary' named 'mimmo'
Then print 'mimmo'
`;

const slangroom = new Slangroom(helpers);
const res = await slangroom.execute(picked, {
data: {
"the_array": [0, "c", false, "d", '', "πŸ˜†"]
},
});
t.deepEqual(res.result, {
mimmo: [
"c",
"d",
"πŸ˜†",
]
}, res.logs)
});
113 changes: 113 additions & 0 deletions pkg/helpers/test/concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Slangroom } from '@slangroom/core';
import { helpers } from '@slangroom/helpers';
import test from 'ava';

test('@slangroom/helpers 🧲 concat arrays ', async (t) => {
const picked = `Rule unknown ignore
Given I send array 'the_array' and send values 'v' and manipulate and concat and output into 'r'
Given I have a 'string array' named 'the_array'
Given I have a 'string dictionary' named 'r'
Then print 'r'
`;

const slangroom = new Slangroom(helpers);
const res = await slangroom.execute(picked, {
data: {
"v": ["a", "b", "πŸ˜€"],
"the_array": ["c", "d", "πŸ˜†"]
},
});
t.deepEqual(res.result, {
r: [
"c",
"d",
"πŸ˜†",
"a",
"b",
"πŸ˜€"
]
}, res.logs)
});

test('@slangroom/helpers 🧲 concat arrays with missing values', async (t) => {
const picked = `Rule unknown ignore
Given I send array 'the_array' and manipulate and concat and output into 'r'
Given I have a 'string array' named 'the_array'
Given I have a 'string dictionary' named 'r'
Then print 'r'
`;

const slangroom = new Slangroom(helpers);
const fn = slangroom.execute(picked, {
data: {
"the_array": ["c", "d", "πŸ˜†"]
},
});
const error = await t.throwsAsync(fn);
t.is((error as Error).message, 'ParseError: "concat" between (50, 55) must be one of: compact');
});

test('@slangroom/helpers 🧲 concat empty array', async (t) => {
const picked = `Rule unknown ignore
Given I send array 'the_array' and send values 'v' and manipulate and concat and output into 'r'
Given I have a 'string array' named 'the_array'
Given I have a 'string dictionary' named 'r'
Then print 'r'
`;

const slangroom = new Slangroom(helpers);
const res = await slangroom.execute(picked, {
data: {
"v": ["a", "b", "πŸ˜€"],
"the_array": []
},
});
t.deepEqual(res.result, {
r: ["a", "b", "πŸ˜€"]
}, res.logs)
});

test('@slangroom/helpers 🧲 concat empty values', async (t) => {
const picked = `Rule unknown ignore
Given I send array 'the_array' and send values 'v' and manipulate and concat and output into 'r'
Given I have a 'string array' named 'the_array'
Given I have a 'string dictionary' named 'r'
Then print 'r'
`;

const slangroom = new Slangroom(helpers);
const res = await slangroom.execute(picked, {
data: {
"v": [],
"the_array": ["c", "d", "πŸ˜†"]
},
});
t.deepEqual(res.result, {
r: ["c", "d", "πŸ˜†"]
}, res.logs)
});

test('@slangroom/helpers 🧲 concat empty values and empty array', async (t) => {
const picked = `Rule unknown ignore
Given I send array 'the_array' and send values 'v' and manipulate and concat and output into 'r'
Given I have a 'string array' named 'the_array'
Given I have a 'string dictionary' named 'r'
Then print 'r'
`;

const slangroom = new Slangroom(helpers);
const res = await slangroom.execute(picked, {
data: {
"v": [],
"the_array": []
},
});
t.deepEqual(res.result, {
r: []
}, res.logs)
});

0 comments on commit 877fb8a

Please sign in to comment.