I imagine that some (many ?) new users of omniJS might at first be more accustomed to reading and writing AppleScript than JavaScript.
JavaScript allows for a number of styles, and glancing through the early drafts of the excellent https://omni-automation.com/ pages, it occurred to me that it might be worth thinking about what JS style conventions might turn out to be:
- Clearer for reading JS
- Less error-prone for writing JS
For example:
in lieu of
actionNames = new Array()
we could just write:
actionNames = []
which is not unfamiliar in spirit to an AppleScript statement like set actionNames to {}
I notice, FWIW, that Google’s JS guidelines also prefer these briefer and simpler ‘object literals’ like [] and {}.
Perhaps another example might be the three / four component:
- New array
- forEach
- Array.push
- Return
vs the one-stage Array.map ?
One of the delights of escaping from AppleScript to JavaScript is no longer being obliged to endlessly fiddle with setting up loops and tripping over their border conditions, so I am very pleased to see the first https://omni-automation.com/ examples already preferring Array.forEach to the messier and more complex for(initialize; condition; step) etc.
Having made that transition, though, I wonder if it might be cleaner or clearer to just go for Array.map ?
We could, for example, rewrite this 3 stage (Create, repeatedly push into, Return)
actionNames = new Array()
aPlugin.actions.forEach(function(action){
actionNames.push(action.name)
})
return actionNames
as the single stage (new array from actions mapped to their names)
return aPlugin.actions.map(function(action) {
return action.name
})
Relieving the user of all that array creation and repeated pushing - perhaps less error-prone to write as well as quicker to read ?