Skip to content

Commit adb80ce

Browse files
authored
Safely mutate properties using lodash/omit (#543)
1 parent c3412da commit adb80ce

15 files changed

+28
-169
lines changed

lib/common/_remove.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/common/delete-by-dot.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/services/de-populate.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1+
const omit = require('lodash/omit');
12

23
const getItems = require('./get-items');
34
const replaceItems = require('./replace-items');
4-
const deleteByDot = require('../common/delete-by-dot');
55

66
module.exports = function (func) {
77
return context => {
88
const items = getItems(context);
9-
10-
(Array.isArray(items) ? items : [items]).forEach(item => {
9+
const converter = item => {
1110
if (typeof func === 'function') {
1211
func(item);
1312
}
1413

15-
removeProps('_computed', item);
16-
removeProps('_include', item);
17-
delete item._elapsed;
18-
});
14+
const keys = ['_elapsed', '_computed', '_include'];
15+
const { _computed = [], _include = [] } = item;
16+
17+
return omit(item, keys.concat(_computed).concat(_include));
18+
};
19+
const converted = Array.isArray(items) ? items.map(converter) : converter(items);
20+
21+
replaceItems(context, converted);
1922

20-
replaceItems(context, items);
2123
return context;
2224
};
2325
};
24-
25-
function removeProps (name, item) {
26-
if (name in item) {
27-
item[name].forEach(key => { deleteByDot(item, key); });
28-
delete item[name];
29-
}
30-
}

lib/services/discard-query.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
const omit = require('lodash/omit');
12

2-
const _remove = require('../common/_remove');
33
const checkContext = require('./check-context');
44

55
module.exports = function (...fieldNames) {
66
return context => {
77
checkContext(context, 'before', null, 'discardQuery');
88

99
const query = (context.params || {}).query || {};
10-
_remove(query, fieldNames);
10+
11+
context.params.query = omit(query, fieldNames);
1112

1213
return context;
1314
};

lib/services/discard.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
const omit = require('lodash/omit');
12

2-
const _remove = require('../common/_remove');
33
const checkContextIf = require('./check-context-if');
44
const getItems = require('./get-items');
5+
const replaceItems = require('./replace-items');
56

67
module.exports = function (...fieldNames) {
78
return context => {
89
checkContextIf(context, 'before', ['create', 'update', 'patch'], 'discard');
910

10-
_remove(getItems(context), fieldNames);
11+
const items = getItems(context);
12+
const convert = item => omit(item, fieldNames);
13+
const converted = Array.isArray(items) ? items.map(convert) : convert(items);
14+
15+
replaceItems(context, converted);
1116

1217
return context;
1318
};

lib/services/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const checkContext = require('./check-context');
88
const checkContextIf = require('./check-context-if');
99
const combine = require('./combine');
1010
const debug = require('./debug');
11-
const deleteByDot = require('../common/delete-by-dot');
1211
const dePopulate = require('./de-populate');
1312
const disablePagination = require('./disable-pagination');
1413
const discard = require('./discard');
@@ -59,7 +58,6 @@ module.exports = Object.assign({
5958
checkContextIf,
6059
combine,
6160
debug,
62-
deleteByDot,
6361
dePopulate,
6462
disablePagination,
6563
disallow,

lib/services/prevent-changes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const existsByDot = require('lodash/has');
2+
const omit = require('lodash/omit');
23

3-
const deleteByDot = require('../common/delete-by-dot');
44
const checkContext = require('./check-context');
55
const errors = require('@feathersjs/errors');
66

@@ -19,9 +19,11 @@ module.exports = function (...fieldNames) {
1919

2020
fieldNames.forEach(name => {
2121
if (existsByDot(data, name)) {
22-
if (ifThrow) throw new errors.BadRequest(`Field ${name} may not be patched. (preventChanges)`);
22+
if (ifThrow) {
23+
throw new errors.BadRequest(`Field ${name} may not be patched. (preventChanges)`);
24+
}
2325
// Delete data.contactPerson.name
24-
deleteByDot(data, name);
26+
context.data = omit(data, name);
2527
}
2628
// Delete data['contactPerson.name']
2729
if (data[name]) {

lib/services/serialize.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const getByDot = require('lodash/get');
22
const setByDot = require('lodash/set');
3+
const omit = require('lodash/omit');
34

45
const getItems = require('./get-items');
56
const replaceItems = require('./replace-items');
6-
const deleteByDot = require('../common/delete-by-dot');
77

88
module.exports = function (schema1) {
99
return context => {
@@ -43,9 +43,7 @@ module.exports = function (schema1) {
4343
let exclude = schema.exclude;
4444
exclude = typeof exclude === 'string' ? [exclude] : exclude;
4545
if (exclude) {
46-
exclude.forEach(key => {
47-
deleteByDot(item, key);
48-
});
46+
item = omit(item, exclude);
4947
}
5048

5149
const _computed = Object.keys(computed);

tests/services/delete-by-dot.test.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

tests/services/discard-1.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('common hook discard', () => {
2020
['after', afterJane(), 'create', null, ['last'], { first: 'Jane'} ],
2121
['call internally on server', afterJane(), 'create', undefined, ['last'], { first: 'Jane'} ],
2222
['not throw field missing', beforeJohn(), 'create', 'rest', ['first', 'xx'], { last: 'Doe' } ],
23-
['not throw field undefined', beforeUndef(), 'create', 'rest', ['first'], { first: undefined, last: 'Doe' } ], // ???
23+
['not throw field undefined', beforeUndef(), 'create', 'rest', ['first'], { last: 'Doe' } ],
2424
['not throw field null', beforeNull(), 'create', 'rest', ['first'], { last: 'Doe' } ],
2525
];
2626
/* eslint-enable */

0 commit comments

Comments
 (0)