Skip to content

Commit

Permalink
perf: replace lodash.omit with native alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
bravo-kernel committed Nov 1, 2019
1 parent 762433d commit 34a8c06
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/schema-manager.js
@@ -1,8 +1,7 @@
const _assign = require('lodash.assign');
const _merge = require('lodash.merge');
const _omit = require('lodash.omit');
const plural = require('pluralize');
const { capitalize, pick } = require('./utils/lodash-natives');
const { capitalize, omit, pick } = require('./utils/lodash-natives');

const StrategyInterface = require('./strategy-interface');
const TypeMapper = require('./type-mapper');
Expand Down Expand Up @@ -267,7 +266,7 @@ class SchemaManager {
}

if (_modelOptions.get(this).exclude.length > 0) {
return _omit(attributes, _modelOptions.get(this).exclude);
return omit(attributes, _modelOptions.get(this).exclude);
}

return attributes;
Expand Down
28 changes: 28 additions & 0 deletions lib/utils/lodash-natives.js
Expand Up @@ -13,6 +13,9 @@
/* eslint-disable no-param-reassign */
/* eslint-disable unicorn/prevent-abbreviations */

// ---------------------------------------------------
// capitalize
// ---------------------------------------------------
const capitalize = function(string) {
if (typeof string !== 'string') {
throw new TypeError("The 'string' argument for _capitalize() must be a string");
Expand All @@ -21,16 +24,41 @@ const capitalize = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};

// ---------------------------------------------------
// omit
// ---------------------------------------------------
const inProps = function(key, props) {
return props.some(omitKey => {
return omitKey === key;
});
};

const omit = function(object, properties) {
const newObject = {};
Object.keys(object).forEach(key => {
if (!inProps(key, properties)) {
newObject[key] = object[key];
}
});

return newObject;
};

// ---------------------------------------------------
// pick
// ---------------------------------------------------
const pick = function(object, keys) {
return keys.reduce((obj, key) => {
if (object && object.hasOwnProperty(key)) {
obj[key] = object[key];
}

return obj;
}, {});
};

module.exports = {
capitalize,
omit,
pick,
};
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -48,7 +48,6 @@
"dependencies": {
"lodash.assign": "^4.2.0",
"lodash.merge": "^4.6.2",
"lodash.omit": "^4.5.0",
"pluralize": "^8.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 34a8c06

Please sign in to comment.