Detect and Parse string separated module names (#787)

* detecting and parsing string separated module names

* test fix

* parsing multiple input methods

* travis fix

* test cases

* requested changes made

* test fix

* rebase and fix conflicts
This commit is contained in:
Vibhor Gupta
2019-04-19 01:42:33 +05:30
committed by Jeffrey Warren
parent 84aede7bc3
commit ec40224831
6 changed files with 112 additions and 18 deletions

View File

@@ -59,7 +59,7 @@ A diagram of this running 5 steps on a single sample image may help explain how
This library conveniently works in the browser, in Node, and on the command line (CLI).
### Unix based platforms
You can set up a local environment to test the UI with `npm run setup` followed by `npm start`.
You can set up a local environment to test the UI with `sudo npm run setup` followed by `npm start`.
### Windows
Our npm scripts do not support windows shells, please run the following snippet in PowerShell.
@@ -243,6 +243,38 @@ to be added, in that particular order.
optional_otions is just an optional parameter, in object form, which you might
want to provide to the modules.
A variety of syntaxes are supported by Image Sequencer to add multiple steps and configurations quickly for module chaining. The project supports the string syntax, designed to be compact and URL friendly, and JSON, for handling more complex sequences. This can be achieved by passing strings to `sequencer.addStep()`:
```js
sequencer.addSteps('invert,channel');
sequencer.addSteps(['invert','channel']);
```
For passing default configurations ({} is optional):
```js
sequencer.addSteps('brightness{}');
```
For passing custom configurations:
```js
sequencer.addSteps('brightness{brightness:80}');
```
For passing multiple custom configurations:
```js
sequencer.addSteps('crop{x:120|y:90}')
```
For passing multiple custom configurable modules:
```js
sequencer.addSteps('crop{x:130|y:80},brightness{brightness:80}')
```
return value: **`sequencer`** (To allow method chaining)

View File

@@ -308,6 +308,9 @@ ImageSequencer = function ImageSequencer(options) {
exportBin: exportBin,
modulesInfo: modulesInfo,
toCliString: str.toCliString,
detectStringSyntax: str.detectStringSyntax,
parseStringSyntax: str.parseStringSyntax,
stringToSteps: str.stringToSteps,
toString: str.toString,
stepToString: str.stepToString,
toJSON: str.toJSON,

View File

@@ -6,6 +6,9 @@ function InsertStep(ref, index, name, o) {
return ref.importJSON(ref.sequences[name]);
}
if (ref.detectStringSyntax(name)) {
return ref.stringToSteps(name)
}
function insertStep(index, name, o_) {
if (ref.modules[name]) var moduleInfo = ref.modules[name][1];

View File

@@ -42,7 +42,11 @@ function ReplaceImage(ref,selector,steps,options) {
function make(url) {
tempSequencer.loadImage(url, function(){
this.addSteps(steps).run({stop:function(){}},function(out){
// this.addSteps(steps).run({stop:function(){}},function(out){
var sequence = this.addSteps(steps)
if (ref.detectStringSyntax(steps))
sequence = this.stringToSteps(steps)
sequence.run({stop:function(){}},function(out){
img.src = out;
});
});

View File

@@ -15,6 +15,32 @@ module.exports = function(steps, modulesInfo, addSteps, copy) {
return `sequencer -i [PATH] -s ${cliStringSteps} -d '${JSON.stringify(cliOptions)}'`
}
// Checks if input is a string of comma separated module names
function detectStringSyntax(str) {
let result = (str.includes(',') || str.includes('{')) ? true : false
return result
}
// Parses input string and returns array of module names
function parseStringSyntax(str) {
let stringifiedNames = str.replace(/\s/g, '')
let moduleNames = stringifiedNames.split(',')
return moduleNames
}
// imports string of comma separated module names to sequencer steps
function stringToSteps(str) {
let sequencer = this;
let names = []
if (this.name != "ImageSequencer")
sequencer = this.sequencer;
if (detectStringSyntax(str))
names = stringToJSON(str)
names.forEach(function eachStep(stepObj) {
sequencer.addSteps(stepObj.name, stepObj.options)
})
}
// Strigifies the current sequence
function toString(step) {
if (step) {
@@ -50,8 +76,8 @@ module.exports = function(steps, modulesInfo, addSteps, copy) {
// Coverts stringified sequence into an array of JSON steps
function stringToJSON(str) {
let steps;
if (str.includes(','))
steps = str.split(',');
if (detectStringSyntax(str))
steps = parseStringSyntax(str);
else
steps = [str];
return steps.map(stringToJSONstep);
@@ -119,6 +145,9 @@ module.exports = function(steps, modulesInfo, addSteps, copy) {
return {
toCliString: toCliString,
detectStringSyntax: detectStringSyntax,
parseStringSyntax: parseStringSyntax,
stringToSteps: stringToSteps,
toString: toString,
stepToString: stepToString,
toJSON: toJSON,

View File

@@ -1,3 +1,4 @@
'use strict';
var fs = require('fs');
@@ -103,29 +104,53 @@ test('addSteps("name",o) adds a step', function(t) {
t.end();
});
test('addSteps("name,name") adds two steps', function(t) {
sequencer.addSteps('channel,invert')
t.equal(sequencer.steps.length, 7, 'Length of steps increase')
t.equal(sequencer.steps[5].options.name, "channel", "Correct Step Added");
t.equal(sequencer.steps[6].options.name, "invert", "Correct Step Added");
t.end()
})
test('addSteps("name{parameter:value},name{parameter:value}") adds two steps', function(t) {
sequencer.addSteps('brightness{brightness:80},average{}')
t.equal(sequencer.steps.length, 9, 'Length of steps increase')
t.equal(sequencer.steps[7].options.name, "brightness", "Correct Step Added");
t.equal(sequencer.steps[7].options.brightness, '80', "Correct options loaded")
t.equal(sequencer.steps[8].options.name, "average", "Correct Step Added");
t.end()
})
test('addSteps("name{parameter:value}" adds a step', function(t) {
sequencer.addSteps('brightness{brightness:1}')
t.equal(sequencer.steps.length, 10, 'Length of steps increase')
t.equal(sequencer.steps[9].options.name, "brightness", "Correct Step Added");
t.equal(sequencer.steps[9].options.brightness, '1', "Correct options loaded")
t.end()
})
test('removeSteps(position) removes a step', function(t) {
sequencer.removeSteps( 1);
t.equal(sequencer.steps.length, 4, "Length of steps reduced");
sequencer.removeSteps(1);
t.equal(sequencer.steps.length, 9, "Length of steps reduced");
t.end();
});
test('removeSteps([positions]) removes steps', function(t) {
sequencer.removeSteps([1, 2]);
t.equal(sequencer.steps.length, 2, "Length of steps reduced");
t.equal(sequencer.steps.length, 7, "Length of steps reduced");
t.end();
});
test('insertSteps(position,"module",options) inserts a step', function(t) {
sequencer.insertSteps( 1, 'channel', {});
t.equal(sequencer.steps.length, 3, "Length of Steps increased");
t.equal(sequencer.steps.length, 8, "Length of Steps increased");
t.equal(sequencer.steps[1].options.name, "channel", "Correct Step Inserted");
t.end();
});
test('insertSteps(position,"module") inserts a step', function(t) {
sequencer.insertSteps(1, 'channel');
t.equal(sequencer.steps.length, 4, "Length of Steps increased");
t.equal(sequencer.steps.length, 9, "Length of Steps increased");
t.equal(sequencer.steps[1].options.name, "channel", "Correct Step Inserted");
t.end();
});
@@ -149,16 +174,14 @@ test('getSteps() returns correct array of steps', function(t){
t.end();
})
test('run() runs the sequencer and returns output to callback', function(t) {
sequencer.run(function(out) {
sequencer.run({ mode: 'test' }, function(out) {
t.equal(typeof (sequencer.steps[sequencer.steps.length - 1].output), "object", "Output is Generated");
t.equal(out, sequencer.steps[sequencer.steps.length - 1].output.src, "Output callback works");
t.end();
});
});
test('getStep(offset) returns the step at offset distance relative to current step', function(t) {
sequencer.addSteps('invert', {});
sequencer.addSteps('blend', {});
@@ -169,7 +192,7 @@ test('getStep(offset) returns the step at offset distance relative to current st
});
test('toCliString() returns the CLI command for the sequence', function(t) {
t.deepEqual(sequencer.toCliString(), `sequencer -i [PATH] -s "channel channel channel invert blend" -d '{"channel":"green","offset":-2}'`, "works correctly");
t.deepEqual(sequencer.toCliString(), `sequencer -i [PATH] -s "channel channel channel channel invert brightness average brightness invert blend" -d \'{"channel":"green","brightness":"1","offset":-2}'`, "works correctly");
t.end();
});