forked from TooTallNate/nTunes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nTunes-command.js
62 lines (54 loc) · 1.58 KB
/
nTunes-command.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var sys = require("sys");
// Represents one iTunes AppleScript "command".
function nCommand(command) {
this.name = command["@"].name;
this.description = command["@"].description;
this.parameters = [];
// There can only be one "direct-parameter".
if (command['direct-parameter']) {
this.parameters.push(
nCommand.createParamObject(command['direct-parameter'])
);
}
if (command.parameter) {
if (Array.isArray(command.parameter)) {
command.parameter.forEach(function(parameter) {
this.parameters.push(
nCommand.createParamObject(parameter)
);
}, this);
} else {
this.parameters.push(
nCommand.createParamObject(command.parameter)
);
}
}
// There can only be one return value for the command.
if (command.result) {
this.result = {
type: command.result['@'].type,
description: command.result['@'].description
};
}
}
var commands = {};
nCommand.processCommands = function(list) {
for (var i=0, l=list.length; i<l; i++) {
var c = new nCommand(list[i]);
commands[c.name] = c;
}
nCommand.REGEXP = new RegExp(Object.keys(commands).join("|"));
}
nCommand.getCommand = function(command) {
return commands[command];
}
nCommand.createParamObject = function(parameter) {
return {
name: parameter['@'].name || "value",
type: parameter['@'].type || parameter.type['@'].type,
list: parameter.type ? parameter.type['@'].list == "yes": false,
optional: parameter['@'].optional == "yes",
description: parameter['@'].description
};
}
module.exports = nCommand;