Comparing version 0.1.12 to 0.1.13
27
index.js
const debug = require('debug')('jigawatt'); | ||
const { inspect } = require('util'); | ||
const _ = require('ramda'); | ||
@@ -80,2 +83,26 @@ const Bluebird = require('bluebird'); | ||
Middleware.debug = (label) => ({ | ||
transform: (req, data) => { | ||
const data_as_string = inspect(data); | ||
debug(`${label}: ${data_as_string}`); | ||
return data; | ||
} | ||
}); | ||
Middleware.tap = (fn) => ({ | ||
transform: (req, data) => Bluebird.resolve(fn(req, data)).return(data) | ||
}); | ||
Middleware.branch = (predicate, ifTrue, otherwise) => ({ | ||
transform: (req, data) => | ||
Bluebird.resolve(predicate(req, data)) | ||
.then((branch) => run([ branch ? ifTrue : otherwise ], req)) | ||
}); | ||
module.exports = Middleware; |
{ | ||
"name": "jigawatt", | ||
"version": "0.1.12", | ||
"version": "0.1.13", | ||
"description": "Influential's Functional, Promise-based Express Middleware", | ||
@@ -56,4 +56,5 @@ "main": "index.js", | ||
"bluebird": "^3.4.1", | ||
"debug": "^2.2.0", | ||
"ramda": "^0.21.0" | ||
} | ||
} |
@@ -317,2 +317,153 @@ /*eslint-env node, mocha */ | ||
describe('JW::debug', function() { | ||
it('should not affect the data object', function(done) { | ||
const test = JW(JW.debug('foo')); | ||
const req = { | ||
data: { foo: 'bar' } | ||
}; | ||
const res = { | ||
json: (x) => { | ||
expect(req.data).to.eql(x); | ||
done(); | ||
} | ||
}; | ||
test(req, res, done); | ||
}); | ||
}); | ||
describe('JW::tap', function() { | ||
it('should not affect the data object', function(done) { | ||
const foo = (req, data) => { | ||
expect(data.foo).to.eql('bar'); | ||
return 'blah'; | ||
}; | ||
const test = JW(JW.tap(foo)); | ||
const req = { | ||
data: { foo: 'bar' } | ||
}; | ||
const res = { | ||
json: (x) => { | ||
expect(req.data).to.eql(x); | ||
done(); | ||
} | ||
}; | ||
test(req, res, done); | ||
}); | ||
}); | ||
describe('JW::branch', function() { | ||
it('should call the first middleware if the predicate function returns ' + | ||
' a truthy value', function(done) { | ||
const predicate = _.T | ||
const whenTrue = { | ||
transform: (req, data) => { | ||
expect(data.foo).to.eql('bar'); | ||
return data; | ||
} | ||
}; | ||
const whenFalse = { | ||
transform: () => { throw new Error("Wait...what? How?") } | ||
}; | ||
const test = JW(JW.branch(predicate, whenTrue, whenFalse)); | ||
const req = { data: { foo: 'bar' } }; | ||
const res = { json: (x) => { | ||
expect(x).to.eql(req.data); | ||
done(); | ||
}}; | ||
test(req, res, done); | ||
}); | ||
it('should call the second middleware if the predicate function returns ' + | ||
' a falsy value', function(done) { | ||
const predicate = _.F | ||
const whenTrue = { | ||
transform: () => { throw new Error('The result is a lie!') } | ||
}; | ||
const whenFalse = { | ||
transform: (req, data) => { | ||
expect(data.foo).to.eql('baz'); | ||
return data; | ||
} | ||
}; | ||
const test = JW(JW.branch(predicate, whenTrue, whenFalse)); | ||
const req = { data: { foo: 'baz' } }; | ||
const res = { json: (x) => { | ||
expect(x).to.eql(req.data); | ||
done(); | ||
}}; | ||
test(req, res, done); | ||
}); | ||
it('should call the predicate with the req and data objects', | ||
function(done) { | ||
const req = { data: { foo: 'baz' } }; | ||
const res = { json: (x) => { | ||
expect(x).to.eql(req.data); | ||
done(); | ||
}}; | ||
const predicate = (req, data) => { | ||
expect(req).to.eql(req); | ||
expect(data).to.eql(data); | ||
return true; | ||
}; | ||
const whenTrue = { | ||
transform: (req, data) => data | ||
}; | ||
const whenFalse = { | ||
transform: () => { throw new Error('I call shenanigans!') } | ||
}; | ||
const test = JW(JW.branch(predicate, whenTrue, whenFalse)); | ||
test(req, res, done); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20521
457
4
+ Addeddebug@^2.2.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)