Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break out string parsing functions into a separate file called Strings.js #458

Closed
jywarren opened this issue Nov 8, 2018 · 13 comments · Fixed by #667
Closed

Break out string parsing functions into a separate file called Strings.js #458

jywarren opened this issue Nov 8, 2018 · 13 comments · Fixed by #667
Labels

Comments

@jywarren
Copy link
Member

jywarren commented Nov 8, 2018

This is some great code! Let's break it out so it's more readable. We might even make a folder called strings and break it out more thoroughly.

// Genates a CLI string for the current sequence
function toCliString() {
var cliStringSteps = `"`, cliOptions = {};
for (var step in this.steps) {
if (this.steps[step].options.name !== "load-image")
cliStringSteps += `${this.steps[step].options.name} `;
for (var inp in modulesInfo(this.steps[step].options.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.images.image1.steps).map(stepToString).slice(1).join(',');
}
}
// Stringifies one step of the sequence
function stepToString(step) {
let inputs = copy(modulesInfo(step.options.name).inputs);
inputs = inputs || {};
for (let input in inputs) {
inputs[input] = step.options[input] || inputs[input].default;
inputs[input] = encodeURIComponent(inputs[input]);
}
var configurations = Object.keys(inputs).map(key => key + ':' + inputs[key]).join('|');
return `${step.options.name}{${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);
});
}

Keep an eye on the tests here, which will tell us if we've properly moved the code:

https://github.com/publiclab/image-sequencer/blob/main/test/modules/import-export.js

@gitmate gitmate bot added the module New Module idea label Nov 8, 2018
@jywarren
Copy link
Member Author

jywarren commented Nov 8, 2018

@Mridul97 would you be at all interested in this one? Just curious!

@gitmate
Copy link

gitmate bot commented Nov 8, 2018

GitMate.io thinks possibly related issues are #26 (Break out longer functions into sub source files and require in), #118 (Assorted improvements to be broken into separate issues), #165 (Various module ideas to be broken out into separate issues), and #141 (move module tests into separate test files).

1 similar comment
@gitmate
Copy link

gitmate bot commented Nov 8, 2018

GitMate.io thinks possibly related issues are #26 (Break out longer functions into sub source files and require in), #118 (Assorted improvements to be broken into separate issues), #165 (Various module ideas to be broken out into separate issues), and #141 (move module tests into separate test files).

@ho-dor
Copy link

ho-dor commented Nov 10, 2018

Can I claim this @jywarren, please?

@jywarren
Copy link
Member Author

jywarren commented Nov 10, 2018 via email

@vibhorgupta-gh
Copy link

@jywarren this has been inactive for a while, shall I take this up?

@jywarren
Copy link
Member Author

jywarren commented Dec 5, 2018 via email

@vibhorgupta-gh
Copy link

Doesn't look like it

@jywarren
Copy link
Member Author

jywarren commented Dec 5, 2018 via email

@Mridul97
Copy link

Mridul97 commented Dec 5, 2018

@VibhorCodecianGupta I am working on this one. Could you please take up any other issue. Thanks!

@jywarren
Copy link
Member Author

jywarren commented Dec 5, 2018 via email

@Mridul97
Copy link

Mridul97 commented Dec 5, 2018

No problem at all! @jywarren

@vibhorgupta-gh
Copy link

@Mridul97 yup, no issues!

@harshkhandeparkar harshkhandeparkar added has-pull-request Issues which have a PR open and removed module New Module idea labels Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants