react 0.5.1 → 0.6.2
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/.travis.yml +5 -0
- package/Jakefile.js +39 -0
- package/README.md +17 -24
- package/browser-test/dist.html +89 -0
- package/browser-test/index.html +85 -0
- package/browser-test/min.html +89 -0
- package/dist/react.js +3094 -0
- package/dist/react.min.js +22 -0
- package/doc/advanced.md +2 -2
- package/doc/simple.dot +25 -0
- package/doc/simple.png +0 -0
- package/examples/using-events1.js +1 -1
- package/lib/base-task.js +116 -110
- package/lib/cb-task.js +71 -67
- package/lib/core.js +116 -117
- package/lib/dsl.js +120 -115
- package/lib/error.js +44 -36
- package/lib/event-collector.js +69 -56
- package/lib/event-manager.js +69 -56
- package/lib/eventemitter.js +20 -0
- package/lib/finalcb-first-task.js +56 -53
- package/lib/finalcb-task.js +55 -51
- package/lib/id.js +18 -7
- package/lib/input-parser.js +49 -41
- package/lib/log-events.js +79 -73
- package/lib/parse.js +34 -25
- package/lib/promise-resolve.js +42 -27
- package/lib/promise-task.js +78 -74
- package/lib/react.js +59 -0
- package/lib/ret-task.js +59 -55
- package/lib/sprintf.js +18 -0
- package/lib/status.js +11 -2
- package/lib/task.js +215 -217
- package/lib/track-tasks.js +72 -58
- package/lib/validate.js +136 -136
- package/lib/vcon.js +78 -69
- package/lib/when-task.js +69 -65
- package/package.json +10 -9
- package/src/dist.build.requirejs +20 -0
- package/test/ast.mocha.js +136 -0
- package/test/cb-task.mocha.js +220 -0
- package/test/core-deferred.mocha.js +143 -0
- package/test/core-when.mocha.js +96 -0
- package/test/core.mocha.js +589 -0
- package/test/dsl.mocha.js +350 -0
- package/test/event-manager.mocha.js +119 -0
- package/test/exec-options.mocha.js +48 -0
- package/test/finalcb-task.mocha.js +58 -0
- package/test/input-parser.mocha.js +86 -0
- package/test/mocha.opts +2 -0
- package/test/module-use.mocha.js +147 -0
- package/test/promise-auto-resolve.mocha.js +68 -0
- package/test/ret-task.mocha.js +220 -0
- package/test/task.mocha.js +42 -0
- package/test/validate-cb-task.mocha.js +100 -0
- package/test/validate-ret-task.mocha.js +110 -0
- package/test/validate.mocha.js +324 -0
- package/test/vcon.mocha.js +193 -0
- package/vendor/chai/chai.js +2038 -0
- package/vendor/jquery/jquery-1.7.1.js +9266 -0
- package/vendor/jquery/jquery-1.7.1.min.js +4 -0
- package/vendor/mocha/mocha.css +135 -0
- package/vendor/mocha/mocha.js +3589 -0
- package/vendor/node/util.js +531 -0
- package/vendor/requirejs/require.js +2053 -0
- package/vendor/requirejs/require.min.js +33 -0
- package/doc/default-simple.dot +0 -19
- package/doc/default-simple.dot.png +0 -0
- package/react.js +0 -40
- package/test/ast.test.js +0 -95
- package/test/cb-task.test.js +0 -197
- package/test/core-deferred.test.js +0 -134
- package/test/core-promised.test.js +0 -132
- package/test/core-when.test.js +0 -84
- package/test/core.test.js +0 -593
- package/test/dsl.test.js +0 -330
- package/test/event-manager.test.js +0 -102
- package/test/exec-options.test.js +0 -33
- package/test/finalcb-task.test.js +0 -38
- package/test/input-parser.test.js +0 -66
- package/test/module-use.test.js +0 -134
- package/test/promise-auto-resolve.test.js +0 -52
- package/test/ret-task.test.js +0 -199
- package/test/task.test.js +0 -21
- package/test/validate-cb-task.test.js +0 -74
- package/test/validate-ret-task.test.js +0 -83
- package/test/validate.test.js +0 -295
- package/test/vcon.test.js +0 -173
package/lib/track-tasks.js
CHANGED
|
@@ -1,60 +1,74 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
/*global define:true */
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
react.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
react.events.on(react.events.TYPES.
|
|
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
|
-
|
|
4
|
+
if (typeof define !== 'function') {
|
|
5
|
+
var define = require('amdefine')(module);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
define([], function () {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
Track the tasks, start, complete, args, results, elapsed time
|
|
12
|
+
Emits events that can be monitored
|
|
13
|
+
|
|
14
|
+
- track start and complete
|
|
15
|
+
- record args each task was called with
|
|
16
|
+
- record results at completion
|
|
17
|
+
- record start, end, and calc elapsed time
|
|
18
|
+
- emits flow.begin with flowEnv
|
|
19
|
+
- emits task.begin with task
|
|
20
|
+
- emits task.complete with task
|
|
21
|
+
- emits flow complete with flowEnv
|
|
22
|
+
- emits flow errored with flowEnv
|
|
23
|
+
|
|
24
|
+
@example
|
|
25
|
+
var react = require('react');
|
|
26
|
+
react.trackTasks(); // enable task and flow tracking
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
var trackingTasks = false;
|
|
31
|
+
|
|
32
|
+
function trackTasks(react) {
|
|
33
|
+
if (trackingTasks) return; // already tracking
|
|
34
|
+
trackingTasks = true;
|
|
35
|
+
|
|
36
|
+
react.events.on(react.events.TYPES.EXEC_FLOW_START, function (env) {
|
|
37
|
+
env.startTime = Date.now();
|
|
38
|
+
env.flowEmitter.emit(react.events.TYPES.FLOW_BEGIN, env); //fire public ev
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
react.events.on(react.events.TYPES.EXEC_TASK_START, function (task) {
|
|
42
|
+
task.startTime = Date.now();
|
|
43
|
+
task.env.flowEmitter.emit(react.events.TYPES.TASK_BEGIN, task); //fire public ev
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
react.events.on(react.events.TYPES.EXEC_TASK_COMPLETE, function (task) {
|
|
47
|
+
task.endTime = Date.now();
|
|
48
|
+
task.elapsedTime = task.endTime - task.startTime;
|
|
49
|
+
task.env.flowEmitter.emit(react.events.TYPES.TASK_COMPLETE, task); // fire public ev
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
react.events.on(react.events.TYPES.EXEC_TASK_ERRORED, function (task) {
|
|
53
|
+
task.endTime = Date.now();
|
|
54
|
+
task.elapsedTime = task.endTime - task.startTime;
|
|
55
|
+
task.env.flowEmitter.emit(react.events.TYPES.TASK_ERRORED, task); // fire public ev
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
react.events.on(react.events.TYPES.EXEC_FLOW_COMPLETE, function (env) {
|
|
59
|
+
env.endTime = Date.now();
|
|
60
|
+
env.elapsedTime = env.endTime - env.startTime;
|
|
61
|
+
env.flowEmitter.emit(react.events.TYPES.FLOW_COMPLETE, env); //fire public ev
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
react.events.on(react.events.TYPES.EXEC_FLOW_ERRORED, function (env) {
|
|
65
|
+
env.endTime = Date.now();
|
|
66
|
+
env.elapsedTime = env.endTime - env.startTime;
|
|
67
|
+
env.flowEmitter.emit(react.events.TYPES.FLOW_ERRORED, env); //fire public ev
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return trackTasks;
|
|
73
|
+
|
|
74
|
+
});
|
package/lib/validate.js
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
/*global define:true */
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
var
|
|
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
|
-
var MISSING_INPUTS = 'missing or mispelled variable referenced in flow definition: %s';
|
|
15
|
-
|
|
16
|
-
// match any of our literals true, false, int, float, quoted strings, or is property (has dot), match vcon.js
|
|
17
|
-
var LITERAL_OR_PROP_RE = /^(true|false|this|null|\-?[0-9\.]+)$|'|"|\./i;
|
|
18
|
-
|
|
19
|
-
var validateInParams, validateTasks, validateOutTask, validateTaskNamesUnique;
|
|
20
|
-
var validateLocals, validateOuputsUnique, validateNoMissingNames;
|
|
21
|
-
|
|
22
|
-
function format_error(errmsg, obj) {
|
|
23
|
-
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
true if is a literal name
|
|
28
|
-
*/
|
|
29
|
-
function isLiteralOrProp(name) { // need to match what is in vcon.js, TODO consolidate?
|
|
30
|
-
return LITERAL_OR_PROP_RE.test(name);
|
|
4
|
+
if (typeof define !== 'function') {
|
|
5
|
+
var define = require('amdefine')(module);
|
|
31
6
|
}
|
|
32
7
|
|
|
8
|
+
define(['util', './sprintf', 'ensure-array', './task'], function (util, sprintf, array, taskUtil) {
|
|
9
|
+
/*jshint latedef:false */
|
|
10
|
+
|
|
11
|
+
var AST_IS_OBJECT = 'ast must be an object with inParams, tasks, and outTask';
|
|
12
|
+
var INPARAMS_ARR_STR = 'ast.inParams must be an array of strings';
|
|
13
|
+
var TASKS_ARR = 'ast.tasks must be an array of tasks';
|
|
14
|
+
var NAMES_UNIQUE = 'ast.tasks that specify name need to be unique, duplicate:';
|
|
15
|
+
var LOCALS_NOTNULL = 'ast.locals should not be null';
|
|
16
|
+
var DUP_OUTPUTS = 'multiple tasks output the same param, must be unique. param';
|
|
17
|
+
var MISSING_INPUTS = 'missing or mispelled variable referenced in flow definition: %s';
|
|
18
|
+
|
|
19
|
+
// match any of our literals true, false, int, float, quoted strings, or is property (has dot), match vcon.js
|
|
20
|
+
var LITERAL_OR_PROP_RE = /^(true|false|this|null|\-?[0-9\.]+)$|'|"|\./i;
|
|
21
|
+
|
|
22
|
+
function format_error(errmsg, obj) {
|
|
23
|
+
return sprintf('%s - %s', errmsg, util.inspect(obj));
|
|
24
|
+
}
|
|
33
25
|
|
|
26
|
+
/**
|
|
27
|
+
true if is a literal name
|
|
28
|
+
*/
|
|
29
|
+
function isLiteralOrProp(name) { // need to match what is in vcon.js, TODO consolidate?
|
|
30
|
+
return LITERAL_OR_PROP_RE.test(name);
|
|
31
|
+
}
|
|
34
32
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
/**
|
|
34
|
+
validate the AST return Errors
|
|
35
|
+
@example
|
|
36
|
+
var validate = require('./validate');
|
|
37
|
+
var errors = validate(ast);
|
|
38
|
+
@returns array of errors, could be empty
|
|
41
39
|
*/
|
|
42
|
-
function validate(ast) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
40
|
+
function validate(ast) {
|
|
41
|
+
if (!ast || !ast.inParams || !ast.tasks || !ast.outTask) return [AST_IS_OBJECT];
|
|
42
|
+
var errors = [];
|
|
43
|
+
errors = errors.concat(validateInParams(ast.inParams));
|
|
44
|
+
errors = errors.concat(validateTasks(ast.tasks));
|
|
45
|
+
errors = errors.concat(validateTaskNamesUnique(ast.tasks));
|
|
46
|
+
errors = errors.concat(taskUtil.validateOutTask(ast.outTask));
|
|
47
|
+
errors = errors.concat(validateLocals(ast.locals));
|
|
48
|
+
if (errors.length === 0) { // if no errors do additional validation
|
|
49
|
+
if (ast.outTask.type !== 'finalcbFirst') errors = errors.concat(validateOuputsUnique(ast.tasks));
|
|
50
|
+
errors = errors.concat(taskUtil.validateLocalFunctions(ast.inParams, ast.tasks, ast.locals));
|
|
51
|
+
errors = errors.concat(validateNoMissingNames(ast));
|
|
52
|
+
}
|
|
53
|
+
return errors;
|
|
54
54
|
}
|
|
55
|
-
return errors;
|
|
56
|
-
}
|
|
57
55
|
|
|
58
|
-
/**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
function validateInParams(inParams) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
56
|
+
/**
|
|
57
|
+
@returns array of errors, could be empty
|
|
58
|
+
*/
|
|
59
|
+
function validateInParams(inParams) {
|
|
60
|
+
if (!Array.isArray(inParams) ||
|
|
61
|
+
!inParams.every(function (x) { return (typeof(x) === 'string'); })) {
|
|
62
|
+
return [INPARAMS_ARR_STR];
|
|
63
|
+
}
|
|
64
|
+
return [];
|
|
65
65
|
}
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
66
|
|
|
69
|
-
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
function validateTasks(tasks) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
67
|
+
/**
|
|
68
|
+
@returns array of errors, could be empty
|
|
69
|
+
*/
|
|
70
|
+
function validateTasks(tasks) {
|
|
71
|
+
if (!Array.isArray(tasks)) return [TASKS_ARR];
|
|
72
|
+
var errors = [];
|
|
73
|
+
tasks.forEach(function (t) {
|
|
74
|
+
errors = errors.concat(taskUtil.validate(t));
|
|
75
|
+
});
|
|
76
|
+
return errors;
|
|
77
|
+
}
|
|
80
78
|
|
|
81
|
-
function validateTaskNamesUnique(tasks) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
79
|
+
function validateTaskNamesUnique(tasks) {
|
|
80
|
+
if (!Array.isArray(tasks)) return [];
|
|
81
|
+
var errors = [];
|
|
82
|
+
var namedTasks = tasks.filter(function (t) { return (t.name); });
|
|
83
|
+
var names = namedTasks.map(function (t) { return t.name; });
|
|
84
|
+
names.reduce(function (accum, name) {
|
|
85
|
+
if (accum[name]) errors.push(sprintf('%s %s', NAMES_UNIQUE, name));
|
|
86
|
+
else accum[name] = true;
|
|
87
|
+
return accum;
|
|
88
|
+
}, {});
|
|
89
|
+
return errors;
|
|
90
|
+
}
|
|
93
91
|
|
|
94
|
-
function validateLocals(locals) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
92
|
+
function validateLocals(locals) {
|
|
93
|
+
var errors = [];
|
|
94
|
+
if (locals === null) errors.push(LOCALS_NOTNULL);
|
|
95
|
+
return errors;
|
|
96
|
+
}
|
|
99
97
|
|
|
100
|
-
function getOutputParams(taskDef) {
|
|
101
|
-
|
|
102
|
-
}
|
|
98
|
+
function getOutputParams(taskDef) {
|
|
99
|
+
return array(taskDef.out); //ensure array
|
|
100
|
+
}
|
|
103
101
|
|
|
104
|
-
function validateOuputsUnique(taskDefs) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
102
|
+
function validateOuputsUnique(taskDefs) {
|
|
103
|
+
var errors = [];
|
|
104
|
+
taskDefs.reduce(function (accum, t) {
|
|
105
|
+
getOutputParams(t).forEach(function (param) {
|
|
106
|
+
if (accum[param] !== undefined) errors.push(sprintf('%s: %s', DUP_OUTPUTS, param));
|
|
107
|
+
else accum[param] = true;
|
|
108
|
+
});
|
|
109
|
+
return accum;
|
|
110
|
+
}, {});
|
|
111
|
+
return errors;
|
|
112
|
+
}
|
|
115
113
|
|
|
116
114
|
|
|
117
|
-
/**
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
/**
|
|
116
|
+
validate there are no missing or mispelled param names in any task inputs
|
|
117
|
+
or the final task output
|
|
120
118
|
|
|
121
|
-
|
|
119
|
+
@return array of errors, or empty array if none
|
|
122
120
|
*/
|
|
123
|
-
function validateNoMissingNames(ast) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
121
|
+
function validateNoMissingNames(ast) {
|
|
122
|
+
var errors = [];
|
|
123
|
+
var names = {};
|
|
124
|
+
if (ast.locals) {
|
|
125
|
+
names = Object.keys(ast.locals).reduce(function (accum, k) { // start with locals
|
|
126
|
+
accum[k] = true;
|
|
127
|
+
return accum;
|
|
128
|
+
}, names);
|
|
129
|
+
}
|
|
130
|
+
ast.inParams.reduce(function (accum, p) { // add input params
|
|
131
|
+
accum[p] = true;
|
|
129
132
|
return accum;
|
|
130
133
|
}, names);
|
|
134
|
+
ast.tasks.reduce(function (accum, t) { // add task outputs
|
|
135
|
+
return t.out.reduce(function (innerAccum, p) {
|
|
136
|
+
innerAccum[p] = true;
|
|
137
|
+
return innerAccum;
|
|
138
|
+
}, accum);
|
|
139
|
+
}, names);
|
|
140
|
+
|
|
141
|
+
// now we have all possible provided vars, check task inputs are accounted for
|
|
142
|
+
ast.tasks.reduce(function (accum, t) { // for all tasks
|
|
143
|
+
return t.a.reduce(function (innerAccum, p) { // for all in params, except property
|
|
144
|
+
if (!isLiteralOrProp(p) && !names[p]) innerAccum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
|
|
145
|
+
return innerAccum;
|
|
146
|
+
}, accum);
|
|
147
|
+
}, errors);
|
|
148
|
+
|
|
149
|
+
// now check the final task outputs
|
|
150
|
+
ast.outTask.a.reduce(function (accum, p) { // for final task out params
|
|
151
|
+
if (!isLiteralOrProp(p) && !names[p]) accum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
|
|
152
|
+
return accum;
|
|
153
|
+
}, errors);
|
|
154
|
+
return errors;
|
|
131
155
|
}
|
|
132
|
-
ast.inParams.reduce(function (accum, p) { // add input params
|
|
133
|
-
accum[p] = true;
|
|
134
|
-
return accum;
|
|
135
|
-
}, names);
|
|
136
|
-
ast.tasks.reduce(function (accum, t) { // add task outputs
|
|
137
|
-
return t.out.reduce(function (innerAccum, p) {
|
|
138
|
-
innerAccum[p] = true;
|
|
139
|
-
return innerAccum;
|
|
140
|
-
}, accum);
|
|
141
|
-
}, names);
|
|
142
|
-
|
|
143
|
-
// now we have all possible provided vars, check task inputs are accounted for
|
|
144
|
-
ast.tasks.reduce(function (accum, t) { // for all tasks
|
|
145
|
-
return t.a.reduce(function (innerAccum, p) { // for all in params, except property
|
|
146
|
-
if (!isLiteralOrProp(p) && !names[p]) innerAccum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
|
|
147
|
-
return innerAccum;
|
|
148
|
-
}, accum);
|
|
149
|
-
}, errors);
|
|
150
|
-
|
|
151
|
-
// now check the final task outputs
|
|
152
|
-
ast.outTask.a.reduce(function (accum, p) { // for final task out params
|
|
153
|
-
if (!isLiteralOrProp(p) && !names[p]) accum.push(sprintf(MISSING_INPUTS, p)); // add error if missing
|
|
154
|
-
return accum;
|
|
155
|
-
}, errors);
|
|
156
|
-
return errors;
|
|
157
|
-
}
|
|
158
156
|
|
|
159
|
-
|
|
157
|
+
return validate;
|
|
158
|
+
|
|
159
|
+
});
|
package/lib/vcon.js
CHANGED
|
@@ -1,81 +1,90 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
/*global define:true */
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function VContext() {
|
|
4
|
+
if (typeof define !== 'function') {
|
|
5
|
+
var define = require('amdefine')(module);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
define([], function () {
|
|
9
|
+
|
|
10
|
+
var LAST_RESULTS_KEY = ':LAST_RESULTS';
|
|
11
|
+
|
|
12
|
+
function VContext() {
|
|
13
|
+
}
|
|
10
14
|
|
|
11
|
-
VContext.prototype.
|
|
12
|
-
|
|
13
|
-
var vConValues = this.values;
|
|
14
|
-
if (typeof(name) !== 'string') return name; // literal boolean or number
|
|
15
|
-
name = name.trim();
|
|
16
|
-
// literal checks need to match what is in validate.js
|
|
17
|
-
if (name === 'true') return true;
|
|
18
|
-
if (name === 'false') return false;
|
|
19
|
-
if (name === 'null') return null;
|
|
20
|
-
if (/^-?[0-9]+$/.test(name)) return parseInt(name, 10); //int
|
|
21
|
-
if (/^-?[0-9.]+$/.test(name)) return parseFloat(name); //float
|
|
22
|
-
var m = /^("|')([^\1]*)\1$/.exec(name); //check for quoted string " or '
|
|
23
|
-
if (m) return m[2]; // if is quoted str, return inside of the quotes
|
|
24
|
-
var nameAndProps = name.split('.');
|
|
25
|
-
return nameAndProps.reduce(function (accObj, prop) {
|
|
26
|
-
if (accObj === undefined || accObj === null) return undefined; // prevent exception
|
|
27
|
-
return accObj[prop];
|
|
28
|
-
}, vConValues); // vCon['foo']['bar']
|
|
29
|
-
};
|
|
15
|
+
VContext.prototype.getLastResults = function () { return this.getVar(LAST_RESULTS_KEY); };
|
|
16
|
+
VContext.prototype.setLastResults = function (args) { this.setVar(LAST_RESULTS_KEY, args); };
|
|
30
17
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
18
|
+
VContext.prototype.getVar = function (name) { //name might be simple or obj.prop, also literals
|
|
19
|
+
/*jshint regexp: false */
|
|
20
|
+
var vConValues = this.values;
|
|
21
|
+
if (typeof(name) !== 'string') return name; // literal boolean or number
|
|
22
|
+
name = name.trim();
|
|
23
|
+
// literal checks need to match what is in validate.js
|
|
24
|
+
if (name === 'true') return true;
|
|
25
|
+
if (name === 'false') return false;
|
|
26
|
+
if (name === 'null') return null;
|
|
27
|
+
if (/^-?[0-9]+$/.test(name)) return parseInt(name, 10); //int
|
|
28
|
+
if (/^-?[0-9.]+$/.test(name)) return parseFloat(name); //float
|
|
29
|
+
var m = /^("|')([^\1]*)\1$/.exec(name); //check for quoted string " or '
|
|
30
|
+
if (m) return m[2]; // if is quoted str, return inside of the quotes
|
|
31
|
+
var nameAndProps = name.split('.');
|
|
32
|
+
return nameAndProps.reduce(function (accObj, prop) {
|
|
33
|
+
if (accObj === undefined || accObj === null) return undefined; // prevent exception
|
|
34
|
+
return accObj[prop];
|
|
35
|
+
}, vConValues); // vCon['foo']['bar']
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
Saves all the results from a task as a unit, also sets special
|
|
40
|
+
variable :LAST_RESULTS which keeps an array of the last values
|
|
41
|
+
which can be used for chaining and testing last results, etc.
|
|
35
42
|
*/
|
|
36
|
-
VContext.prototype.saveResults = function (paramArr, valuesArr) { // set values for params
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
+
VContext.prototype.saveResults = function (paramArr, valuesArr) { // set values for params
|
|
44
|
+
var self = this;
|
|
45
|
+
paramArr.forEach(function (k, idx) { //save values to v context
|
|
46
|
+
self.setVar(k, (valuesArr[idx] !== undefined) ? valuesArr[idx] : null); //upgrade any undefined to null
|
|
47
|
+
});
|
|
48
|
+
this.setLastResults(valuesArr);
|
|
49
|
+
};
|
|
43
50
|
|
|
44
|
-
VContext.prototype.setVar = function (name, value) { //name might be simple or obj.prop
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
};
|
|
51
|
+
VContext.prototype.setVar = function (name, value) { //name might be simple or obj.prop
|
|
52
|
+
if (!name) return; // if name is undefined or null, then discard
|
|
53
|
+
var vConValues = this.values;
|
|
54
|
+
var nameAndProps = name.split('.');
|
|
55
|
+
var lastProp = nameAndProps.pop();
|
|
56
|
+
var obj = nameAndProps.reduce(function (accObj, prop) {
|
|
57
|
+
var o = accObj[prop];
|
|
58
|
+
if (o === undefined || o === null) { // if doesn't exist create it
|
|
59
|
+
o = accObj[prop] = { };
|
|
60
|
+
}
|
|
61
|
+
return o;
|
|
62
|
+
}, vConValues); // vCon['foo']['bar']
|
|
63
|
+
obj[lastProp] = value;
|
|
64
|
+
};
|
|
58
65
|
|
|
59
66
|
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
VContext.create = function (args, inParams, locals, self) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
};
|
|
67
|
+
/**
|
|
68
|
+
Create Variable Context using arguments passed in.
|
|
69
|
+
Ignore extra arguments passed in. Locals can be
|
|
70
|
+
passed into seed the VContext otherwise empty {}
|
|
71
|
+
will be used
|
|
72
|
+
@param self used to pass 'this' context in
|
|
73
|
+
*/
|
|
74
|
+
VContext.create = function (args, inParams, locals, self) {
|
|
75
|
+
var initValues = {};
|
|
76
|
+
if (self) initValues['this'] = self;
|
|
77
|
+
if (locals) Object.keys(locals).forEach(function (k) { initValues[k] = locals[k]; }); // copy over keys
|
|
78
|
+
var vContext = new VContext();
|
|
79
|
+
vContext.values = args.reduce(function (vcon, x, idx) { // create vCon start with input args
|
|
80
|
+
var param = inParams[idx];
|
|
81
|
+
if (param) vcon[param] = (x !== undefined) ? x : null; // upgrade undefined to null
|
|
82
|
+
return vcon;
|
|
83
|
+
}, initValues);
|
|
84
|
+
return vContext;
|
|
85
|
+
};
|
|
86
|
+
|
|
79
87
|
|
|
88
|
+
return VContext;
|
|
80
89
|
|
|
81
|
-
|
|
90
|
+
});
|