Skip to content

Commit ffb73e7

Browse files
authored
Merge pull request #27 from dynamiccast/upgrade-lodash
Upgrade to lodash 4
2 parents f68e69d + 8b6116e commit ffb73e7

File tree

5 files changed

+26
-54
lines changed

5 files changed

+26
-54
lines changed

lib/api/blueprints/_util/actionUtil.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
var omit = require('lodash/object/omit');
2-
var isObject = require('lodash/lang/isObject');
3-
var util = require('util');
4-
51
/**
62
* Utility methods used in built-in blueprint actions.
73
*
@@ -14,51 +10,34 @@ module.exports = {
1410
* @param {Request} req
1511
* @return {WLCollection}
1612
*/
17-
parseModel: function ( req ) {
13+
parseModel: function(req) {
1814

1915
// Ensure a model can be deduced from the request options.
2016
var model = req.options.model || req.options.controller;
21-
if ( !model ) throw new Error( util.format( 'No "model" specified in route options.' ) );
17+
if (!model) {
18+
throw new Error('No "model" specified in route options.');
19+
}
2220

23-
var Model = req._sails.models[ model ];
24-
if ( !Model ) throw new Error( util.format( 'Invalid route option, "model".\nI don\'t know about any models named: `%s`', model ) );
21+
var Model = req._sails.models[model];
22+
if (!Model) {
23+
throw new Error('Invalid route option, "model". No model named: ' + model);
24+
}
2525

2626
return Model;
2727
},
2828

29-
/**
29+
/**
3030
* Parse `values` for a Waterline `create` or `update` from all
3131
* request parameters.
3232
*
3333
* @param {Request} req
3434
* @return {Object}
3535
*/
36-
parseValues: function ( req, model ) {
37-
// Create data object (monolithic combination of all parameters)
38-
// Omit the blacklisted params (like JSONP callback param, etc.)
39-
40-
// Allow customizable blacklist for params NOT to include as values.
41-
req.options.values = req.options.values || {};
42-
req.options.values.blacklist = req.options.values.blacklist;
36+
parseValues: function(req, model) {
4337

44-
// Validate blacklist to provide a more helpful error msg.
45-
var blacklist = req.options.values.blacklist;
46-
if ( blacklist && !isArray( blacklist ) ) {
47-
throw new Error( 'Invalid `req.options.values.blacklist`. Should be an array of strings (parameter names.)' );
48-
}
49-
50-
// Get values using the model identity as resource identifier
51-
var values = req.body.data.attributes || {};
38+
var values = req.body.data.attributes || {};
5239
values.id = req.allParams()['id'];
5340

54-
// Omit built-in runtime config (like query modifiers)
55-
values = omit( values, blacklist || [] );
56-
57-
// Omit any params w/ undefined values
58-
values = omit( values, function ( p ) {
59-
if ( _.isUndefined( p ) ) return true;
60-
});
61-
6241
return values;
6342
},
6443

@@ -69,7 +48,7 @@ module.exports = {
6948
* @param {Request} req
7049
* @return {Integer|String}
7150
*/
72-
parsePk: function ( req ) {
51+
parsePk: function(req) {
7352

7453
return req.options.id || ( req.options.where && req.options.where.id ) || req.allParams()['id'];
7554
},
@@ -81,11 +60,12 @@ module.exports = {
8160
* @param {Request} req
8261
* @return {Integer|String}
8362
*/
84-
requirePk: function ( req ) {
85-
var pk = module.exports.parsePk( req );
63+
requirePk: function(req) {
64+
65+
var pk = module.exports.parsePk(req);
8666

8767
// Validate the required `id` parameter
88-
if ( !pk ) {
68+
if (!pk) {
8969

9070
var err = new Error(
9171
'No `id` parameter provided.' +

lib/api/blueprints/destroy.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
22
* Module dependencies
33
*/
4-
var util = require( 'util' ),
5-
actionUtil = require( './_util/actionUtil' );
4+
const actionUtil = require( './_util/actionUtil' );
65

76
/**
87
* Destroy One Record

lib/api/blueprints/update.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
/**
2-
* Module dependencies
3-
*/
4-
5-
var actionUtil = require( './_util/actionUtil' );
6-
var util = require( 'util' );
1+
const actionUtil = require( './_util/actionUtil' );
72

83
/**
94
* Update One Record
@@ -39,9 +34,7 @@ module.exports = function updateOneRecord(req, res) {
3934
// returns an array, just use the first item. If more than one
4035
// record was returned, something is amiss.
4136
if ( !records || !records.length || records.length > 1 ) {
42-
req._sails.log.warn(
43-
util.format( 'Unexpected output from `%s.update`.', Model.globalId )
44-
);
37+
req._sails.log.warn('Unexpected output from `' + Model.globalId + '.update`.');
4538
}
4639

4740
var updatedRecord = records[0];

lib/hook.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ module.exports = function(sails) {
286286
// -> or implicitly by globalId
287287
// -> or implicitly by controller id
288288
var routeConfig = sails.router.explicitRoutes[controllerId] || {};
289-
var modelFromGlobalId = _.findWhere(sails.models, {globalId: globalId});
289+
var modelFromGlobalId = _.find(sails.models, {globalId: globalId});
290290
var modelId = config.model || routeConfig.model || (modelFromGlobalId && modelFromGlobalId.identity) || controllerId;
291291

292292
// If the orm hook is enabled, it has already been loaded by this time,
@@ -342,7 +342,7 @@ module.exports = function(sails) {
342342
_bindRoute(_getRoute('%s/destroy/:id?'), 'destroy');
343343

344344
// Bind add/remove "shortcuts" for each `collection` associations
345-
_(Model.associations).where({type: 'collection'}).forEach(function (association) {
345+
_(Model.associations).filter({type: 'collection'}).forEach(function (association) {
346346
var alias = association.alias;
347347
var _getAssocRoute = _.partialRight(util.format, baseRoute, alias);
348348
var opts = _.merge({ alias: alias }, routeOpts);
@@ -366,15 +366,15 @@ module.exports = function(sails) {
366366

367367
// Bind "rest" blueprint/shadow routes based on known associations in our model's schema
368368
// Bind add/remove for each `collection` associations
369-
_(Model.associations).where({type: 'collection'}).forEach(function (association) {
369+
_(Model.associations).filter({type: 'collection'}).forEach(function (association) {
370370
var alias = association.alias;
371371
var _getAssocRoute = _.partialRight(util.format, baseRestRoute, alias);
372372
var opts = _.merge({ alias: alias }, routeOpts);
373373
sails.log.silly('Binding RESTful association blueprint `'+alias+'` for',controllerId);
374374

375375
_bindRoute( _getAssocRoute('post %s/:parentid/%s/:id?'), 'add', opts );
376376
_bindRoute( _getAssocRoute('delete %s/:parentid/%s/:id?'), 'remove', opts );
377-
}).value();
377+
});
378378

379379
// and populate for both `collection` and `model` associations
380380
_(Model.associations).forEach(function (association) {
@@ -384,7 +384,7 @@ module.exports = function(sails) {
384384
sails.log.silly('Binding RESTful association blueprint `'+alias+'` for',controllerId);
385385

386386
_bindRoute( _getAssocRoute('get %s/:parentid/%s/:id?'), 'populate', opts );
387-
}).value();
387+
});
388388
}
389389
}
390390
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Blueprints to turn a Sails.js API into a JSON API",
55
"main": "index.js",
66
"scripts": {
7-
"test": "npm run-script test-jsonapi ; npm run-script test-unit",
7+
"test": "npm run-script test-unit && npm run-script test-jsonapi",
88
"test-jsonapi": "cd tests/dummy ; npm test",
99
"test-unit": "mocha tests/unit/**/*.test.js"
1010
},
@@ -27,7 +27,7 @@
2727
"dependencies": {
2828
"json-api-serializer": "1.1.0",
2929
"jsonapi-validator": "^2.0.0",
30-
"lodash": "3.10.1",
30+
"lodash": "^4.13.1",
3131
"pluralize": "^2.0.0",
3232
"util": "^0.10.3"
3333
},

0 commit comments

Comments
 (0)