-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
318 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@slangroom/helpers", | ||
"version": "1.20.0", | ||
"dependencies": { | ||
"@slangroom/core": "workspace:*", | ||
"@slangroom/shared": "workspace:*", | ||
"@types/lodash": "^4.14.202", | ||
"lodash": "^4.17.21" | ||
}, | ||
"repository": "https://github.com/dyne/slangroom", | ||
"license": "AGPL-3.0-only", | ||
"type": "module", | ||
"main": "./build/esm/src/index.js", | ||
"types": "./build/esm/src/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./build/esm/src/index.d.ts", | ||
"default": "./build/esm/src/index.js" | ||
} | ||
}, | ||
"./*": { | ||
"import": { | ||
"types": "./build/esm/src/*.d.ts", | ||
"default": "./build/esm/src/*.js" | ||
} | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from '@slangroom/helpers/plugin'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Plugin } from '@slangroom/core'; | ||
import _ from 'lodash'; | ||
|
||
const p = new Plugin() | ||
|
||
class HelperError extends Error { | ||
constructor(e: Error) { | ||
super(e.message) | ||
this.name = 'Slangroom @slangroom/helper 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 pick = p.new(['object', 'properties'], 'manipulate and pick', async (ctx) => { | ||
const properties = ctx.fetch('properties') as string[] | string; | ||
const obj = ctx.fetch('object') as {}; | ||
try { | ||
const manipulated = _.pick(obj, properties); | ||
if (Object.keys(manipulated).length === 0) { | ||
throw new Error(`MANIPULATION ERRROR: \nNone of the properties \n\n ${JSON.stringify(properties)} \n\n exist in the object: \n\n ${JSON.stringify(obj, null, 3)}`); | ||
} | ||
return ctx.pass(manipulated); | ||
} catch (e) { | ||
throw new HelperError(e); | ||
} | ||
}); | ||
|
||
export const helpers = p | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { Slangroom } from '@slangroom/core'; | ||
import { helpers } from '@slangroom/helpers'; | ||
import test from 'ava'; | ||
|
||
const expected = { | ||
r: { | ||
a: 'b', | ||
c: { | ||
d: { | ||
e: '42', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
test('@slangroom/helpers 😆 pick object', async (t) => { | ||
const picked = `Rule unknown ignore | ||
Given I send object 'complex_object' and send properties 'props' and manipulate and pick and output into 'r' | ||
Given I have a 'string dictionary' named 'complex_object' | ||
Given I have a 'string dictionary' named 'r' | ||
Then print 'r' | ||
`; | ||
|
||
const slangroom = new Slangroom(helpers); | ||
const res = await slangroom.execute(picked, { | ||
data: { | ||
props: ["a", "c.d"], | ||
complex_object: { | ||
"a": "b", | ||
"c": { | ||
"d": { | ||
"e": 42 | ||
}, | ||
"f": "g" | ||
} | ||
} | ||
}, | ||
}); | ||
t.deepEqual(res.result, expected, res.logs) | ||
}); | ||
|
||
test('@slangroom/helpers 😆 pick object with params not in order', async (t) => { | ||
const picked = `Rule unknown ignore | ||
Given I send properties 'props' and send object 'complex_object' and manipulate and pick and output into 'r' | ||
Given I have a 'string dictionary' named 'complex_object' | ||
Given I have a 'string dictionary' named 'r' | ||
Then print 'r' | ||
`; | ||
|
||
const slangroom = new Slangroom(helpers); | ||
const res = await slangroom.execute(picked, { | ||
data: { | ||
props: ["a", "c.d"], | ||
complex_object: { | ||
"a": "b", | ||
"c": { | ||
"d": { | ||
"e": 42 | ||
}, | ||
"f": "g" | ||
} | ||
} | ||
}, | ||
}); | ||
t.deepEqual(res.result, expected, res.logs) | ||
}); | ||
|
||
|
||
test('@slangroom/helpers 😆 pick object with one string params ', async (t) => { | ||
const picked = `Rule unknown ignore | ||
Given I send properties 'props' and send object 'complex_object' and manipulate and pick and output into 'r' | ||
Given I have a 'string dictionary' named 'complex_object' | ||
Given I have a 'string dictionary' named 'r' | ||
Then print 'r' | ||
`; | ||
|
||
const slangroom = new Slangroom(helpers); | ||
const res = await slangroom.execute(picked, { | ||
data: { | ||
props: "a", | ||
complex_object: { | ||
"a": "b", | ||
"c": { | ||
"d": { | ||
"e": 42 | ||
}, | ||
"f": "g" | ||
} | ||
} | ||
}, | ||
}); | ||
t.deepEqual(res.result, { r: { a: 'b' } }, res.logs) | ||
}); | ||
|
||
test('@slangroom/helpers 😆 pick object with one string as an array of one element', async (t) => { | ||
const picked = `Rule unknown ignore | ||
Given I send properties 'props' and send object 'complex_object' and manipulate and pick and output into 'r' | ||
Given I have a 'string dictionary' named 'complex_object' | ||
Given I have a 'string dictionary' named 'r' | ||
Then print 'r' | ||
`; | ||
|
||
const slangroom = new Slangroom(helpers); | ||
const res = await slangroom.execute(picked, { | ||
data: { | ||
props: ["a"], | ||
complex_object: { | ||
"a": "b", | ||
"c": { | ||
"d": { | ||
"e": 42 | ||
}, | ||
"f": "g" | ||
} | ||
} | ||
}, | ||
}); | ||
t.deepEqual(res.result, { r: { a: 'b' } }, res.logs) | ||
}); | ||
test('@slangroom/helpers 😆 pick handle fuzzy input', async (t) => { | ||
const picked = `Rule unknown ignore | ||
Given I send properties 'props' and send object 'complex_object' and manipulate and pick and output into 'r' | ||
Given I have a 'string dictionary' named 'complex_object' | ||
Given I have a 'string dictionary' named 'r' | ||
Then print 'r' | ||
`; | ||
|
||
const slangroom = new Slangroom(helpers); | ||
const res = await slangroom.execute(picked, { | ||
data: { | ||
props: ["a", "c.d", "random", 123, null, {}], | ||
complex_object: { | ||
"a": "b", | ||
"c": { | ||
"d": { | ||
"e": 42 | ||
}, | ||
"f": "g" | ||
}, | ||
"random": "value", | ||
"123": 123, | ||
"null": null, | ||
"object": {} | ||
} | ||
}, | ||
}); | ||
const reuslt = { ...expected.r, random: "value", 123: "123" } | ||
t.deepEqual(res.result, { r: reuslt }, res.logs); | ||
}); | ||
|
||
test('@slangroom/helpers 😆 pick breaks when not found', async (t) => { | ||
const picked = `Rule unknown ignore | ||
Given I send properties 'props' and send object 'complex_object' and manipulate and pick and output into 'r' | ||
Given I have a 'string dictionary' named 'complex_object' | ||
Given I have a 'string dictionary' named 'r' | ||
Then print 'r' | ||
`; | ||
|
||
const slangroom = new Slangroom(helpers); | ||
const fn = slangroom.execute(picked, { | ||
data: { | ||
props: "a", // wrong type | ||
complex_object: expected | ||
}, | ||
}); | ||
const error = await t.throwsAsync(fn); | ||
t.is((error as Error).message, `MANIPULATION ERRROR: | ||
None of the properties | ||
"a" | ||
exist in the object: | ||
{ | ||
"r": { | ||
"a": "b", | ||
"c": { | ||
"d": { | ||
"e": "42" | ||
} | ||
} | ||
} | ||
}`); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.