dat-middleware
Advanced tools
Comparing version 1.0.2 to 1.1.0
var _ = require('lodash'); | ||
var error = require('./error'); | ||
var utils = require('map-utils'); | ||
var pick = utils.pick; | ||
var set = utils.set; | ||
var exists = utils.exists; | ||
var flow = require('middleware-flow'); | ||
@@ -45,3 +42,3 @@ var keypather = require('keypather')(); | ||
RequestData.prototype.keypath = function (key) { | ||
return [this.dataType, key].filter(exists).join('.'); | ||
return [this.dataType, key].filter(utils.exists).join('.'); | ||
}; | ||
@@ -246,3 +243,3 @@ /** | ||
if (!transformation) { | ||
throw new Error('transform requires a transformation fn'); | ||
throw new Error('mapValues requires a transformation fn'); | ||
} | ||
@@ -256,12 +253,13 @@ this.steps.push({ | ||
/** | ||
* pick keys from data object | ||
* @param {...key} string - keys on the request data to validate. | ||
* transform data object with function | ||
* @method function | ||
* @param {function} transformation - function to transform value of key with. | ||
*/ | ||
RequestData.prototype.pick = function () { | ||
if (arguments.length) { | ||
console.log('dat-middleware pick does not take arguments.'); | ||
RequestData.prototype.transform = function (transformation) { | ||
if (!transformation) { | ||
throw new Error('transform requires a transformation fn'); | ||
} | ||
this.steps.push({ | ||
type: 'transformObject', | ||
transformation: pick.apply(null, this._keys) | ||
transformation: transformation | ||
}); | ||
@@ -271,16 +269,29 @@ return this; | ||
/** | ||
* transform pick keys | ||
* @param key string - key to set value for or key-value set object | ||
* @param value string - value to set for key | ||
* pick keys from data object | ||
* @param {...key} string - keys on the request data to validate. | ||
*/ | ||
RequestData.prototype.set = function (key, value) { | ||
RequestData.prototype.pick = function () { | ||
if (arguments.length) { | ||
console.log('dat-middleware pick does not take arguments.'); | ||
} | ||
this.steps.push({ | ||
type: 'transformObject', | ||
transformation: set(key, value) | ||
}); | ||
return this; | ||
return this.transform(utils.pick.apply(null, this._keys)); | ||
}; | ||
/** | ||
* set keys or extend with obj | ||
* @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) { | ||
return this.transform(utils.set(key, value)); | ||
}; | ||
/** | ||
* delete keys from data object | ||
* @param {...key} string - keys on the request data to delete. | ||
*/ | ||
RequestData.prototype.unset = function (/* ...keys */) { | ||
var keys = Array.prototype.slice.call(arguments); | ||
return this.transform(utils.unset.apply(null, keys)); | ||
}; | ||
@@ -287,0 +298,0 @@ /*************** CONDITIONALS ***************/ |
{ | ||
"name": "dat-middleware", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "common request, response, body, query, and param validation, transformation, and flow control middleware", | ||
@@ -38,5 +38,5 @@ "main": "index.js", | ||
"fn-object": "^0.2.2", | ||
"map-utils": "^0.1.1", | ||
"keypather": "^1.1.0", | ||
"middleware-flow": "^0.2.0" | ||
"middleware-flow": "^0.2.0", | ||
"map-utils": "^0.2.0" | ||
}, | ||
@@ -43,0 +43,0 @@ "devDependencies": { |
@@ -182,6 +182,18 @@ # 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 } | ||
app.use(mw.body('key1').set('key', 'value')); | ||
app.use(mw.body('key1').set(obj)); | ||
app.use(mw.body().set('key', 'value')); | ||
app.use(mw.body().set(obj)); | ||
``` | ||
## unset() | ||
deletes the keys on the data type. | ||
```js | ||
var mw = require('dat-middleware'); | ||
var app = require('express')(); | ||
// a body of { key1: true, key2: true, key3:true } becomes { key3:true } | ||
app.use(mw.body().unset('key1', 'key2')); | ||
``` | ||
# Conditionals (flow control): | ||
@@ -188,0 +200,0 @@ |
@@ -56,3 +56,3 @@ var createAppWithMiddleware = require('./fixtures/createAppWithMiddleware'); | ||
}); | ||
it('should pick key', function (done) { | ||
it('should pick keys', function (done) { | ||
var data = {}; | ||
@@ -59,0 +59,0 @@ data[this.keys[0]] = 'value'; |
@@ -28,8 +28,8 @@ var createAppWithMiddleware = require('./fixtures/createAppWithMiddleware'); | ||
describe('set', function () { | ||
describe('mw.body(key).set(key, value)', setKey('body', 'key2', true)); | ||
describe('mw.body(key).set(key, value)', setKey('query', 'key2', true)); | ||
describe('mw.body(key).set(key, value)', setKey('params', 'key2', true)); | ||
describe('mw.body(key).set(obj)', setKeys('body', {key2: true, key3: true})); | ||
describe('mw.body(key).set(obj)', setKeys('query', {key2: true, key3: true})); | ||
describe('mw.body(key).set(obj)', setKeys('params', {key2: true, key3: true})); | ||
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})); | ||
describe('mw.query().set(obj)', setKeys('query', {key2: true, key3: true})); | ||
describe('mw.params().set(obj)', setKeys('params', {key2: true, key3: true})); | ||
}); | ||
@@ -41,3 +41,3 @@ | ||
this.key = 'key1'; | ||
this.app = createAppWithMiddleware(mw[dataType](this.key).set(key, value)); | ||
this.app = createAppWithMiddleware(mw[dataType]().set(key, value)); | ||
}); | ||
@@ -44,0 +44,0 @@ it('should set key', function (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
48500
24
1254
285
+ Addedmap-utils@0.2.0(transitive)
- Removedmap-utils@0.1.2(transitive)
Updatedmap-utils@^0.2.0