react 0.5.0 → 0.6.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.
Files changed (92) hide show
  1. package/.travis.yml +4 -0
  2. package/Jakefile.js +39 -0
  3. package/README.md +18 -26
  4. package/browser-test/dist.html +89 -0
  5. package/browser-test/index.html +85 -0
  6. package/browser-test/min.html +89 -0
  7. package/dist/react.js +3093 -0
  8. package/dist/react.min.js +1 -0
  9. package/doc/advanced.md +7 -2
  10. package/doc/simple.dot +25 -0
  11. package/doc/simple.png +0 -0
  12. package/examples/{default1.js → longer-example.js} +0 -0
  13. package/examples/{default-simple.js → simple.js} +0 -0
  14. package/examples/{ast1.js → using-ast-directly.js} +4 -0
  15. package/examples/{default-events1.js → using-events1.js} +1 -1
  16. package/examples/{default-log-events.js → using-log-events.js} +0 -0
  17. package/lib/base-task.js +116 -110
  18. package/lib/cb-task.js +71 -67
  19. package/lib/core.js +118 -107
  20. package/lib/dsl.js +120 -115
  21. package/lib/error.js +44 -36
  22. package/lib/event-collector.js +69 -53
  23. package/lib/event-manager.js +69 -55
  24. package/lib/eventemitter.js +20 -0
  25. package/lib/finalcb-first-task.js +56 -53
  26. package/lib/finalcb-task.js +55 -51
  27. package/lib/id.js +18 -6
  28. package/lib/input-parser.js +49 -41
  29. package/lib/log-events.js +79 -73
  30. package/lib/parse.js +34 -25
  31. package/lib/promise-resolve.js +42 -27
  32. package/lib/promise-task.js +78 -74
  33. package/lib/react.js +59 -0
  34. package/lib/ret-task.js +59 -55
  35. package/lib/sprintf.js +18 -0
  36. package/lib/status.js +11 -2
  37. package/lib/task.js +216 -213
  38. package/lib/track-tasks.js +72 -58
  39. package/lib/validate.js +136 -136
  40. package/lib/vcon.js +78 -69
  41. package/lib/when-task.js +69 -65
  42. package/package.json +10 -9
  43. package/src/dist.build.requirejs +20 -0
  44. package/test/ast.mocha.js +136 -0
  45. package/test/cb-task.mocha.js +220 -0
  46. package/test/core-deferred.mocha.js +143 -0
  47. package/test/core-when.mocha.js +96 -0
  48. package/test/core.mocha.js +589 -0
  49. package/test/dsl.mocha.js +350 -0
  50. package/test/event-manager.mocha.js +119 -0
  51. package/test/exec-options.mocha.js +48 -0
  52. package/test/finalcb-task.mocha.js +58 -0
  53. package/test/input-parser.mocha.js +86 -0
  54. package/test/mocha.opts +2 -0
  55. package/test/module-use.mocha.js +147 -0
  56. package/test/promise-auto-resolve.mocha.js +68 -0
  57. package/test/ret-task.mocha.js +220 -0
  58. package/test/task.mocha.js +42 -0
  59. package/test/validate-cb-task.mocha.js +100 -0
  60. package/test/validate-ret-task.mocha.js +110 -0
  61. package/test/validate.mocha.js +324 -0
  62. package/test/vcon.mocha.js +193 -0
  63. package/vendor/chai/chai.js +2038 -0
  64. package/vendor/jquery/jquery-1.7.1.js +9266 -0
  65. package/vendor/jquery/jquery-1.7.1.min.js +4 -0
  66. package/vendor/mocha/mocha.css +135 -0
  67. package/vendor/mocha/mocha.js +3589 -0
  68. package/vendor/node/util.js +531 -0
  69. package/vendor/requirejs/require.js +2053 -0
  70. package/vendor/requirejs/require.min.js +33 -0
  71. package/doc/default-simple.dot +0 -19
  72. package/doc/default-simple.dot.png +0 -0
  73. package/react.js +0 -40
  74. package/test/ast.test.js +0 -69
  75. package/test/cb-task.test.js +0 -197
  76. package/test/core-deferred.test.js +0 -134
  77. package/test/core-promised.test.js +0 -132
  78. package/test/core-when.test.js +0 -84
  79. package/test/core.test.js +0 -568
  80. package/test/dsl.test.js +0 -330
  81. package/test/event-manager.test.js +0 -102
  82. package/test/exec-options.test.js +0 -33
  83. package/test/finalcb-task.test.js +0 -38
  84. package/test/input-parser.test.js +0 -66
  85. package/test/module-use.test.js +0 -134
  86. package/test/promise-auto-resolve.test.js +0 -52
  87. package/test/ret-task.test.js +0 -199
  88. package/test/task.test.js +0 -21
  89. package/test/validate-cb-task.test.js +0 -74
  90. package/test/validate-ret-task.test.js +0 -83
  91. package/test/validate.test.js +0 -295
  92. package/test/vcon.test.js +0 -173
@@ -1,61 +1,65 @@
1
1
  'use strict';
2
+ /*global define:true */
2
3
 
3
- var sprintf = require('sprintf').sprintf;
4
- var util = require('util');
4
+ if (typeof define !== 'function') {
5
+ var define = require('amdefine')(module);
6
+ }
5
7
 
6
- var STATUS = require('./status.js');
7
- var EventManager = require('./event-manager.js');
8
+ define(['./sprintf', 'util', './status', './event-manager'],
9
+ function (sprintf, util, STATUS, EventManager) {
8
10
 
9
- var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
11
+ var OUTTASK_A_REQ = 'ast.outTask.a should be an array of string param names';
10
12
 
11
- function FinalCbTask(outTaskOptions) {
12
- var taskDef = outTaskOptions.taskDef;
13
- if (typeof(outTaskOptions.cbFunc) !== 'function') throw new Error('callback is not a function');
14
- var self = this;
15
- for (var k in taskDef) {
16
- if (true) self[k] = taskDef[k]; // if to make jshint happy
13
+ function FinalCbTask(outTaskOptions) {
14
+ var taskDef = outTaskOptions.taskDef;
15
+ if (typeof(outTaskOptions.cbFunc) !== 'function') throw new Error('callback is not a function');
16
+ var self = this;
17
+ for (var k in taskDef) {
18
+ if (true) self[k] = taskDef[k]; // if to make jshint happy
19
+ }
20
+ this.f = outTaskOptions.cbFunc;
21
+ this.tasks = outTaskOptions.tasks;
22
+ this.vCon = outTaskOptions.vCon;
23
+ this.retValue = outTaskOptions.retValue;
24
+ this.execOptions = outTaskOptions.execOptions;
25
+ this.env = outTaskOptions.env;
17
26
  }
18
- this.f = outTaskOptions.cbFunc;
19
- this.tasks = outTaskOptions.tasks;
20
- this.vCon = outTaskOptions.vCon;
21
- this.retValue = outTaskOptions.retValue;
22
- this.execOptions = outTaskOptions.execOptions;
23
- this.env = outTaskOptions.env;
24
- }
25
27
 
26
- function format_error(errmsg, obj) {
27
- return sprintf('%s - %s', errmsg, util.inspect(obj));
28
- }
28
+ function format_error(errmsg, obj) {
29
+ return sprintf('%s - %s', errmsg, util.inspect(obj));
30
+ }
29
31
 
30
32
 
31
- FinalCbTask.validate = function (taskDef) {
32
- var errors = [];
33
- if (! (Array.isArray(taskDef.a) &&
34
- taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
35
- errors.push(format_error(OUTTASK_A_REQ, taskDef));
36
- }
37
- return errors;
38
- };
39
-
40
- FinalCbTask.prototype.isReady = function () {
41
- return (this.tasks.every(function (t) { return (t.status === STATUS.COMPLETE); }));
42
- };
43
-
44
- FinalCbTask.prototype.exec = function (err) {
45
- if (!this.f) return; //must have already been called
46
- if (err) {
47
- this.env.error = err;
48
- this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_ERRORED, this.env);
49
- this.f.call(null, err); //call the final callback with the first error hit
50
- } else { // no error, call with args
51
- var vCon = this.vCon;
52
- var finalArgs = this.a.map(function (k) { return vCon.getVar(k); });
53
- finalArgs.unshift(null); //unshift err=null to front
54
- this.env.results = finalArgs;
55
- this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_COMPLETE, this.env);
56
- this.f.apply(null, finalArgs);
57
- }
58
- this.f = null; // prevent multiple calls
59
- };
33
+ FinalCbTask.validate = function (taskDef) {
34
+ var errors = [];
35
+ if (! (Array.isArray(taskDef.a) &&
36
+ taskDef.a.every(function (x) { return (typeof(x) === 'string'); }))) {
37
+ errors.push(format_error(OUTTASK_A_REQ, taskDef));
38
+ }
39
+ return errors;
40
+ };
41
+
42
+ FinalCbTask.prototype.isReady = function () {
43
+ return (this.tasks.every(function (t) { return (t.status === STATUS.COMPLETE); }));
44
+ };
45
+
46
+ FinalCbTask.prototype.exec = function (err) {
47
+ if (!this.f) return; //must have already been called
48
+ if (err) {
49
+ this.env.error = err;
50
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_ERRORED, this.env);
51
+ this.f.call(null, err); //call the final callback with the first error hit
52
+ } else { // no error, call with args
53
+ var vCon = this.vCon;
54
+ var finalArgs = this.a.map(function (k) { return vCon.getVar(k); });
55
+ finalArgs.unshift(null); //unshift err=null to front
56
+ this.env.results = finalArgs;
57
+ this.env.flowEmitter.emit(EventManager.TYPES.EXEC_FLOW_COMPLETE, this.env);
58
+ this.f.apply(null, finalArgs);
59
+ }
60
+ this.f = null; // prevent multiple calls
61
+ };
62
+
63
+ return FinalCbTask;
60
64
 
61
- module.exports = FinalCbTask;
65
+ });
package/lib/id.js CHANGED
@@ -1,10 +1,22 @@
1
1
  'use strict';
2
+ /*global define:true */
2
3
 
3
- var startingId = 0;
4
-
5
- function createUniqueId() {
6
- startingId += 1;
7
- return startingId;
4
+ if (typeof define !== 'function') {
5
+ var define = require('amdefine')(module);
8
6
  }
9
7
 
10
- exports.createUniqueId = createUniqueId;
8
+ define([], function () {
9
+
10
+ var startingId = 0;
11
+
12
+ function createUniqueId() {
13
+ startingId += 1;
14
+ if (startingId === Number.MAX_VALUE) startingId = 0; // if hits this start over //TODO need something better?
15
+ return startingId;
16
+ }
17
+
18
+ return {
19
+ createUniqueId: createUniqueId
20
+ };
21
+
22
+ });
@@ -1,48 +1,56 @@
1
1
  'use strict';
2
+ /*global define:true */
2
3
 
3
- var EventManager = require('./event-manager.js');
4
+ if (typeof define !== 'function') {
5
+ var define = require('amdefine')(module);
6
+ }
4
7
 
5
- var defaultExecOptions = {
6
- reactExecOptions: true,
7
- outputStyle: 'cb',
8
- };
8
+ define(['./event-manager'], function (EventManager) {
9
9
 
10
- var OUTPUT_STYLES = {
11
- CALLBACK: 'cb',
12
- NONE: 'none'
13
- };
10
+ var defaultExecOptions = {
11
+ reactExecOptions: true,
12
+ outputStyle: 'cb',
13
+ };
14
14
 
15
- function isExecOptions(x) { return (x && x.reactExecOptions); }
16
- function execOptionsFilter(x) { return isExecOptions(x); }
17
- function nonExecOptionsFilter(x) { return !isExecOptions(x); }
18
- function mergeExecOptions(accum, options) {
19
- Object.keys(options).forEach(function (k) { accum[k] = options[k]; });
20
- return accum;
21
- }
15
+ var OUTPUT_STYLES = {
16
+ CALLBACK: 'cb',
17
+ NONE: 'none'
18
+ };
22
19
 
23
- function splitArgs(args, inParams, style) {
24
- var result = { };
25
- result.args = inParams.map(function (p) { return args.shift(); }); // take args for input params first
26
- if (style === OUTPUT_STYLES.CALLBACK && args.length) result.cb = args.shift(); // next take the cb
27
- result.extra = args; // these remaining were after the callback
28
- return result;
29
- }
30
-
31
- function inputParser(inputArgs, ast) {
32
- var parsedInput = { };
33
- var execOptionsArr = inputArgs.filter(execOptionsFilter);
34
- execOptionsArr.unshift(defaultExecOptions);
35
- parsedInput.options = execOptionsArr.reduce(mergeExecOptions, {});
36
-
37
- var args = inputArgs.filter(nonExecOptionsFilter);
38
- var splitResult = splitArgs(args, ast.inParams, parsedInput.options.outputStyle);
39
- parsedInput.args = splitResult.args;
40
- parsedInput.cb = splitResult.cb;
41
- if (splitResult.outputStyle) parsedInput.options.outputStyle = splitResult.outputStyle;
42
- if (splitResult.extra) parsedInput.extraArgs = splitResult.extra;
43
- EventManager.global.emit(EventManager.TYPES.EXEC_INPUT_PREPROCESS, parsedInput); // hook
44
- return parsedInput;
45
- }
20
+ function isExecOptions(x) { return (x && x.reactExecOptions); }
21
+ function execOptionsFilter(x) { return isExecOptions(x); }
22
+ function nonExecOptionsFilter(x) { return !isExecOptions(x); }
23
+ function mergeExecOptions(accum, options) {
24
+ Object.keys(options).forEach(function (k) { accum[k] = options[k]; });
25
+ return accum;
26
+ }
27
+
28
+ function splitArgs(args, inParams, style) {
29
+ var result = { };
30
+ result.args = inParams.map(function (p) { return args.shift(); }); // take args for input params first
31
+ if (style === OUTPUT_STYLES.CALLBACK && args.length) result.cb = args.shift(); // next take the cb
32
+ result.extra = args; // these remaining were after the callback
33
+ return result;
34
+ }
35
+
36
+ function inputParser(inputArgs, ast) {
37
+ var parsedInput = { };
38
+ var execOptionsArr = inputArgs.filter(execOptionsFilter);
39
+ execOptionsArr.unshift(defaultExecOptions);
40
+ parsedInput.options = execOptionsArr.reduce(mergeExecOptions, {});
41
+
42
+ var args = inputArgs.filter(nonExecOptionsFilter);
43
+ var splitResult = splitArgs(args, ast.inParams, parsedInput.options.outputStyle);
44
+ parsedInput.args = splitResult.args;
45
+ parsedInput.cb = splitResult.cb;
46
+ if (splitResult.outputStyle) parsedInput.options.outputStyle = splitResult.outputStyle;
47
+ if (splitResult.extra) parsedInput.extraArgs = splitResult.extra;
48
+ EventManager.global.emit(EventManager.TYPES.EXEC_INPUT_PREPROCESS, parsedInput); // hook
49
+ return parsedInput;
50
+ }
51
+
52
+
53
+ inputParser.defaultExecOptions = defaultExecOptions;
54
+ return inputParser;
46
55
 
47
- module.exports = inputParser;
48
- module.exports.defaultExecOptions = defaultExecOptions;
56
+ });
package/lib/log-events.js CHANGED
@@ -1,86 +1,92 @@
1
1
  'use strict';
2
+ /*global define:true */
2
3
 
3
- /**
4
- Log events to console.error
4
+ if (typeof define !== 'function') {
5
+ var define = require('amdefine')(module);
6
+ }
5
7
 
6
- @example
7
- var react = require('react');
8
- react.logEvents(); // log all task and flow events on all react functions
9
- react.logEvents('task.*'); // log all task events on all react functions
10
- react.logEvents(flowFn); // log all task and flow events on flowFn only
11
- react.logEvents(flowFn, 'flow.*'); // log all flow events on flowFn only
12
- */
8
+ define(['util'], function (util) { // TODO replace util.inspect with something portable to browser
13
9
 
14
- var util = require('util'); // TODO replace inspect with something portable to browser
10
+ var logEventsMod = { };
11
+
12
+ /**
13
+ Log events to console.error
15
14
 
16
- var react = require('../'); // require('react');
17
- react.trackTasks(); // enable task and flow tracking
15
+ @example
16
+ var react = require('react');
17
+ react.logEvents(); // log all task and flow events on all react functions
18
+ react.logEvents('task.*'); // log all task events on all react functions
19
+ react.logEvents(flowFn); // log all task and flow events on flowFn only
20
+ react.logEvents(flowFn, 'flow.*'); // log all flow events on flowFn only
21
+ */
18
22
 
19
- var ALL_FLOW_EVENTS = 'flow.*';
20
- var ALL_TASK_EVENTS = 'task.*';
21
- var FLOW_RE = /^flow\./;
23
+ var ALL_FLOW_EVENTS = 'flow.*';
24
+ var ALL_TASK_EVENTS = 'task.*';
25
+ var FLOW_RE = /^flow\./;
22
26
 
23
- function flowLog(obj) {
24
- /*jshint validthis: true */
25
- var time = new Date();
26
- time.setTime(obj.time);
27
- var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
28
- var eventTimeStr = time.toISOString();
29
- if (this.event === 'flow.complete') {
30
- var env = obj;
31
- console.error('%s: %s \tmsecs: %s \n\targs: %s \n\tresults: %s\n',
32
- this.event, env.name, env.elapsedTime, util.inspect(argsNoCb), util.inspect(env.results));
33
- } else {
34
- var name = obj.name;
35
- var args = obj.args;
36
- console.error('%s: %s \n\targs: %s\n', this.event, name, util.inspect(argsNoCb));
37
- }
38
- }
27
+ function flowLog(obj) {
28
+ /*jshint validthis: true */
29
+ var time = new Date();
30
+ time.setTime(obj.time);
31
+ var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
32
+ var eventTimeStr = time.toISOString();
33
+ if (this.event === 'flow.complete') {
34
+ var env = obj;
35
+ console.error('%s: %s \tmsecs: %s \n\targs: %s \n\tresults: %s\n',
36
+ this.event, env.name, env.elapsedTime, util.inspect(argsNoCb), util.inspect(env.results));
37
+ } else {
38
+ var name = obj.name;
39
+ var args = obj.args;
40
+ console.error('%s: %s \n\targs: %s\n', this.event, name, util.inspect(argsNoCb));
41
+ }
42
+ }
39
43
 
40
- function taskLog(obj) {
41
- /*jshint validthis: true */
42
- var time = new Date();
43
- time.setTime(obj.time);
44
- var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
45
- var eventTimeStr = time.toISOString();
46
- if (this.event === 'task.complete') {
47
- var task = obj;
48
- console.error('%s: %s \tmsecs: %s \n\targs: %s \n\tresults: %s\n',
49
- this.event, task.name, task.elapsedTime, util.inspect(argsNoCb), util.inspect(task.results));
50
- } else {
51
- var name = obj.name;
52
- var args = obj.args;
53
- console.error('%s: %s \n\targs: %s\n', this.event, name, util.inspect(argsNoCb));
44
+ function taskLog(obj) {
45
+ /*jshint validthis: true */
46
+ var time = new Date();
47
+ time.setTime(obj.time);
48
+ var argsNoCb = obj.args.filter(function (a) { return (typeof(a) !== 'function'); });
49
+ var eventTimeStr = time.toISOString();
50
+ if (this.event === 'task.complete') {
51
+ var task = obj;
52
+ console.error('%s: %s:%s \tmsecs: %s \n\targs: %s \n\tresults: %s\n',
53
+ this.event, task.env.name, task.name, task.elapsedTime, util.inspect(argsNoCb), util.inspect(task.results));
54
+ } else {
55
+ var name = obj.name;
56
+ var args = obj.args;
57
+ console.error('%s: %s:%s \n\targs: %s\n', this.event, obj.env.name, obj.name, util.inspect(argsNoCb));
58
+ }
59
+
54
60
  }
55
-
56
- }
57
61
 
58
- /**
59
- Log flow and task events for a flowFn or all of react.
60
- If called multiple times, remove previous listener (if any) before
61
- adding.
62
-
63
- @example
64
- var react = require('react');
65
- react.logEvents(flowFn, eventWildcard); //log events on flowfn matching wildcard
66
-
67
- @param flowFn Flow function or global react object
68
- @param eventWildcard wildcarded event type, if not provided use flow.* and task.*
62
+ /**
63
+ Log flow and task events for a flowFn or all of react.
64
+ If called multiple times, remove previous listener (if any) before
65
+ adding.
66
+
67
+ @example
68
+ var react = require('react');
69
+ react.logEvents(flowFn, eventWildcard); //log events on flowfn matching wildcard
70
+
71
+ @param flowFn Flow function or global react object
72
+ @param eventWildcard wildcarded event type, if not provided use flow.* and task.*
69
73
  */
70
- function logEvents(flowFn, eventWildcard) {
71
- if (!flowFn) flowFn = react; // use global
72
- if (eventWildcard && eventWildcard !== '*') {
73
- var logFn = (FLOW_RE.test(eventWildcard)) ? flowLog : taskLog;
74
- flowFn.events.removeListener(eventWildcard, logFn);
75
- flowFn.events.on(eventWildcard, logFn);
76
- } else { // none provided, use flow.* and task.*
77
- //output events as tasks start and complete
78
- flowFn.events.removeListener(ALL_FLOW_EVENTS, flowLog);
79
- flowFn.events.on(ALL_FLOW_EVENTS, flowLog);
80
- flowFn.events.removeListener(ALL_TASK_EVENTS, taskLog);
81
- flowFn.events.on(ALL_TASK_EVENTS, taskLog);
74
+ function logEvents(flowFn, eventWildcard) {
75
+ if (!flowFn) throw new Error('flowFn is required');
76
+ if (eventWildcard && eventWildcard !== '*') {
77
+ var logFn = (FLOW_RE.test(eventWildcard)) ? flowLog : taskLog;
78
+ flowFn.events.removeListener(eventWildcard, logFn);
79
+ flowFn.events.on(eventWildcard, logFn);
80
+ } else { // none provided, use flow.* and task.*
81
+ //output events as tasks start and complete
82
+ flowFn.events.removeListener(ALL_FLOW_EVENTS, flowLog);
83
+ flowFn.events.on(ALL_FLOW_EVENTS, flowLog);
84
+ flowFn.events.removeListener(ALL_TASK_EVENTS, taskLog);
85
+ flowFn.events.on(ALL_TASK_EVENTS, taskLog);
86
+ }
82
87
  }
83
- }
84
88
 
85
- module.exports = react;
86
- module.exports.logEvents = logEvents;
89
+ logEventsMod.logEvents = logEvents;
90
+ return logEventsMod;
91
+
92
+ });
package/lib/parse.js CHANGED
@@ -1,32 +1,41 @@
1
1
  'use strict';
2
+ /*global define:true */
2
3
 
3
- var sprintf = require('sprintf').sprintf;
4
-
5
- function splitTrimFilterArgs(commaSepArgs) { //parse 'one, two' into ['one', 'two']
6
- if (!commaSepArgs) return [];
7
- return commaSepArgs.split(',') //split on commas
8
- .map(function (s) { return s.trim(); }) //trim
9
- .filter(function (s) { return (s); }); //filter out empty strings
4
+ if (typeof define !== 'function') {
5
+ var define = require('amdefine')(module);
10
6
  }
11
7
 
12
- /**
13
- @param patternFn regex + fn or splitStr + fn
8
+ define(['./sprintf'], function (sprintf) {
9
+
10
+ function splitTrimFilterArgs(commaSepArgs) { //parse 'one, two' into ['one', 'two']
11
+ if (!commaSepArgs) return [];
12
+ return commaSepArgs.split(',') //split on commas
13
+ .map(function (s) { return s.trim(); }) //trim
14
+ .filter(function (s) { return (s); }); //filter out empty strings
15
+ }
16
+
17
+ /**
18
+ @param patternFn regex + fn or splitStr + fn
14
19
  */
15
- function parseReduce(accum, patternFn) {
16
- if (typeof(accum) !== 'string') return accum; // already matched
17
- var m = (patternFn.regex) ? patternFn.regex.exec(accum) : accum.split(patternFn.splitStr);
18
- if (m) return patternFn.fn(m, accum); // pass in matches and origStr, return result obj
19
- return accum; // no match, return str, will try next matcher
20
- }
20
+ function parseReduce(accum, patternFn) {
21
+ if (typeof(accum) !== 'string') return accum; // already matched
22
+ var m = (patternFn.regex) ? patternFn.regex.exec(accum) : accum.split(patternFn.splitStr);
23
+ if (m) return patternFn.fn(m, accum); // pass in matches and origStr, return result obj
24
+ return accum; // no match, return str, will try next matcher
25
+ }
21
26
 
22
- function parseStr(str, parseMatchers, errStr) {
23
- var result = parseMatchers.reduce(parseReduce, str);
24
- if (typeof(result) !== 'string') { // matched
25
- return result;
26
- } else { // no match
27
- throw new Error(sprintf(errStr, str));
28
- }
29
- }
27
+ function parseStr(str, parseMatchers, errStr) {
28
+ var result = parseMatchers.reduce(parseReduce, str);
29
+ if (typeof(result) !== 'string') { // matched
30
+ return result;
31
+ } else { // no match
32
+ throw new Error(sprintf(errStr, str));
33
+ }
34
+ }
35
+
36
+ return {
37
+ splitTrimFilterArgs: splitTrimFilterArgs,
38
+ parseStr: parseStr
39
+ };
30
40
 
31
- exports.splitTrimFilterArgs = splitTrimFilterArgs;
32
- exports.parseStr = parseStr;
41
+ });