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.

\$\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.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.