Comparing version 0.0.1 to 0.1.1
57
ordu.js
@@ -30,2 +30,3 @@ /* Copyright (c) 2016 Richard Rodger and other contributors, MIT License */ | ||
self.tasknames = api_tasknames | ||
self.taskdetails = api_taskdetails | ||
self.toString = api_toString | ||
@@ -37,3 +38,5 @@ | ||
function api_add (task) { | ||
function api_add (spec, task) { | ||
task = task || spec | ||
Assert('function' === typeof task) | ||
@@ -47,10 +50,36 @@ | ||
task.tags = spec.tags || [] | ||
tasks.push(task) | ||
return self | ||
} | ||
function api_process (ctxt, data) { | ||
// Valid calls: | ||
// * process(spec, ctxt, data) | ||
// * process(ctxt, data) | ||
// * process(data) | ||
// * process() | ||
function api_process () { | ||
var i = arguments.length | ||
var data = 0 < i && arguments[--i] | ||
var ctxt = 0 < i && arguments[--i] | ||
var spec = 0 < i && arguments[--i] | ||
data = data || {} | ||
ctxt = ctxt || {} | ||
spec = spec || {} | ||
spec.tags = spec.tags || [] | ||
for (var tI = 0; tI < tasks.length; ++tI) { | ||
var task = tasks[tI] | ||
if (0 < spec.tags.length && !contains(task.tags, spec.tags)) { | ||
continue | ||
} | ||
var index$ = tI | ||
var taskname$ = tasks[tI].name | ||
var taskname$ = task.name | ||
@@ -60,3 +89,3 @@ ctxt.index$ = index$ | ||
var res = tasks[tI].call(null, ctxt, data) | ||
var res = task(ctxt, data) | ||
@@ -71,2 +100,4 @@ if (res) { | ||
} | ||
return null | ||
} | ||
@@ -82,2 +113,9 @@ | ||
function api_taskdetails () { | ||
return tasks.map(function (v) { | ||
return v.name + ':{tags:' + v.tags + '}' | ||
}) | ||
} | ||
function api_toString () { | ||
@@ -89,1 +127,12 @@ return opts.name + ':[' + self.tasknames() + ']' | ||
} | ||
function contains (all, some) { | ||
for (var i = 0; i < some.length; ++i) { | ||
if (-1 === all.indexOf(some[i])) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
@@ -17,3 +17,3 @@ { | ||
"main": "ordu.js", | ||
"version": "0.0.1", | ||
"version": "0.1.1", | ||
"scripts": { | ||
@@ -30,4 +30,3 @@ "test": "lab -v -P test -L -t 100", | ||
], | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -34,0 +33,0 @@ "code": "3.0.x", |
@@ -9,2 +9,10 @@ # ordu | ||
Task functions are executed in order of addition, and passed a shared | ||
context, and a modifiable data structure. Execution is | ||
synchronous. You can exit early by returning a non-null value from a | ||
task function. | ||
You can tag task functions, and restrict execution to the subset of | ||
task functions with matching tags. | ||
This module is used by the [Seneca](http://senecajs.org) framework to | ||
@@ -14,2 +22,36 @@ provide configurable extension hooks. | ||
### Quick example | ||
```js | ||
var Ordu = require('ordu') | ||
var w = Ordu() | ||
w.add(function first (ctxt, data) { | ||
if (null == data.foo) { | ||
return {kind: 'error', why: 'no foo'} | ||
} | ||
data.foo = data.foo.substring(0, ctxt.len) | ||
}) | ||
w.add({tags: ['upper']}, function second (ctxt, data) { | ||
data.foo = data.foo.toUpperCase() | ||
}) | ||
var ctxt = {len: 3} | ||
var data = {foo: 'green'} | ||
w.process(ctxt, data) | ||
console.log(data.foo) // prints 'GRE' (first, second) | ||
data = {foo: 'blue'} | ||
w.process({tags: ['upper']}, ctxt, data) | ||
console.log(data.foo) // prints 'BLUE' (second) | ||
data = [] | ||
var res = w.process(ctxt, data) | ||
console.log(res) // prints {kind: 'error', why: 'no foo', ... introspection ...} | ||
``` | ||
### Support | ||
@@ -16,0 +58,0 @@ |
6073
88
73