Comparing version 1.1.2 to 1.2.0
@@ -6,3 +6,6 @@ 'use strict'; | ||
, once = require('once') | ||
, Promise = typeof global.Promise == 'function' ? global.Promise : require('es6-promise').Promise | ||
; | ||
function parallel(tasks, fn) { | ||
@@ -96,19 +99,6 @@ var errors = [], counter = 0, limit = Object.keys(tasks).length, results = {}; | ||
Datamodel.prototype.apply = function (input, output, fn) | ||
var apply = function (self, input, output, fn) | ||
{ | ||
var results = {}, errors = []; | ||
if (typeof input === 'function' && output === undefined && fn === undefined) { | ||
fn = input; | ||
input = {}; | ||
output = false; | ||
} | ||
else if (typeof input !== 'function' && typeof output === 'function' && fn === undefined) { | ||
fn = output; | ||
output = false; | ||
} | ||
assert.equal(typeof fn, 'function'); | ||
var self = this; | ||
function mapper(ino, v) { | ||
@@ -179,2 +169,29 @@ var ouo = {}; | ||
} | ||
Datamodel.prototype.apply = function (input, output, fn) | ||
{ | ||
var self = this; | ||
if (typeof input === 'function' && output === undefined && fn === undefined) { | ||
fn = input; | ||
input = {}; | ||
output = false; | ||
} | ||
else if (typeof input !== 'function' && typeof output === 'function' && fn === undefined) { | ||
fn = output; | ||
output = false; | ||
} | ||
if(typeof fn == 'function') { | ||
return apply(self, input, output, fn); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
apply(self, input, output, function(e, r) { | ||
if (e) { | ||
reject(e); | ||
} | ||
else { | ||
resolve(r); | ||
} | ||
}); | ||
}); | ||
} | ||
Datamodel.prototype.attach = function (module) { | ||
@@ -189,3 +206,3 @@ var self = this; | ||
return function(input, output, fn) { | ||
self.apply(input, output, fn); | ||
return self.apply(input, output, fn); | ||
} | ||
@@ -195,4 +212,5 @@ }; | ||
module.exports = function (options) { | ||
return new Datamodel(options); | ||
} |
{ | ||
"name": "datamodel", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"author": "Nicolas Thouvenin <nthouvenin@gmail.com>", | ||
@@ -24,2 +24,3 @@ "contributors": [], | ||
"dependencies": { | ||
"es6-promise": "^2.3.0", | ||
"once": "^1.3.1" | ||
@@ -26,0 +27,0 @@ }, |
@@ -27,5 +27,39 @@ # Datamodel | ||
## Example 1 : Create a datamodel with 3 files and 1 directory | ||
## Example 1 : Create a object with 3 fields | ||
```javascript | ||
var datamodel = require('datamodel'); | ||
datamodel() | ||
.declare('field1', function(input, fill) { | ||
fill(input + ' hello'); | ||
}) | ||
.declare('field2', function(input, fill) { | ||
fill(input + ' world'); | ||
}) | ||
.append('field3', function(input, fill) { | ||
fill({ 'field1': this.field1, 'field2': this.field2}); | ||
}) | ||
.apply('->') | ||
.then(function(res) { | ||
console.log('field1', res.field1); | ||
console.log('field2', res.field2); | ||
console.log('field3', res.field3); | ||
}) | ||
.catch(function(err) { | ||
console.error('Error>', err.message); | ||
}); | ||
``` | ||
**output** | ||
``` | ||
field1 -> hello | ||
field2 -> world | ||
field3 { field1: '-> hello', field2: '-> world' } | ||
``` | ||
## Example 2 : Create a object with the content of 3 files | ||
```javascript | ||
var fs = require('fs') | ||
@@ -53,5 +87,8 @@ , datamodel = require('datamodel'); | ||
}) | ||
.apply(function(err, res) { | ||
console.error('Error>', err); | ||
.apply() | ||
.then(function(res) { | ||
console.log('Result>', res); | ||
}) | ||
.catch(function(err) { | ||
console.error('Error>', err.message); | ||
}); | ||
@@ -62,10 +99,7 @@ ``` | ||
``` | ||
Error> { [Error: Some fields are errors.] fields: [ 'file3' ] } | ||
Result> { dirname: '/etc', | ||
file1: 'root:x:...', | ||
file2: '127.0.0...', | ||
file3: { [Error: ENOENT, open '/etc/unknown'] errno: 34, code: 'ENOENT', path: '/etc/unknown' } } | ||
Error> ENOENT, open '/etc/unknown' | ||
``` | ||
## Example 2 : Declare a datamodel as package/module | ||
## Example 3 : Declare a datamodel as package/module | ||
@@ -98,11 +132,13 @@ **obj.js** | ||
```javascript | ||
var util = require('util'); | ||
var mdl = require('./obj.js'); | ||
require('./obj.js')('#', function(o) { | ||
console.log(util.inspect(o, { colors:true, depth: null })); | ||
} | ||
); | ||
mdl('#') | ||
.then(function(o) { | ||
console.log('Result>', o); | ||
}) | ||
.catch(console.error); | ||
``` | ||
**Output** | ||
``` | ||
Result> | ||
{ a: '#A', | ||
@@ -115,3 +151,3 @@ 'b.1': '#B1', | ||
## Example 3 : Combine a datamodel and a route with Express | ||
## Example 4 : Combine a datamodel and a route with Express | ||
@@ -165,3 +201,3 @@ | ||
### send(Function callback) | ||
### apply(Mixed input, Mixed output, Function callback) | ||
### apply(Mixed input, Mixed output, Function callback) -> Promise | ||
### attach(Object module) | ||
@@ -168,0 +204,0 @@ |
@@ -232,2 +232,27 @@ /* global describe, it */ | ||
it('09p - declare & append & complete with errors', function (done) { | ||
datamodel() | ||
.declare('a', function(input, fill) { | ||
fill(new Error('a!')); | ||
}) | ||
.append('b', function(input, fill) { | ||
fill('(' + this.a + ' * b)'); | ||
}) | ||
.complete('c', function(input, fill) { | ||
fill('(' + this.a + ' * ' + this.b + ' * c)'); | ||
}) | ||
.apply() | ||
.catch(function(err) { | ||
assert.equal(err.fields.length, 1); | ||
assert.equal(err.toString(), 'Error: a!'); | ||
done(); | ||
}) | ||
.then(function(res) { | ||
assert(false); | ||
done(); | ||
}) | ||
; | ||
}); | ||
it('09bis - declare & prepend & complete with errors', function (done) { | ||
@@ -449,3 +474,18 @@ datamodel() | ||
}); | ||
it('15p - module', function (done) { | ||
var m = require('./assets/obj.js') | ||
m('$', 'out').then(function(res) { | ||
assert.equal(res.a, '$A'); | ||
assert.equal(res['b.1'], '$B1'); | ||
assert.equal(res['b.2'], '$B2'); | ||
assert.equal(res['b.3'], '$B3'); | ||
assert.equal(res.c, '$C'); | ||
done(); | ||
}).catch(function(e) { | ||
assert(false); | ||
done(); | ||
}); | ||
}); | ||
it('16 - prepend & append', function (done) { | ||
@@ -488,3 +528,29 @@ datamodel() | ||
it('17b - callback called once', function (done) { | ||
datamodel() | ||
.declare('a', function(input, fill) { | ||
fill('(a)'); | ||
fill('(x)'); | ||
}) | ||
.declare('b', function(input, fill) { | ||
fill('(b)'); | ||
fill('(y)'); | ||
}) | ||
.declare('c', function(input, fill) { | ||
fill('(c)'); | ||
fill('(z)'); | ||
}) | ||
.apply().catch(function(e) { | ||
assert(false); | ||
done(); | ||
}).then(function(result) { | ||
assert.equal(result.a, '(a)'); | ||
assert.equal(result.b, '(b)'); | ||
assert.equal(result.c, '(c)'); | ||
done(); | ||
}); | ||
}); | ||
/* */ | ||
}); |
27669
14
799
204
2
2
+ Addedes6-promise@^2.3.0
+ Addedes6-promise@2.3.0(transitive)