Skip to content

Commit 7c3fd07

Browse files
authored
refactor: replace lodash with small libraries (#507)
`camelcase` and `is-plain-obj` are small, single purpose and dependency free utilities with builtin TS support. closes #428
1 parent 5dec5dc commit 7c3fd07

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"@types/express": "^4.17.3",
5454
"@types/is-glob": "^4.0.1",
5555
"@types/jest": "^26.0.22",
56-
"@types/lodash": "^4.14.168",
5756
"@types/micromatch": "^4.0.1",
5857
"@types/node": "^14.14.37",
5958
"@types/supertest": "^2.0.10",
@@ -79,9 +78,10 @@
7978
},
8079
"dependencies": {
8180
"@types/http-proxy": "^1.17.5",
81+
"camelcase": "^6.2.0",
8282
"http-proxy": "^1.18.1",
8383
"is-glob": "^4.0.1",
84-
"lodash": "^4.17.21",
84+
"is-plain-obj": "^3.0.0",
8585
"micromatch": "^4.0.2"
8686
},
8787
"engines": {

src/config-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'lodash';
1+
import isPlainObj = require('is-plain-obj');
22
import * as url from 'url';
33
import { ERRORS } from './errors';
44
import { getInstance } from './logger';
@@ -74,7 +74,7 @@ function isStringShortHand(context: Filter) {
7474
* @return {Boolean} [description]
7575
*/
7676
function isContextless(context: Filter, opts: Options) {
77-
return _.isPlainObject(context) && (opts == null || Object.keys(opts).length === 0);
77+
return isPlainObj(context) && (opts == null || Object.keys(opts).length === 0);
7878
}
7979

8080
function configureLogger(options: Options) {

src/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'lodash';
1+
import camelcase = require('camelcase');
22
import { getInstance } from './logger';
33
const logger = getInstance();
44

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

2727
if (typeof fnHandler === 'function') {

src/path-rewriter.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'lodash';
1+
import isPlainObj = require('is-plain-obj');
22
import { ERRORS } from './errors';
33
import { getInstance } from './logger';
44
const logger = getInstance();
@@ -42,13 +42,9 @@ export function createPathRewriter(rewriteConfig) {
4242
function isValidRewriteConfig(rewriteConfig) {
4343
if (typeof rewriteConfig === 'function') {
4444
return true;
45-
} else if (_.isPlainObject(rewriteConfig) && Object.keys(rewriteConfig).length !== 0) {
46-
return true;
47-
} else if (
48-
rewriteConfig === undefined ||
49-
rewriteConfig === null ||
50-
_.isEqual(rewriteConfig, {})
51-
) {
45+
} else if (isPlainObj(rewriteConfig)) {
46+
return Object.keys(rewriteConfig).length !== 0;
47+
} else if (rewriteConfig === undefined || rewriteConfig === null) {
5248
return false;
5349
} else {
5450
throw new Error(ERRORS.ERR_PATH_REWRITER_CONFIG);
@@ -58,7 +54,7 @@ function isValidRewriteConfig(rewriteConfig) {
5854
function parsePathRewriteRules(rewriteConfig) {
5955
const rules = [];
6056

61-
if (_.isPlainObject(rewriteConfig)) {
57+
if (isPlainObj(rewriteConfig)) {
6258
for (const [key] of Object.entries(rewriteConfig)) {
6359
rules.push({
6460
regex: new RegExp(key),

src/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import * as _ from 'lodash';
1+
import isPlainObj = require('is-plain-obj');
22
import { getInstance } from './logger';
33
const logger = getInstance();
44

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

9-
if (_.isPlainObject(router)) {
9+
if (isPlainObj(router)) {
1010
newTarget = getTargetFromProxyTable(req, router);
1111
} else if (typeof router === 'function') {
1212
newTarget = await router(req);

yarn.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,6 @@
902902
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd"
903903
integrity sha512-7+2BITlgjgDhH0vvwZU/HZJVyk+2XUlvxXe8dFMedNX/aMkaOq++rMAFXc0tM7ij15QaWlbdQASBR9dihi+bDQ==
904904

905-
"@types/lodash@^4.14.168":
906-
version "4.14.168"
907-
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
908-
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==
909-
910905
"@types/micromatch@^4.0.1":
911906
version "4.0.1"
912907
resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.1.tgz#9381449dd659fc3823fd2a4190ceacc985083bc7"
@@ -1705,6 +1700,11 @@ camelcase@^6.0.0:
17051700
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.0.0.tgz#5259f7c30e35e278f1bdc2a4d91230b37cad981e"
17061701
integrity sha512-8KMDF1Vz2gzOq54ONPJS65IvTUaB1cHJ2DMM7MbPmLZljDH1qpzzLsWdiN9pHh6qvkRVDTi/07+eNGch/oLU4w==
17071702

1703+
camelcase@^6.2.0:
1704+
version "6.2.0"
1705+
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809"
1706+
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==
1707+
17081708
capture-exit@^2.0.0:
17091709
version "2.0.0"
17101710
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
@@ -3617,6 +3617,11 @@ is-plain-obj@^1.0.0, is-plain-obj@^1.1.0:
36173617
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
36183618
integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4=
36193619

3620+
is-plain-obj@^3.0.0:
3621+
version "3.0.0"
3622+
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
3623+
integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==
3624+
36203625
is-plain-object@^2.0.3, is-plain-object@^2.0.4:
36213626
version "2.0.4"
36223627
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"

0 commit comments

Comments
 (0)