react 0.0.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +6 -0
- package/Jakefile.js +8 -0
- package/README.md +226 -86
- package/examples/ast1.js +26 -0
- package/examples/chain-events1.js +34 -0
- package/examples/chain1.js +19 -0
- package/examples/default-events1.js +52 -0
- package/examples/default1.js +40 -0
- package/examples/fstr-events1.js +51 -0
- package/examples/fstr1.js +36 -0
- package/examples/pcode1.js +22 -0
- package/jake-tasks/jake-test.js +64 -0
- package/lib/base-task.js +115 -0
- package/lib/cb-task.js +67 -0
- package/lib/chain.js +148 -0
- package/lib/core.js +96 -0
- package/lib/dsl.js +122 -0
- package/lib/error.js +37 -0
- package/lib/event-manager.js +57 -0
- package/lib/finalcb-first-task.js +59 -0
- package/lib/finalcb-task.js +54 -0
- package/lib/fstr.js +110 -0
- package/lib/id.js +10 -0
- package/lib/input-parser.js +44 -0
- package/lib/parse.js +29 -0
- package/lib/pcode.js +164 -0
- package/lib/ret-task.js +67 -0
- package/lib/status.js +5 -0
- package/lib/task.js +234 -0
- package/lib/validate.js +102 -0
- package/lib/vcon.js +76 -0
- package/oldExamples/analyze.js +29 -0
- package/oldExamples/analyze2.js +29 -0
- package/oldExamples/example10-dsl.js +63 -0
- package/oldExamples/example11.js +62 -0
- package/oldExamples/example12.js +63 -0
- package/oldExamples/example13.js +63 -0
- package/oldExamples/example14.js +63 -0
- package/oldExamples/example15.js +75 -0
- package/{test → oldExamples}/example6-ast.js +4 -4
- package/{test → oldExamples}/example6-dsl.js +3 -2
- package/{test → oldExamples}/example8-ast.js +3 -2
- package/{test → oldExamples}/example8-dsl.js +3 -2
- package/{test → oldExamples}/example9-ast.js +3 -2
- package/{test → oldExamples}/example9-dsl.js +3 -2
- package/oldExamples/function-str-ex1.js +33 -0
- package/oldExamples/function-str-ex2.js +67 -0
- package/oldExamples/trait1.js +41 -0
- package/oldExamples/trait2.js +44 -0
- package/package.json +16 -6
- package/react.js +11 -1
- package/test/ast.test.js +69 -0
- package/test/cb-task.test.js +197 -0
- package/test/chain.test.js +239 -0
- package/test/core.test.js +519 -0
- package/test/dsl.test.js +237 -0
- package/test/event-manager.test.js +102 -0
- package/test/exec-options.test.js +32 -0
- package/test/finalcb-task.test.js +37 -0
- package/test/fstr.test.js +288 -0
- package/test/input-parser.test.js +62 -0
- package/test/module-use.test.js +317 -0
- package/test/pcode.test.js +321 -0
- package/test/ret-task.test.js +199 -0
- package/test/task.test.js +21 -0
- package/test/validate-cb-task.test.js +74 -0
- package/test/validate-ret-task.test.js +83 -0
- package/test/validate.test.js +218 -0
- package/test/vcon.test.js +160 -0
- package/lib/react.js +0 -239
package/lib/ret-task.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var util = require('util');
|
|
4
|
+
var sprintf = require('sprintf').sprintf;
|
|
5
|
+
|
|
6
|
+
var BaseTask = require('./base-task.js');
|
|
7
|
+
|
|
8
|
+
function format_error(errmsg, obj) {
|
|
9
|
+
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var REQ = 'retTask requires f, a, out';
|
|
13
|
+
var FN_REQ = 'retTask requires f to be a function or string';
|
|
14
|
+
var A_REQ = 'retTask requires a to be an array of string param names';
|
|
15
|
+
var RET_REQ = 'retTask requires out to be an array with single string param name or []';
|
|
16
|
+
|
|
17
|
+
function RetTask(taskDef) {
|
|
18
|
+
var self = this;
|
|
19
|
+
Object.keys(taskDef).forEach(function (k) { self[k] = taskDef[k]; });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
RetTask.prototype = new BaseTask();
|
|
23
|
+
RetTask.prototype.constructor = RetTask;
|
|
24
|
+
|
|
25
|
+
RetTask.validate = function (taskDef) {
|
|
26
|
+
var errors = [];
|
|
27
|
+
if (!taskDef.f || !taskDef.a || !taskDef.out) {
|
|
28
|
+
errors.push(format_error(REQ, taskDef));
|
|
29
|
+
} else {
|
|
30
|
+
var ftype = typeof(taskDef.f);
|
|
31
|
+
if (! ((taskDef.f instanceof Function) || (ftype === 'string'))) {
|
|
32
|
+
errors.push(format_error(FN_REQ, taskDef));
|
|
33
|
+
}
|
|
34
|
+
if (! (Array.isArray(taskDef.a) &&
|
|
35
|
+
taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
|
|
36
|
+
errors.push(format_error(A_REQ, taskDef));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (! (Array.isArray(taskDef.out) &&
|
|
40
|
+
(taskDef.out.length === 0 ||
|
|
41
|
+
(taskDef.out.length === 1 && typeof(taskDef.out[0] === 'string'))))) {
|
|
42
|
+
errors.push(format_error(RET_REQ, taskDef));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return errors;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
RetTask.prototype.exec = function exec(vCon, handleError, contExec) {
|
|
49
|
+
try {
|
|
50
|
+
var args = this.a.map(function (k) { return vCon.getVar(k); }); //get args from vCon
|
|
51
|
+
this.start(args); //note the start time, args
|
|
52
|
+
var func = this.f;
|
|
53
|
+
var bindObj = null; //global space
|
|
54
|
+
if (this.isMethodCall()) { //if method call then reset func and bindObj
|
|
55
|
+
func = vCon.getVar(this.f);
|
|
56
|
+
bindObj = this.getMethodObj(vCon);
|
|
57
|
+
} else if (typeof(func) === 'string') {
|
|
58
|
+
func = vCon.getVar(func); // we want the actual fn from this string
|
|
59
|
+
}
|
|
60
|
+
var results = [func.apply(bindObj, args)];
|
|
61
|
+
vCon.saveResults(this.out, results); // save retval, takes arrays
|
|
62
|
+
this.complete(results);
|
|
63
|
+
contExec(); // continue since no callback to run this
|
|
64
|
+
} catch (err) { handleError(this, err); } // catch and handle the task error, calling final cb
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
module.exports = RetTask;
|
package/lib/status.js
ADDED
package/lib/task.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var util = require('util');
|
|
4
|
+
var sprintf = require('sprintf').sprintf;
|
|
5
|
+
var array = require('ensure-array');
|
|
6
|
+
var CbTask = require('./cb-task.js');
|
|
7
|
+
var RetTask = require('./ret-task.js');
|
|
8
|
+
var FinalCbTask = require('./finalcb-task.js');
|
|
9
|
+
var FinalCbFirstSuccTask = require('./finalcb-first-task.js');
|
|
10
|
+
var STATUS = require('./status.js');
|
|
11
|
+
var error = require('./error.js');
|
|
12
|
+
var VContext = require('./vcon.js');
|
|
13
|
+
|
|
14
|
+
var TASK_TYPES = {
|
|
15
|
+
cb: CbTask,
|
|
16
|
+
ret: RetTask
|
|
17
|
+
};
|
|
18
|
+
function taskTypeKeys() { return Object.keys(TASK_TYPES); }
|
|
19
|
+
|
|
20
|
+
var OUT_TASK_TYPES = {
|
|
21
|
+
finalcb: FinalCbTask, //first task is the default if no type specified in taskDef
|
|
22
|
+
finalcbFirst: FinalCbFirstSuccTask
|
|
23
|
+
};
|
|
24
|
+
function outTaskTypeKeys() { return Object.keys(OUT_TASK_TYPES); }
|
|
25
|
+
|
|
26
|
+
var LOCAL_FN_MISSING = 'function: %s not found in locals or input params - task[%s]';
|
|
27
|
+
var TASKDEF_IS_OBJECT = 'task must be an object';
|
|
28
|
+
var NO_TASKS_RUNNING_WONT_COMPLETE = 'no tasks running, flow will not complete';
|
|
29
|
+
var TASK_TYPE_SHOULD_MATCH = 'task.type should match one of ' +
|
|
30
|
+
Object.keys(TASK_TYPES).join(', ');
|
|
31
|
+
|
|
32
|
+
var validateTaskType, validateTask, create;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
function format_error(errmsg, obj) {
|
|
36
|
+
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
guess the missing types from params.
|
|
41
|
+
Augments in place but also returns taskDef.
|
|
42
|
+
If not specified then is 'cb'
|
|
43
|
+
*/
|
|
44
|
+
function setMissingType(taskDef) {
|
|
45
|
+
if (taskDef.type) return taskDef; //already set, return
|
|
46
|
+
taskDef.type = 'cb';
|
|
47
|
+
return taskDef;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setMissingOutTaskType(taskDef) {
|
|
51
|
+
if (!taskDef.type) taskDef.type = Object.keys(OUT_TASK_TYPES)[0]; //use first outTask type as default
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ensureAfterArrStrings(taskDef) { // convert any fn to str, and make sure is array
|
|
55
|
+
if (!taskDef.after) return;
|
|
56
|
+
var afterArr = array(taskDef.after); // ensure is array, null becomes []
|
|
57
|
+
afterArr = afterArr.map(function (a) { return (typeof(a) === 'function') ? a.name : a; });
|
|
58
|
+
taskDef.after = afterArr;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
@returns array of errors for taskDef, could be empty
|
|
63
|
+
*/
|
|
64
|
+
function validate(taskDef) {
|
|
65
|
+
if (!taskDef || typeof(taskDef) !== 'object') {
|
|
66
|
+
return [format_error(TASKDEF_IS_OBJECT, taskDef)];
|
|
67
|
+
}
|
|
68
|
+
setMissingType(taskDef);
|
|
69
|
+
ensureAfterArrStrings(taskDef);
|
|
70
|
+
var errors = [];
|
|
71
|
+
errors = errors.concat(validateTaskType(taskDef));
|
|
72
|
+
errors = errors.concat(validateTask(taskDef));
|
|
73
|
+
return errors;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validateTaskType(taskDef) {
|
|
77
|
+
var errors = [];
|
|
78
|
+
if (!Object.keys(TASK_TYPES).some(
|
|
79
|
+
function (type) { return (taskDef.type === type); })) {
|
|
80
|
+
errors.push(format_error(TASK_TYPE_SHOULD_MATCH, taskDef));
|
|
81
|
+
}
|
|
82
|
+
return errors;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function validateTask(taskDef) {
|
|
86
|
+
var errors = [];
|
|
87
|
+
var taskCons = TASK_TYPES[taskDef.type];
|
|
88
|
+
if (taskCons) {
|
|
89
|
+
errors = errors.concat(taskCons.validate(taskDef));
|
|
90
|
+
}
|
|
91
|
+
return errors;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function validateOutTask(taskDef) {
|
|
95
|
+
var errors = [];
|
|
96
|
+
setMissingOutTaskType(taskDef);
|
|
97
|
+
var taskCons = OUT_TASK_TYPES[taskDef.type];
|
|
98
|
+
errors = errors.concat(taskCons.validate(taskDef));
|
|
99
|
+
return errors;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
function validateLocalFunctions(inParams, taskDefs, locals) {
|
|
104
|
+
var errors = [];
|
|
105
|
+
function foo() { } //used to mock args as fns for validation check
|
|
106
|
+
var mock_args = inParams.map(function (p) { return foo; }); //mock args with fns
|
|
107
|
+
var vCon = VContext.create(mock_args, inParams, locals);
|
|
108
|
+
var tasks = taskDefs.map(create);
|
|
109
|
+
tasks.forEach(function (t, idx) {
|
|
110
|
+
if (!t.functionExists(vCon)) { // error if function doesnt exist AND
|
|
111
|
+
if (!t.isMethodCall()) errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx)); // not method OR
|
|
112
|
+
else {
|
|
113
|
+
var obj = t.getMethodObj(vCon);
|
|
114
|
+
if (obj && obj !== foo) { // (has parent but not our mock)
|
|
115
|
+
errors.push(sprintf(LOCAL_FN_MISSING, t.f, idx));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return errors;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function fName(fn) {
|
|
124
|
+
return (typeof(fn) === 'string') ? fn : fn.name;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
Name tasks that are not already named. Prenamed task uniquness validation
|
|
129
|
+
will be done in validate.
|
|
130
|
+
|
|
131
|
+
This modifies the tasks with the new names.
|
|
132
|
+
|
|
133
|
+
@returns map of names to tasks
|
|
134
|
+
*/
|
|
135
|
+
function nameTasks(tasks) { //name tasks that are not already named, validation done elsewhere, ret map
|
|
136
|
+
var namesMap = tasks.reduce(function (map, t) {
|
|
137
|
+
if (t.name) { map[t.name] = t; }
|
|
138
|
+
return map;
|
|
139
|
+
}, {});
|
|
140
|
+
tasks.forEach(function (t, idx) {
|
|
141
|
+
if (!t.name) { //not already named
|
|
142
|
+
var name = fName(t.f);
|
|
143
|
+
if (!name || namesMap[name]) {
|
|
144
|
+
name = sprintf('%s_%s', name, idx); //if empty or already used, postfix with _idx
|
|
145
|
+
}
|
|
146
|
+
t.name = name;
|
|
147
|
+
namesMap[name] = t;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return namesMap;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function create(taskDef) {
|
|
154
|
+
var TaskConstructor = TASK_TYPES[taskDef.type];
|
|
155
|
+
return new TaskConstructor(taskDef);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function createOutTask(taskDef, cbFinal, tasks, vCon) {
|
|
159
|
+
setMissingOutTaskType(taskDef);
|
|
160
|
+
var TaskConstructor = OUT_TASK_TYPES[taskDef.type];
|
|
161
|
+
return new TaskConstructor(taskDef, cbFinal, tasks, vCon);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function createCallback(task, handleTaskError, vCon, contExec) {
|
|
165
|
+
return function (err, arg0, arg1, argn) {
|
|
166
|
+
var args = Array.prototype.slice.call(arguments, 1);
|
|
167
|
+
if (err) { handleTaskError(task, err); return; } //handle error and return, we are done
|
|
168
|
+
|
|
169
|
+
//no error, save callback args to vCon context, then continue execution
|
|
170
|
+
vCon.saveResults(task.out, args);
|
|
171
|
+
task.complete(args);
|
|
172
|
+
contExec();
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function createErrorHandler(vCon, outTask) {
|
|
177
|
+
return function handleError(task, err) {
|
|
178
|
+
task.status = STATUS.ERRORED;
|
|
179
|
+
var errWithMeta = error.augmentError(err, {task: task, vcon: vCon});
|
|
180
|
+
outTask.exec(errWithMeta); //call the final callback with the first error hit
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function findTasksReady(vCon, tasks, tasksByName) {
|
|
185
|
+
return tasks.filter(function (t) { return t.isReady(vCon, tasksByName); });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function execTasks(tasksReady, vCon, handleError, contExec) {
|
|
189
|
+
tasksReady.forEach(function (t) { t.status = STATUS.READY; }); //set ready first, no double exec
|
|
190
|
+
tasksReady.forEach(function (t) { t.exec(vCon, handleError, contExec); });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
this will be called if there are no tasks found to run,
|
|
195
|
+
and it will check if there are still tasks running or ready
|
|
196
|
+
(which means they will be running shortly), in which
|
|
197
|
+
case everything is fine. If no tasks are running then
|
|
198
|
+
call handleError since this will never complete.
|
|
199
|
+
*/
|
|
200
|
+
function checkIfTasksRunning(vCon, tasks, handleError) {
|
|
201
|
+
var tasksRunning = tasks.filter(function (t) {
|
|
202
|
+
return (t.status === STATUS.RUNNING || t.status === STATUS.READY);
|
|
203
|
+
});
|
|
204
|
+
if (!tasksRunning.length) handleError({}, new Error(NO_TASKS_RUNNING_WONT_COMPLETE));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function findReadyAndExec(vCon, tasks, tasksByName, handleError, contExec) {
|
|
208
|
+
var tasksReady = findTasksReady(vCon, tasks, tasksByName);
|
|
209
|
+
if (!tasksReady.length) checkIfTasksRunning(vCon, tasks, handleError); // no tasks to run, check if ok
|
|
210
|
+
execTasks(tasksReady, vCon, handleError, contExec);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function serializeTasks(tasks) { // conveniently set after for each task idx > 0
|
|
214
|
+
nameTasks(tasks);
|
|
215
|
+
tasks.forEach(function (t, idx, arr) { if (idx !== 0) t.after = [arr[idx - 1].name]; });
|
|
216
|
+
return tasks;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
exports.serializeTasks = serializeTasks;
|
|
220
|
+
|
|
221
|
+
exports.TASK_TYPES = TASK_TYPES;
|
|
222
|
+
exports.taskTypeKeys = taskTypeKeys;
|
|
223
|
+
exports.OUT_TASK_TYPES = OUT_TASK_TYPES;
|
|
224
|
+
exports.outTaskTypeKeys = outTaskTypeKeys;
|
|
225
|
+
exports.setMissingType = setMissingType;
|
|
226
|
+
exports.validate = validate;
|
|
227
|
+
exports.validateOutTask = validateOutTask;
|
|
228
|
+
exports.validateLocalFunctions = validateLocalFunctions;
|
|
229
|
+
exports.nameTasks = nameTasks;
|
|
230
|
+
exports.create = create;
|
|
231
|
+
exports.createOutTask = createOutTask;
|
|
232
|
+
exports.createCallback = createCallback;
|
|
233
|
+
exports.createErrorHandler = createErrorHandler;
|
|
234
|
+
exports.findReadyAndExec = findReadyAndExec;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var util = require('util');
|
|
4
|
+
var sprintf = require('sprintf').sprintf;
|
|
5
|
+
var array = require('ensure-array');
|
|
6
|
+
var tutil = require('./task.js');
|
|
7
|
+
|
|
8
|
+
var AST_IS_OBJECT = 'ast must be an object with inParams, tasks, and outTask';
|
|
9
|
+
var INPARAMS_ARR_STR = 'ast.inParams must be an array of strings';
|
|
10
|
+
var TASKS_ARR = 'ast.tasks must be an array of tasks';
|
|
11
|
+
var NAMES_UNIQUE = 'ast.tasks that specify name need to be unique, duplicate:';
|
|
12
|
+
var LOCALS_NOTNULL = 'ast.locals should not be null';
|
|
13
|
+
var DUP_OUTPUTS = 'multiple tasks output the same param, must be unique. param';
|
|
14
|
+
|
|
15
|
+
var validateInParams, validateTasks, validateOutTask, validateTaskNamesUnique;
|
|
16
|
+
var validateLocals, validateOuputsUnique;
|
|
17
|
+
|
|
18
|
+
function format_error(errmsg, obj) {
|
|
19
|
+
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
validate the AST return Errors
|
|
24
|
+
@example
|
|
25
|
+
var validate = require('./validate');
|
|
26
|
+
var errors = validate(ast);
|
|
27
|
+
@returns array of errors, could be empty
|
|
28
|
+
*/
|
|
29
|
+
function validate(ast) {
|
|
30
|
+
if (!ast || !ast.inParams || !ast.tasks || !ast.outTask) return [AST_IS_OBJECT];
|
|
31
|
+
var errors = [];
|
|
32
|
+
errors = errors.concat(validateInParams(ast.inParams));
|
|
33
|
+
errors = errors.concat(validateTasks(ast.tasks));
|
|
34
|
+
errors = errors.concat(validateTaskNamesUnique(ast.tasks));
|
|
35
|
+
errors = errors.concat(tutil.validateOutTask(ast.outTask));
|
|
36
|
+
errors = errors.concat(validateLocals(ast.locals));
|
|
37
|
+
if (errors.length === 0) { // if no errors do additional validation
|
|
38
|
+
if (ast.outTask.type !== 'finalcbFirst') errors = errors.concat(validateOuputsUnique(ast.tasks));
|
|
39
|
+
errors = errors.concat(tutil.validateLocalFunctions(ast.inParams, ast.tasks, ast.locals));
|
|
40
|
+
}
|
|
41
|
+
return errors;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
@returns array of errors, could be empty
|
|
46
|
+
*/
|
|
47
|
+
function validateInParams(inParams) {
|
|
48
|
+
if (!Array.isArray(inParams) ||
|
|
49
|
+
!inParams.every(function (x) { return (typeof(x) === 'string'); })) {
|
|
50
|
+
return [INPARAMS_ARR_STR];
|
|
51
|
+
}
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
@returns array of errors, could be empty
|
|
57
|
+
*/
|
|
58
|
+
function validateTasks(tasks) {
|
|
59
|
+
if (!Array.isArray(tasks)) return [TASKS_ARR];
|
|
60
|
+
var errors = [];
|
|
61
|
+
tasks.forEach(function (t) {
|
|
62
|
+
errors = errors.concat(tutil.validate(t));
|
|
63
|
+
});
|
|
64
|
+
return errors;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function validateTaskNamesUnique(tasks) {
|
|
68
|
+
if (!Array.isArray(tasks)) return [];
|
|
69
|
+
var errors = [];
|
|
70
|
+
var namedTasks = tasks.filter(function (t) { return (t.name); });
|
|
71
|
+
var names = namedTasks.map(function (t) { return t.name; });
|
|
72
|
+
names.reduce(function (accum, name) {
|
|
73
|
+
if (accum[name]) errors.push(sprintf('%s %s', NAMES_UNIQUE, name));
|
|
74
|
+
else accum[name] = true;
|
|
75
|
+
return accum;
|
|
76
|
+
}, {});
|
|
77
|
+
return errors;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function validateLocals(locals) {
|
|
81
|
+
var errors = [];
|
|
82
|
+
if (locals === null) errors.push(LOCALS_NOTNULL);
|
|
83
|
+
return errors;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function getOutputParams(taskDef) {
|
|
87
|
+
return array(taskDef.out); //ensure array
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function validateOuputsUnique(taskDefs) {
|
|
91
|
+
var errors = [];
|
|
92
|
+
taskDefs.reduce(function (accum, t) {
|
|
93
|
+
getOutputParams(t).forEach(function (param) {
|
|
94
|
+
if (accum[param] !== undefined) errors.push(sprintf('%s: %s', DUP_OUTPUTS, param));
|
|
95
|
+
else accum[param] = true;
|
|
96
|
+
});
|
|
97
|
+
return accum;
|
|
98
|
+
}, {});
|
|
99
|
+
return errors;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = validate;
|
package/lib/vcon.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var LAST_RESULTS_KEY = ':LAST_RESULTS';
|
|
4
|
+
|
|
5
|
+
function VContext() {
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
VContext.prototype.getLastResults = function () { return this.getVar(LAST_RESULTS_KEY); };
|
|
9
|
+
VContext.prototype.setLastResults = function (args) { this.setVar(LAST_RESULTS_KEY, args); };
|
|
10
|
+
|
|
11
|
+
VContext.prototype.getVar = function (name) { //name might be simple or obj.prop, also literals
|
|
12
|
+
var vConValues = this.values;
|
|
13
|
+
if (typeof(name) !== 'string') return name; // literal boolean or number
|
|
14
|
+
name = name.trim();
|
|
15
|
+
if (name === 'true') return true;
|
|
16
|
+
if (name === 'false') return false;
|
|
17
|
+
if (/^-?[0-9]+$/.test(name)) return parseInt(name, 10); //int
|
|
18
|
+
if (/^-?[0-9.]+$/.test(name)) return parseFloat(name); //float
|
|
19
|
+
var m = /^("|')([^\1]*)\1$/.exec(name); //check for quoted string " or '
|
|
20
|
+
if (m) return m[2]; // if is quoted str, return inside of the quotes
|
|
21
|
+
var nameAndProps = name.split('.');
|
|
22
|
+
return nameAndProps.reduce(function (accObj, prop) {
|
|
23
|
+
if (accObj === undefined || accObj === null) return undefined; // prevent exception
|
|
24
|
+
return accObj[prop];
|
|
25
|
+
}, vConValues); // vCon['foo']['bar']
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
Saves all the results from a task as a unit, also sets special
|
|
30
|
+
variable :LAST_RESULTS which keeps an array of the last values
|
|
31
|
+
which can be used for chaining and testing last results, etc.
|
|
32
|
+
*/
|
|
33
|
+
VContext.prototype.saveResults = function(paramArr, valuesArr) { // set values for params
|
|
34
|
+
var self = this;
|
|
35
|
+
paramArr.forEach(function (k, idx) { //save values to v context
|
|
36
|
+
self.setVar(k, (valuesArr[idx] !== undefined) ? valuesArr[idx] : null); //upgrade any undefined to null
|
|
37
|
+
});
|
|
38
|
+
this.setLastResults(valuesArr);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
VContext.prototype.setVar = function (name, value) { //name might be simple or obj.prop
|
|
42
|
+
if (!name) return; // if name is undefined or null, then discard
|
|
43
|
+
var vConValues = this.values;
|
|
44
|
+
var nameAndProps = name.split('.');
|
|
45
|
+
var lastProp = nameAndProps.pop();
|
|
46
|
+
var obj = nameAndProps.reduce(function (accObj, prop) {
|
|
47
|
+
var o = accObj[prop];
|
|
48
|
+
if (o === undefined || o === null) { // if doesn't exist create it
|
|
49
|
+
o = accObj[prop] = { };
|
|
50
|
+
}
|
|
51
|
+
return o;
|
|
52
|
+
}, vConValues); // vCon['foo']['bar']
|
|
53
|
+
obj[lastProp] = value;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
Create Variable Context using arguments passed in.
|
|
59
|
+
Ignore extra arguments passed in. Locals can be
|
|
60
|
+
passed into seed the VContext otherwise empty {}
|
|
61
|
+
will be used
|
|
62
|
+
*/
|
|
63
|
+
VContext.create = function (args, inParams, locals) {
|
|
64
|
+
var initValues = {};
|
|
65
|
+
if (locals) Object.keys(locals).forEach(function (k) { initValues[k] = locals[k]; }); // copy over keys
|
|
66
|
+
var vContext = new VContext();
|
|
67
|
+
vContext.values = args.reduce(function (vcon, x, idx) { // create vCon start with input args
|
|
68
|
+
var param = inParams[idx];
|
|
69
|
+
if (param) vcon[param] = x;
|
|
70
|
+
return vcon;
|
|
71
|
+
}, initValues);
|
|
72
|
+
return vContext;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
module.exports = VContext;
|