Skip to content

Commit

Permalink
changed the way formating of args work (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapasquale committed Nov 7, 2019
1 parent d37f83d commit 4e90d44
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ Generates a random value from on [Chance.JS](http://chancejs.com/)
### Args:
- Type: Select a function from Chance to be executed
- Custom Function: If selected **custom** type, it will execute the function named here
- Options: Stringified arguments to be used with chosen function
- Options: Arguments you want to pass to your `chance` function. Because of the way Insomnia handles plugins, you need to pass it as:

### Examples:
`argumentName: argumentValue, argumentName2: argumentValue2`

![Screenshot](/readme-preview-1.png?raw=true)
![Screenshot](/readme-preview-2.png?raw=true)

### Warning:

If you pass some value on *Options*, Insomnia will say that there is an invalid token on the resulting JSON. That happens because the options has invalid characters (`"`) and fails to stringify. But this is only visual, you can still use it!
For example, with [Natural](https://chancejs.com/basics/natural.html), you can pass it as: `min: 1, max: 20`
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ module.exports.templateTags = [
placeholder: 'integer',
},
{
displayName: 'Options - Stringified arguments for chosen function',
displayName: 'Options - Arguments for selected function, separated by a comma',
type: 'string',
placeholder: '"min": 10, "max": 25',
placeholder: 'min: 10, max: 25',
},
],
run(_, type, func, opt = '') {
Expand All @@ -50,7 +50,7 @@ module.exports.templateTags = [
];

function parseOptions(options) {
if (!options || options === '') {
if (!options) {
return undefined;
}

Expand All @@ -59,6 +59,21 @@ function parseOptions(options) {
return JSON.parse(options);
}

// if not, return as an object
return JSON.parse(`{${options}}`);
// if not, parse to be JSON fields
const stringifiedOptions = options
.split(/,\s?/gi)
.map(t => {
const regex = /(.*):\s?(.*)/gi
const matches = regex.exec(t)

if (!matches) {
return null
}

return `"${matches[1]}": ${matches[2]}`
})
.filter(Boolean)
.join(',')

return JSON.parse(`{${stringifiedOptions}}`);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "insomnia-plugin-chance",
"version": "1.2.0",
"version": "1.3.0",
"author": "Luca Pasquale (https://github.com/lucapasquale)",
"homepage": "https://github.com/lucapasquale/insomnia-plugin-chance",
"main": "index.js",
Expand Down
Binary file removed readme-preview-1.png
Binary file not shown.
Binary file removed readme-preview-2.png
Binary file not shown.

0 comments on commit 4e90d44

Please sign in to comment.