dat-middleware
Advanced tools
Comparing version 1.9.3 to 1.9.4
@@ -0,10 +1,12 @@ | ||
var Boom = require('boom'); | ||
var bindRight = require('./bindRight'); | ||
var clone = require('101/clone'); | ||
var error = require('./error'); | ||
var utils = require('map-utils'); | ||
var flow = require('middleware-flow'); | ||
var isFunction = require('101/is-function'); | ||
var keypather = require('keypather')(); | ||
var pick = require('101/pick'); | ||
var Boom = require('boom'); | ||
var clone = require('101/clone'); | ||
var pluck = require('101/pluck'); | ||
var bindRight = require('./bindRight'); | ||
var utils = require('map-utils'); | ||
var unset = require('./utils/unset'); | ||
var notEquals = function (compare) { | ||
@@ -338,6 +340,11 @@ return function (val) { | ||
* @param {value} string - value to set for key | ||
* @param {formatFunction} function - function that accepts the value | ||
* to set as an argument and returns modified value | ||
* or | ||
* @param {key} string - key to set value for or key-value set object | ||
* @param {value} string - value to set for key | ||
* or | ||
* @param {obj} string - key to set value for or key-value set object | ||
*/ | ||
RequestData.prototype.set = function (key, value) { | ||
RequestData.prototype.set = function (key, value, formatFunction) { | ||
return this.transform(function (data, req, err) { | ||
@@ -350,3 +357,6 @@ if (typeof key === 'object') { | ||
else { | ||
keypather.set(data, key, keypather.get(req, value) || value); | ||
var upsertVal = (isFunction(formatFunction)) ? | ||
formatFunction(keypather.get(req, value) || value) : | ||
(keypather.get(req, value) || value); | ||
keypather.set(data, key, upsertVal); | ||
} | ||
@@ -362,3 +372,3 @@ return data; | ||
var keys = Array.prototype.slice.call(arguments); | ||
return this.transform(utils.unset.apply(null, keys)); | ||
return this.transform(unset.apply(null, keys)); | ||
}; | ||
@@ -365,0 +375,0 @@ |
{ | ||
"name": "dat-middleware", | ||
"version": "1.9.3", | ||
"version": "1.9.4", | ||
"description": "common request, response, body, query, and param validation, transformation, and flow control middleware", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -212,5 +212,10 @@ # dat-middleware [![Build Status](https://travis-ci.org/tjmehta/dat-middleware.png?branch=master)](https://travis-ci.org/tjmehta/dat-middleware) | ||
// a body of { key1: true, key2: true, key3:true } becomes { key1: true } | ||
// a body of { foo: 1 } becomes { foo: 1, key: 'value' } | ||
app.use(mw.body().set('key', 'value')); | ||
// a body of { key1: true, key2: true, key3:true } becomes extended by obj | ||
app.use(mw.body().set(obj)); | ||
// a body of { key1: 2 } becomes { key1: 2, key2: 1.4142135623730951 } | ||
app.use(mw.body().set('key2', 'key1', function (val) { return Math.sqrt(val); })) | ||
``` | ||
@@ -291,2 +296,2 @@ | ||
# License | ||
### MIT | ||
### MIT |
@@ -0,7 +1,8 @@ | ||
var clone = require('101/clone'); | ||
var createAppWithMiddleware = require('./fixtures/createAppWithMiddleware'); | ||
var createCounter = require('callback-count'); | ||
var fno = require('fn-object'); | ||
var isFunction = require('101/is-function'); | ||
var mw = require('../index'); | ||
var fno = require('fn-object'); | ||
var createCounter = require('callback-count'); | ||
var request = require('./lib/superdupertest'); | ||
var clone = require('101/clone'); | ||
var values = function (obj) { | ||
@@ -27,6 +28,15 @@ return Object.keys(obj).map(function (key) { | ||
function formatFunction (val) { | ||
return val + '_'; | ||
} | ||
describe('set', function () { | ||
describe('mw.body().set(key, value, formatFunction)', setKey('body', 'key2', 'key2', formatFunction)); | ||
describe('mw.query().set(key, value, formatFunction)', setKey('query', 'key2', 'key2', formatFunction)); | ||
describe('mw.params().set(key, value, formatFunction)', setKey('params', 'key2', 'key2', formatFunction)); | ||
describe('mw.body().set(key, value)', setKey('body', 'key2', true)); | ||
describe('mw.query().set(key, value)', setKey('query', 'key2', true)); | ||
describe('mw.params().set(key, value)', setKey('params', 'key2', true)); | ||
describe('mw.body().set(obj)', setKeys('body', {key2: true, key3: true})); | ||
@@ -37,7 +47,7 @@ describe('mw.query().set(obj)', setKeys('query', {key2: true, key3: true})); | ||
function setKey (dataType, key, value) { | ||
function setKey (dataType, key, value, formatFunction) { | ||
return function () { | ||
before(function () { | ||
this.key = 'key1'; | ||
this.app = createAppWithMiddleware(mw[dataType]().set(key, value)); | ||
this.app = createAppWithMiddleware(mw[dataType]().set(key, value, formatFunction)); | ||
}); | ||
@@ -47,3 +57,8 @@ it('should set key', function (done) { | ||
data[this.key] = 'value'; | ||
var expected = extend(clone(data), key, value); | ||
if (isFunction(formatFunction)) { | ||
var expected = extend(clone(data), key, formatFunction(value)); | ||
} | ||
else { | ||
var expected = extend(clone(data), key, value); | ||
} | ||
var body = dataType === 'body' ? data : {}; | ||
@@ -50,0 +65,0 @@ var query = dataType === 'query' ? data : {}; |
@@ -6,2 +6,3 @@ var createAppWithMiddleware = require('./fixtures/createAppWithMiddleware'); | ||
var request = require('./lib/superdupertest'); | ||
var keypather = require('keypather')(); | ||
var values = function (obj) { | ||
@@ -20,2 +21,3 @@ return Object.keys(obj).map(function (key) { | ||
describe('mw.params().unset(keys..)', unsetKeys('params')); | ||
describe('mw.body().unset(keys..)', unsetKeypath('body')); | ||
}); | ||
@@ -56,3 +58,4 @@ | ||
this.unsetKeys = ['key3', 'key4']; | ||
this.app = createAppWithMiddleware(mw[dataType]().unset(this.unsetKeys[0], this.unsetKeys[1])); | ||
this.app = createAppWithMiddleware( | ||
mw[dataType]().unset(this.unsetKeys[0], this.unsetKeys[1], this.unsetKeys[2])); | ||
}); | ||
@@ -81,2 +84,37 @@ it('should unset keys', function (done) { | ||
}; | ||
} | ||
function unsetKeypath (dataType) { | ||
return function () { | ||
before(function () { | ||
this.keys = ['key1', 'key2']; | ||
this.unsetKeys = ['key3', 'key4', 'key.path']; | ||
this.app = createAppWithMiddleware( | ||
mw[dataType]().unset(this.unsetKeys[0], this.unsetKeys[1], this.unsetKeys[2])); | ||
}); | ||
it('should unset keys', function (done) { | ||
var data = {}; | ||
data[this.keys[0]] = 'value'; | ||
data[this.keys[1]] = 'value'; | ||
data[this.unsetKeys[0]] = 'value'; | ||
data[this.unsetKeys[1]] = 'value'; | ||
keypather.set(data, this.unsetKeys[2], 'value'); | ||
var expected = {}; | ||
expected[this.keys[0]] = 'value'; | ||
expected[this.keys[1]] = 'value'; | ||
var body = dataType === 'body' ? data : {}; | ||
var query = dataType === 'query' ? data : {}; | ||
var params = dataType === 'params' ? values(data) : []; | ||
request(this.app) | ||
.post('/'+dataType, params, query) | ||
.send(body) | ||
.expect(200) | ||
.expect(function (res) { | ||
Object.keys(res.body.key).length.should.equal(0); | ||
delete res.body.key; | ||
res.body.should.eql(expected); | ||
}) | ||
.end(done); | ||
}); | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
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
80401
35
2218
296