0
\$\begingroup\$

I have a JSON array or literal (passes through JSON.parse).

Requirement: an array of tasks which contains operations which are key/value pairs.

Array of tasks> Array of operations (array of operations at times will contain duplicate key/value pairs)

[
 [
 {
 resize: [1200, 1200]
 }, {
 moveTo: "dest/nick"
 }, {
 rename: "{base}-12.{ext}"
 }, {
 toWeb: true
 }, {
 rename: "{base}.{ext}"
 }
 ], [
 {
 resize: [1000, 1000]
 }, {
 rename: "{base}-10.{ext}"
 }
 ]
 ]

The code works and I simply want to know if there is a better way of doing this. I had to wrap the individual operations with {} so that the duplicate keys wouldn't overwrite each other.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 20, 2015 at 12:19
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I think I would replace { resize: [1200, 1200] } with { operation: 'resize', parameters: [1200, 1200] }. This is more verbose, but easier to maintain/read. This way it's eligible for extension (maybe you want to add identifiers later?). The same can be done for the tasks.

Other than that there's not a lot to say. I find the indentation not easily readable, but that's just style. I'd prefer:

[
 {
 name: "I'm task one",
 operations: [
 {
 operation: "resize",
 parameters: [1200, 1200]
 },
 {
 operation: "moveTo",
 parameters: ["dest/nick"]
 },
 {
 operation: "rename"
 parameters: ["{base}-12.{ext}"]
 },
 {
 operation: "toWeb",
 parameters: [true]
 },
 {
 operation: "rename",
 parameters: ["{base}.{ext}"]
 }
 ]
 },
 {
 name: "I'm task two",
 operations: [
 {
 operation: "resize",
 parameters: [1000, 1000]
 },
 {
 operation: "rename",
 parameters: ["{base}-10.{ext}"]
 }
 ]
 }
]

Also note that the value of the parameters is an array, even if it has one value.

answered Sep 20, 2015 at 12:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.