mirror of
https://github.com/publiclab/image-sequencer.git
synced 2025-12-14 04:10:04 +01:00
Unified Input Parser
This commit is contained in:
126
src/FormatInput.js
Normal file
126
src/FormatInput.js
Normal file
@@ -0,0 +1,126 @@
|
||||
function objTypeOf(object){
|
||||
return Object.prototype.toString.call(object).split(" ")[1].slice(0,-1)
|
||||
}
|
||||
|
||||
function getPrimitive(object){
|
||||
return (objTypeOf(object)=='Array')?object[0]:object;
|
||||
}
|
||||
|
||||
function makeArray(input) {
|
||||
return (objTypeOf(input)=="Array")?input:[input];
|
||||
}
|
||||
|
||||
function copy(a) {
|
||||
if (!typeof(a) == "object") return a;
|
||||
if (objTypeOf(a) == "Array") return a.slice();
|
||||
if (objTypeOf(a) == "Object") return JSON.parse(JSON.stringify(a));
|
||||
return a;
|
||||
}
|
||||
|
||||
function formatInput(args,format,images) {
|
||||
images = images || [];
|
||||
for (image in this.images) {
|
||||
images.push(image);
|
||||
}
|
||||
json_q = {};
|
||||
format_i = format;
|
||||
if (format == "+")
|
||||
format = ['o_string_a', 'string_a', 'o_object'];
|
||||
else if (format == "-")
|
||||
format = ['o_string_a', 'number_a'];
|
||||
else if (format == "^")
|
||||
format = ['o_string_a', 'number', 'string', 'o_object'];
|
||||
else if (format == "r")
|
||||
format = ['o_string_a', 'o_number'];
|
||||
/*
|
||||
formats:
|
||||
addSteps :: o_image_a, name_a, o_o
|
||||
o_string_a, string_a, o_object => { image: [{name,o}] }
|
||||
removeSteps :: o_image_a, index_a
|
||||
o_string_a, number_a => { image: [index] }
|
||||
insertSteps :: o_image_a, index, name, o_o
|
||||
o_string_a, number, string, o_object => { image: [{index,name,o}] }
|
||||
run :: o_image_a, o_from
|
||||
o_string_a, o_number => { image: index }
|
||||
|
||||
optionals:
|
||||
image: o_string_a
|
||||
options: o_object
|
||||
from: o_number
|
||||
*/
|
||||
|
||||
if(format[format.length-1] == "o_object") {
|
||||
if(objTypeOf(args[args.length-1]) != "Object")
|
||||
args.push({});
|
||||
}
|
||||
else if (format[format.length-1] == "o_number") {
|
||||
if(typeof(args[args.length-1]) != "number" && objTypeOf(args[0])!="Object")
|
||||
args.push(1);
|
||||
}
|
||||
if(format[0] == "o_string_a") {
|
||||
if(args.length == format.length - 1) {
|
||||
insert = false;
|
||||
for (i in args) {
|
||||
if (format[parseInt(i)+1].includes( typeof(getPrimitive(args[i])) )){
|
||||
insert = true;
|
||||
}
|
||||
else {insert = false; break;}
|
||||
}
|
||||
if(insert)
|
||||
args.splice(0,0,copy(images));
|
||||
}
|
||||
}
|
||||
|
||||
if(args.length == format.length) {
|
||||
for (i in format) {
|
||||
if (format[i].substr(format[i].length-2,2)=="_a")
|
||||
args[i] = makeArray(args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 1) {
|
||||
json_q = copy(args[0]);
|
||||
if(!(format_i == "r"))
|
||||
for (img in json_q) {
|
||||
json_q[img] = makeArray(json_q[img]);
|
||||
}
|
||||
}
|
||||
else if (format_i == "r")
|
||||
for (img in args[0]) json_q[args[0][img]] = args[1];
|
||||
else {
|
||||
for (img in args[0]) {
|
||||
image = args[0][img];
|
||||
json_q[image] = [];
|
||||
|
||||
if(format_i == "+") {
|
||||
for(s in args[1]) {
|
||||
json_q[image].push({
|
||||
name: args[1][s],
|
||||
o: args[2]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if(format_i == "-") {
|
||||
json_q[image] = args[1];
|
||||
}
|
||||
|
||||
if(format_i == "^") {
|
||||
size = this.images[image].steps.length;
|
||||
index = args[1];
|
||||
index = (index==size)?index:index%size;
|
||||
if (index<0) index += size+1;
|
||||
json_q[image].push({
|
||||
index: index,
|
||||
name: args[2],
|
||||
o: args[3]
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return json_q;
|
||||
|
||||
}
|
||||
module.exports = formatInput;
|
||||
Reference in New Issue
Block a user