Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 994b72b

Browse files
author
Michal Vlasák
committed
Simplify utils, use more lodash packages
1 parent 95f0c01 commit 994b72b

5 files changed

Lines changed: 29 additions & 30 deletions

File tree

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
"node": "8.6.0"
1919
},
2020
"dependencies": {
21+
"lodash.difference": "^4.5.0",
2122
"lodash.foreach": "^4.5.0",
2223
"lodash.isempty": "^4.4.0",
2324
"lodash.isobject": "^3.0.2",
2425
"lodash.isstring": "^4.0.1",
26+
"lodash.pick": "^4.4.0",
2527
"omit-deep": "^0.3.0",
2628
"on-finished": "^2.3.0",
2729
"on-headers": "^1.0.1",

serializers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const forEach = require('lodash.foreach');
2-
const { pick, removeEmpty, deleteKeys } = require('./utils');
2+
const pick = require('lodash.pick');
3+
const { removeEmpty, shallowOmit } = require('./utils');
34

45
const serializers = {
56
error(obj) {
@@ -60,8 +61,7 @@ const disablePaths = paths => {
6061

6162
if (affectedFields.length > 0) {
6263
const newSerializer = obj => {
63-
const objCopy = JSON.parse(JSON.stringify(obj)); // we will loose info about functions being passed to logger, but that's a really specific use case, so probably OK
64-
return deleteKeys(value(objCopy), affectedFields);
64+
return shallowOmit(value(obj), affectedFields);
6565
};
6666
serializers[key] = newSerializer;
6767
}

tests/serializers.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { pick } = require('../utils');
1+
const pick = require('lodash.pick');
22
const omitDeep = require('omit-deep');
33
const stream = require('stream');
44

utils.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
const isEmpty = require('lodash.isempty');
2+
const difference = require('lodash.difference');
3+
const pick = require('lodash.pick');
24

3-
const pick = (object, paths) => {
4-
const obj = {};
5-
for (const path of paths) {
6-
if (object[path]) {
7-
obj[path] = object[path];
8-
}
9-
}
10-
return obj;
11-
};
5+
// left here for future
6+
/* const shallowOmit = (obj, omitKeys) => {
7+
return difference(Object.keys(obj), omitKeys).reduce((omitted, key) => {
8+
omitted[key] = obj[key];
9+
return omitted;
10+
}, {});
11+
};*/
1212

13-
const removeEmpty = obj => {
14-
Object.keys(obj).forEach(key => {
15-
if (obj[key] === undefined || isEmpty(obj[key])) {
16-
delete obj[key];
17-
}
18-
});
19-
return obj;
20-
};
13+
const shallowOmit = (obj, omitKeys) => pick(obj, difference(Object.keys(obj), omitKeys));
2114

22-
const deleteKeys = (obj, keys) => {
23-
keys.forEach(key => {
24-
if (obj[key]) {
25-
delete obj[key];
26-
}
27-
});
28-
return obj;
29-
};
15+
const removeEmpty = obj =>
16+
shallowOmit(obj, Object.keys(obj).filter(key => obj[key] === undefined || isEmpty(obj[key])));
3017

31-
module.exports = { pick, removeEmpty, deleteKeys };
18+
module.exports = { shallowOmit, removeEmpty };

0 commit comments

Comments
 (0)