mirror of
https://github.com/publiclab/image-sequencer.git
synced 2025-12-16 13:20:01 +01:00
Refactor: String parsing methods (#667)
* WIP * string methods refactor * fixed refactor
This commit is contained in:
committed by
Jeffrey Warren
parent
2be7a3dca6
commit
6339229836
@@ -24,7 +24,7 @@ ImageSequencer = function ImageSequencer(options) {
|
|||||||
if (!typeof (a) == "object") return a;
|
if (!typeof (a) == "object") return a;
|
||||||
if (objTypeOf(a) == "Array") return a.slice();
|
if (objTypeOf(a) == "Array") return a.slice();
|
||||||
if (objTypeOf(a) == "Object") {
|
if (objTypeOf(a) == "Object") {
|
||||||
var b = {};
|
var b = {};
|
||||||
for (var v in a) {
|
for (var v in a) {
|
||||||
b[v] = copy(a[v]);
|
b[v] = copy(a[v]);
|
||||||
}
|
}
|
||||||
@@ -178,7 +178,7 @@ ImageSequencer = function ImageSequencer(options) {
|
|||||||
loadPrevSteps(sequencer);
|
loadPrevSteps(sequencer);
|
||||||
json_q.callback.call(ret);
|
json_q.callback.call(ret);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceImage(selector, steps, options) {
|
function replaceImage(selector, steps, options) {
|
||||||
@@ -212,7 +212,7 @@ ImageSequencer = function ImageSequencer(options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (modules[name]){
|
if (modules[name]){
|
||||||
modulesdata = modules[name][1];
|
modulesdata = modules[name][1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -221,124 +221,6 @@ ImageSequencer = function ImageSequencer(options) {
|
|||||||
return modulesdata;
|
return modulesdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Genates a CLI string for the current sequence
|
|
||||||
function toCliString() {
|
|
||||||
var cliStringSteps = `"`, cliOptions = {};
|
|
||||||
for (var step in this.steps) {
|
|
||||||
var name = (typeof this.steps[step].options !== "undefined")? this.steps[step].options.name : this.steps[step].name
|
|
||||||
if (name !== "load-image"){
|
|
||||||
cliStringSteps += `${name} `;
|
|
||||||
}
|
|
||||||
for (var inp in modulesInfo(name).inputs) {
|
|
||||||
cliOptions[inp] = this.steps[step].options[inp];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cliStringSteps = cliStringSteps.substr(0, cliStringSteps.length - 1) + `"`;
|
|
||||||
return `sequencer -i [PATH] -s ${cliStringSteps} -d '${JSON.stringify(cliOptions)}'`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strigifies the current sequence
|
|
||||||
function toString(step) {
|
|
||||||
if (step) {
|
|
||||||
return stepToString(step);
|
|
||||||
} else {
|
|
||||||
return copy(this.steps.map(stepToString).slice(1).join(','));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stringifies one step of the sequence
|
|
||||||
function stepToString(step) {
|
|
||||||
var arg = (step.name)?step.name:step.options.name;
|
|
||||||
let inputs = modulesInfo(arg).inputs || {}, op = {};
|
|
||||||
|
|
||||||
for (let input in inputs) {
|
|
||||||
|
|
||||||
if (!!step.options[input] && step.options[input] != inputs[input].default) {
|
|
||||||
op[input] = step.options[input];
|
|
||||||
op[input] = encodeURIComponent(op[input]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
var configurations = Object.keys(op).map(key => key + ':' + op[key]).join('|');
|
|
||||||
return `${arg}{${configurations}}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// exports the current sequence as an array of JSON steps
|
|
||||||
function toJSON() {
|
|
||||||
return this.stringToJSON(this.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Coverts stringified sequence into an array of JSON steps
|
|
||||||
function stringToJSON(str) {
|
|
||||||
let steps;
|
|
||||||
if (str.includes(','))
|
|
||||||
steps = str.split(',');
|
|
||||||
else
|
|
||||||
steps = [str];
|
|
||||||
return steps.map(stringToJSONstep);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converts one stringified step into JSON
|
|
||||||
function stringToJSONstep(str) {
|
|
||||||
var bracesStrings;
|
|
||||||
if (str.includes('{'))
|
|
||||||
if (str.includes('(') && str.indexOf('(') < str.indexOf('{'))
|
|
||||||
bracesStrings = ['(', ')'];
|
|
||||||
else
|
|
||||||
bracesStrings = ['{', '}'];
|
|
||||||
else
|
|
||||||
bracesStrings = ['(', ')'];
|
|
||||||
|
|
||||||
if (str.indexOf(bracesStrings[0]) === -1) { // if there are no settings specified
|
|
||||||
var moduleName = str.substr(0);
|
|
||||||
stepSettings = "";
|
|
||||||
} else {
|
|
||||||
var moduleName = str.substr(0, str.indexOf(bracesStrings[0]));
|
|
||||||
stepSettings = str.slice(str.indexOf(bracesStrings[0]) + 1, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
stepSettings = stepSettings.split('|').reduce(function formatSettings(accumulator, current, i) {
|
|
||||||
var settingName = current.substr(0, current.indexOf(':')),
|
|
||||||
settingValue = current.substr(current.indexOf(':') + 1);
|
|
||||||
settingValue = settingValue.replace(/^\(/, '').replace(/\)$/, ''); // strip () at start/end
|
|
||||||
settingValue = settingValue.replace(/^\{/, '').replace(/\}$/, ''); // strip {} at start/end
|
|
||||||
settingValue = decodeURIComponent(settingValue);
|
|
||||||
current = [
|
|
||||||
settingName,
|
|
||||||
settingValue
|
|
||||||
];
|
|
||||||
if (!!settingName) accumulator[settingName] = settingValue;
|
|
||||||
return accumulator;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: moduleName,
|
|
||||||
options: stepSettings
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// imports a string into the sequencer steps
|
|
||||||
function importString(str) {
|
|
||||||
let sequencer = this;
|
|
||||||
if (this.name != "ImageSequencer")
|
|
||||||
sequencer = this.sequencer;
|
|
||||||
var stepsFromString = stringToJSON(str);
|
|
||||||
stepsFromString.forEach(function eachStep(stepObj) {
|
|
||||||
sequencer.addSteps(stepObj.name, stepObj.options);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// imports a array of JSON steps into the sequencer steps
|
|
||||||
function importJSON(obj) {
|
|
||||||
let sequencer = this;
|
|
||||||
if (this.name != "ImageSequencer")
|
|
||||||
sequencer = this.sequencer;
|
|
||||||
obj.forEach(function eachStep(stepObj) {
|
|
||||||
sequencer.addSteps(stepObj.name, stepObj.options);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadNewModule(name, options) {
|
function loadNewModule(name, options) {
|
||||||
|
|
||||||
if (!options) {
|
if (!options) {
|
||||||
@@ -401,6 +283,8 @@ ImageSequencer = function ImageSequencer(options) {
|
|||||||
this.sequences = require('./SavedSequences.json');
|
this.sequences = require('./SavedSequences.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var str = require('./Strings.js')(this.steps, modulesInfo, addSteps, copy)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
//literals and objects
|
//literals and objects
|
||||||
name: "ImageSequencer",
|
name: "ImageSequencer",
|
||||||
@@ -423,14 +307,14 @@ ImageSequencer = function ImageSequencer(options) {
|
|||||||
setUI: setUI,
|
setUI: setUI,
|
||||||
exportBin: exportBin,
|
exportBin: exportBin,
|
||||||
modulesInfo: modulesInfo,
|
modulesInfo: modulesInfo,
|
||||||
toCliString: toCliString,
|
toCliString: str.toCliString,
|
||||||
toString: toString,
|
toString: str.toString,
|
||||||
stepToString: stepToString,
|
stepToString: str.stepToString,
|
||||||
toJSON: toJSON,
|
toJSON: str.toJSON,
|
||||||
stringToJSON: stringToJSON,
|
stringToJSON: str.stringToJSON,
|
||||||
stringToJSONstep: stringToJSONstep,
|
stringToJSONstep: str.stringToJSONstep,
|
||||||
importString: importString,
|
importString: str.importString,
|
||||||
importJSON: importJSON,
|
importJSON: str.importJSON,
|
||||||
loadNewModule: loadNewModule,
|
loadNewModule: loadNewModule,
|
||||||
saveNewModule: saveNewModule,
|
saveNewModule: saveNewModule,
|
||||||
createMetaModule: require('./util/createMetaModule'),
|
createMetaModule: require('./util/createMetaModule'),
|
||||||
|
|||||||
130
src/Strings.js
Normal file
130
src/Strings.js
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
module.exports = function(steps, modulesInfo, addSteps, copy) {
|
||||||
|
// Genates a CLI string for the current sequence
|
||||||
|
function toCliString() {
|
||||||
|
var cliStringSteps = `"`, cliOptions = {};
|
||||||
|
for (var step in this.steps) {
|
||||||
|
var name = (typeof this.steps[step].options !== "undefined")? this.steps[step].options.name : this.steps[step].name
|
||||||
|
if (name !== "load-image"){
|
||||||
|
cliStringSteps += `${name} `;
|
||||||
|
}
|
||||||
|
for (var inp in modulesInfo(name).inputs) {
|
||||||
|
cliOptions[inp] = this.steps[step].options[inp];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cliStringSteps = cliStringSteps.substr(0, cliStringSteps.length - 1) + `"`;
|
||||||
|
return `sequencer -i [PATH] -s ${cliStringSteps} -d '${JSON.stringify(cliOptions)}'`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strigifies the current sequence
|
||||||
|
function toString(step) {
|
||||||
|
if (step) {
|
||||||
|
return stepToString(step);
|
||||||
|
} else {
|
||||||
|
return copy(this.steps.map(stepToString).slice(1).join(','));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stringifies one step of the sequence
|
||||||
|
function stepToString(step) {
|
||||||
|
var arg = (step.name)?step.name:step.options.name;
|
||||||
|
let inputs = modulesInfo(arg).inputs || {}, op = {};
|
||||||
|
|
||||||
|
for (let input in inputs) {
|
||||||
|
|
||||||
|
if (!!step.options[input] && step.options[input] != inputs[input].default) {
|
||||||
|
op[input] = step.options[input];
|
||||||
|
op[input] = encodeURIComponent(op[input]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var configurations = Object.keys(op).map(key => key + ':' + op[key]).join('|');
|
||||||
|
return `${arg}{${configurations}}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// exports the current sequence as an array of JSON steps
|
||||||
|
function toJSON() {
|
||||||
|
return this.stringToJSON(this.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Coverts stringified sequence into an array of JSON steps
|
||||||
|
function stringToJSON(str) {
|
||||||
|
let steps;
|
||||||
|
if (str.includes(','))
|
||||||
|
steps = str.split(',');
|
||||||
|
else
|
||||||
|
steps = [str];
|
||||||
|
return steps.map(stringToJSONstep);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts one stringified step into JSON
|
||||||
|
function stringToJSONstep(str) {
|
||||||
|
var bracesStrings;
|
||||||
|
if (str.includes('{'))
|
||||||
|
if (str.includes('(') && str.indexOf('(') < str.indexOf('{'))
|
||||||
|
bracesStrings = ['(', ')'];
|
||||||
|
else
|
||||||
|
bracesStrings = ['{', '}'];
|
||||||
|
else
|
||||||
|
bracesStrings = ['(', ')'];
|
||||||
|
|
||||||
|
if (str.indexOf(bracesStrings[0]) === -1) { // if there are no settings specified
|
||||||
|
var moduleName = str.substr(0);
|
||||||
|
stepSettings = "";
|
||||||
|
} else {
|
||||||
|
var moduleName = str.substr(0, str.indexOf(bracesStrings[0]));
|
||||||
|
stepSettings = str.slice(str.indexOf(bracesStrings[0]) + 1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
stepSettings = stepSettings.split('|').reduce(function formatSettings(accumulator, current, i) {
|
||||||
|
var settingName = current.substr(0, current.indexOf(':')),
|
||||||
|
settingValue = current.substr(current.indexOf(':') + 1);
|
||||||
|
settingValue = settingValue.replace(/^\(/, '').replace(/\)$/, ''); // strip () at start/end
|
||||||
|
settingValue = settingValue.replace(/^\{/, '').replace(/\}$/, ''); // strip {} at start/end
|
||||||
|
settingValue = decodeURIComponent(settingValue);
|
||||||
|
current = [
|
||||||
|
settingName,
|
||||||
|
settingValue
|
||||||
|
];
|
||||||
|
if (!!settingName) accumulator[settingName] = settingValue;
|
||||||
|
return accumulator;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: moduleName,
|
||||||
|
options: stepSettings
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// imports a string into the sequencer steps
|
||||||
|
function importString(str) {
|
||||||
|
let sequencer = this;
|
||||||
|
if (this.name != "ImageSequencer")
|
||||||
|
sequencer = this.sequencer;
|
||||||
|
var stepsFromString = stringToJSON(str);
|
||||||
|
stepsFromString.forEach(function eachStep(stepObj) {
|
||||||
|
sequencer.addSteps(stepObj.name, stepObj.options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// imports a array of JSON steps into the sequencer steps
|
||||||
|
function importJSON(obj) {
|
||||||
|
let sequencer = this;
|
||||||
|
if (this.name != "ImageSequencer")
|
||||||
|
sequencer = this.sequencer;
|
||||||
|
obj.forEach(function eachStep(stepObj) {
|
||||||
|
sequencer.addSteps(stepObj.name, stepObj.options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
toCliString: toCliString,
|
||||||
|
toString: toString,
|
||||||
|
stepToString: stepToString,
|
||||||
|
toJSON: toJSON,
|
||||||
|
stringToJSON: stringToJSON,
|
||||||
|
stringToJSONstep: stringToJSONstep,
|
||||||
|
importString: importString,
|
||||||
|
importJSON: importJSON
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user