Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace lodash with small libraries #507

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@types/express": "^4.17.3",
"@types/is-glob": "^4.0.1",
"@types/jest": "^26.0.22",
"@types/lodash": "^4.14.168",
"@types/micromatch": "^4.0.1",
"@types/node": "^14.14.37",
"@types/supertest": "^2.0.10",
Expand All @@ -79,9 +78,10 @@
},
"dependencies": {
"@types/http-proxy": "^1.17.5",
"camelcase": "^6.2.0",
"http-proxy": "^1.18.1",
"is-glob": "^4.0.1",
"lodash": "^4.17.21",
"is-plain-obj": "^3.0.0",
"micromatch": "^4.0.2"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions src/config-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import isPlainObj = require('is-plain-obj');
import * as url from 'url';
import { ERRORS } from './errors';
import { getInstance } from './logger';
Expand Down Expand Up @@ -74,7 +74,7 @@ function isStringShortHand(context: Filter) {
* @return {Boolean} [description]
*/
function isContextless(context: Filter, opts: Options) {
return _.isPlainObject(context) && (opts == null || Object.keys(opts).length === 0);
return isPlainObj(context) && (opts == null || Object.keys(opts).length === 0);
}

function configureLogger(options: Options) {
Expand Down
4 changes: 2 additions & 2 deletions src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import camelcase = require('camelcase');
import { getInstance } from './logger';
const logger = getInstance();

Expand All @@ -21,7 +21,7 @@ export function getHandlers(options) {
// all handlers for the http-proxy events are prefixed with 'on'.
// loop through options and try to find these handlers
// and add them to the handlers object for subscription in init().
const eventName = _.camelCase('on ' + event);
const eventName = camelcase('on ' + event);
const fnHandler = options ? options[eventName] : null;

if (typeof fnHandler === 'function') {
Expand Down
14 changes: 5 additions & 9 deletions src/path-rewriter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import isPlainObj = require('is-plain-obj');
import { ERRORS } from './errors';
import { getInstance } from './logger';
const logger = getInstance();
Expand Down Expand Up @@ -42,13 +42,9 @@ export function createPathRewriter(rewriteConfig) {
function isValidRewriteConfig(rewriteConfig) {
if (typeof rewriteConfig === 'function') {
return true;
} else if (_.isPlainObject(rewriteConfig) && Object.keys(rewriteConfig).length !== 0) {
return true;
} else if (
rewriteConfig === undefined ||
rewriteConfig === null ||
_.isEqual(rewriteConfig, {})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.isEqual(rewriteConfig, {}) check is now missing in the refactored code. Think the vanilla version would be: JSON.stringify(rewriteConfig) === '{}'.

Copy link
Contributor Author

@TrySound TrySound Mar 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing to empty object means it should be plain one, right? There is plain object check above. I moved Object.keys length check to return value instead. So isEqual check is redundant.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. Missed that part. 👌

) {
} else if (isPlainObj(rewriteConfig)) {
return Object.keys(rewriteConfig).length !== 0;
} else if (rewriteConfig === undefined || rewriteConfig === null) {
return false;
} else {
throw new Error(ERRORS.ERR_PATH_REWRITER_CONFIG);
Expand All @@ -58,7 +54,7 @@ function isValidRewriteConfig(rewriteConfig) {
function parsePathRewriteRules(rewriteConfig) {
const rules = [];

if (_.isPlainObject(rewriteConfig)) {
if (isPlainObj(rewriteConfig)) {
for (const [key] of Object.entries(rewriteConfig)) {
rules.push({
regex: new RegExp(key),
Expand Down
4 changes: 2 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as _ from 'lodash';
import isPlainObj = require('is-plain-obj');
import { getInstance } from './logger';
const logger = getInstance();

export async function getTarget(req, config) {
let newTarget;
const router = config.router;

if (_.isPlainObject(router)) {
if (isPlainObj(router)) {
newTarget = getTargetFromProxyTable(req, router);
} else if (typeof router === 'function') {
newTarget = await router(req);
Expand Down
15 changes: 10 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -902,11 +902,6 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd"
integrity sha512-7+2BITlgjgDhH0vvwZU/HZJVyk+2XUlvxXe8dFMedNX/aMkaOq++rMAFXc0tM7ij15QaWlbdQASBR9dihi+bDQ==

"@types/lodash@^4.14.168":
version "4.14.168"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==

"@types/micromatch@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.1.tgz#9381449dd659fc3823fd2a4190ceacc985083bc7"
Expand Down Expand Up @@ -1705,6 +1700,11 @@ camelcase@^6.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.0.0.tgz#5259f7c30e35e278f1bdc2a4d91230b37cad981e"
integrity sha512-8KMDF1Vz2gzOq54ONPJS65IvTUaB1cHJ2DMM7MbPmLZljDH1qpzzLsWdiN9pHh6qvkRVDTi/07+eNGch/oLU4w==

camelcase@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809"
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==

capture-exit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
Expand Down Expand Up @@ -3617,6 +3617,11 @@ is-plain-obj@^1.0.0, is-plain-obj@^1.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4=

is-plain-obj@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==

is-plain-object@^2.0.3, is-plain-object@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
Expand Down