dat-middleware
Advanced tools
Comparing version 1.10.2 to 1.10.3
@@ -7,2 +7,4 @@ var Boom = require('boom'); | ||
var isFunction = require('101/is-function'); | ||
var isString = require('101/is-string'); | ||
var isEmpty = require('101/is-empty'); | ||
var keypather = require('keypather')(); | ||
@@ -13,2 +15,5 @@ var pick = require('101/pick'); | ||
var unset = require('./utils/unset'); | ||
var emptyString = function (thing) { | ||
return isString(thing) && isEmpty(thing); | ||
}; | ||
var notEquals = function (compare) { | ||
@@ -358,5 +363,7 @@ return function (val) { | ||
else { | ||
var upsertVal = (isFunction(formatFunction)) ? | ||
formatFunction(keypather.get(req, value) || value) : | ||
(keypather.get(req, value) || value); | ||
var val = (emptyString(value) || !isString(value)) ? | ||
value : (keypather.get(req, value) || value); // keypather edgecase | ||
// var val = keypather.get(req, value) || value; | ||
var upsertVal = isFunction(formatFunction) ? | ||
formatFunction(val) : val; | ||
keypather.set(data, key, upsertVal); | ||
@@ -363,0 +370,0 @@ } |
{ | ||
"name": "dat-middleware", | ||
"version": "1.10.2", | ||
"version": "1.10.3", | ||
"description": "common request, response, body, query, and param validation, transformation, and flow control middleware", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,4 +6,6 @@ var clone = require('101/clone'); | ||
var isFunction = require('101/is-function'); | ||
var isString = require('101/is-string'); | ||
var mw = require('../index'); | ||
var request = require('./lib/superdupertest'); | ||
var keypather = require('keypather')(); | ||
var values = function (obj) { | ||
@@ -14,4 +16,4 @@ return Object.keys(obj).map(function (key) { | ||
}; | ||
var extend = function (obj, key, value) { | ||
var extend; | ||
var extend = function (obj, key, value, dataType) { | ||
var extend, keypath; | ||
if (typeof key === 'object') { | ||
@@ -22,6 +24,9 @@ extend = key; | ||
extend = {}; | ||
extend[key] = value; | ||
keypath = value.replace && value.replace(dataType+'.', ''); | ||
extend[key] = isString(value) && keypath ? keypather.get(obj, keypath) || value : value; | ||
} | ||
Object.keys(extend).forEach(function (key) { | ||
obj[key] = extend[key]; | ||
var value = extend[key]; | ||
keypath = value.replace && value.replace(dataType+'.', ''); | ||
obj[key] = isString(value) && keypath ? keypather.get(obj, keypath) || value : value; | ||
}); | ||
@@ -41,2 +46,4 @@ return obj; | ||
describe('mw.body().set(key, value)', setKey('body', 'key2', true)); | ||
describe('mw.body().set(key, value)', setKey('body', 'key2', 'body.key1')); | ||
describe('mw.body().set(key, value)', setKey('body', 'key2', '')); | ||
describe('mw.query().set(key, value)', setKey('query', 'key2', true)); | ||
@@ -48,2 +55,26 @@ describe('mw.params().set(key, value)', setKey('params', 'key2', true)); | ||
describe('mw.params().set(obj)', setKeys('params', {key2: true, key3: true})); | ||
describe('set empty-string', function() { | ||
before(function () { | ||
this.key = 'key1'; | ||
this.app = createAppWithMiddleware( | ||
mw.body('body').require() | ||
.else( | ||
mw.body().set('body', '')) | ||
); | ||
}); | ||
it('should set empty string', function(done) { | ||
request(this.app) | ||
.post('/body') | ||
.send({ foo: 'bar' }) | ||
.expect(200) | ||
.expect(function (res) { | ||
res.body.should.eql({ | ||
foo: 'bar', | ||
body: '' | ||
}); | ||
}) | ||
.end(done); | ||
}); | ||
}); | ||
}); | ||
@@ -55,3 +86,4 @@ | ||
this.key = 'key1'; | ||
this.app = createAppWithMiddleware(mw[dataType]().set(key, value, formatFunction)); | ||
this.app = createAppWithMiddleware( | ||
mw[dataType]().set(key, value, formatFunction)); | ||
}); | ||
@@ -61,8 +93,5 @@ it('should set key', function (done) { | ||
data[this.key] = 'value'; | ||
if (isFunction(formatFunction)) { | ||
var expected = extend(clone(data), key, formatFunction(value)); | ||
} | ||
else { | ||
var expected = extend(clone(data), key, value); | ||
} | ||
var expected = isFunction(formatFunction) ? | ||
extend(clone(data), key, formatFunction(value), dataType) : | ||
extend(clone(data), key, value, dataType); | ||
var body = dataType === 'body' ? data : {}; | ||
@@ -69,0 +98,0 @@ var query = dataType === 'query' ? data : {}; |
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
81757
2253