react 0.5.2 → 0.6.0

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