Skip to content

Commit

Permalink
Merge pull request #707 from talentlessguy/replace-lodash
Browse files Browse the repository at this point in the history
Replace deps with lighter alternatives
  • Loading branch information
wheresrhys authored Jul 18, 2024
2 parents 265fa2e + eb23636 commit 499d94e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 62 deletions.
49 changes: 22 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"homepage": "http://www.wheresrhys.co.uk/fetch-mock",
"engines": {
"node": ">=4.0.0"
"node": ">=8.0.0"
},
"workspaces": [
"packages/*",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
},
"homepage": "https://github.com/wheresrhys/fetch-mock#readme",
"dependencies": {
"glob-to-regexp": "^0.4.1",
"dequal": "^2.0.3",
"globrex": "^0.1.2",
"is-subset": "^0.1.1",
"lodash.isequal": "^4.5.0",
"path-to-regexp": "^2.4.0",
"querystring": "^0.2.1"
"querystring": "^0.2.1",
"regexparam": "^3.0.0"
}
}
25 changes: 11 additions & 14 deletions packages/core/src/Matchers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//@type-check
/** @typedef {import('./Route').RouteConfig} RouteConfig */
/** @typedef {import('./RequestUtils').NormalizedRequestOptions} NormalizedRequestOptions */
/** @typedef {import('path-to-regexp').Key} Key */
import glob from 'glob-to-regexp';
import pathToRegexp from 'path-to-regexp';
import glob from 'globrex';
import * as regexparam from 'regexparam';
import querystring from 'querystring';
import isSubset from 'is-subset';
import isEqual from 'lodash.isequal';
import { dequal as isEqual } from 'dequal';
import {
normalizeHeaders,
getPath,
Expand Down Expand Up @@ -54,11 +53,11 @@ const stringMatchers = {

glob: (targetString) => {
const urlRX = glob(targetString);
return (url) => urlRX.test(url);
return (url) => urlRX.regex.test(url);
},
express: (targetString) => {
const urlRX = pathToRegexp(targetString);
return (url) => urlRX.test(getPath(url));
const urlRX = regexparam.parse(targetString);
return (url) => urlRX.pattern.test(getPath(url));
},
path: (targetString) => (url) => getPath(url) === targetString,
};
Expand Down Expand Up @@ -129,16 +128,14 @@ const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => {
);
}
const expectedKeys = Object.keys(expectedParams);
/** @type {Key[]} */
const keys = [];
const re = pathToRegexp(matcherUrl.replace(/^express:/, ''), keys);
const re = regexparam.parse(matcherUrl.replace(/^express:/, ''));
return (url) => {
const vals = re.exec(getPath(url)) || [];
const vals = re.pattern.exec(getPath(url)) || [];
vals.shift();
/** @type {Object.<string,string>} */
const params = keys.reduce(
(map, { name }, i) =>
vals[i] ? Object.assign(map, { [name]: vals[i] }) : map,
const params = re.keys.reduce(
(map, paramName, i) =>
vals[i] ? Object.assign(map, { [paramName]: vals[i] }) : map,
{},
);
return expectedKeys.every((key) => params[key] === expectedParams[key]);
Expand Down
8 changes: 4 additions & 4 deletions packages/fetch-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
"homepage": "http://www.wheresrhys.co.uk/fetch-mock",
"dependencies": {
"debug": "^4.1.1",
"glob-to-regexp": "^0.4.0",
"dequal": "^2.0.3",
"globrex": "^0.1.2",
"is-subset": "^0.1.1",
"lodash.isequal": "^4.5.0",
"path-to-regexp": "^2.2.1"
"regexparam": "^3.0.0"
},
"peerDependenciesMeta": {
"node-fetch": {
"optional": true
}
},
"engines": {
"node": ">=4.0.0"
"node": ">=8.0.0"
}
}
23 changes: 11 additions & 12 deletions packages/fetch-mock/src/Route/matchers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import glob from 'glob-to-regexp';
import pathToRegexp from 'path-to-regexp';
import glob from 'globrex';
import * as regexparam from 'regexparam';
import isSubset from 'is-subset';
import isEqual from 'lodash.isequal';
import { dequal as isEqual } from 'dequal';
import {
headers as headerUtils,
getPath,
Expand All @@ -24,11 +24,11 @@ const stringMatchers = {
),
glob: (targetString) => {
const urlRX = glob(targetString);
return debuggableUrlFunc((url) => urlRX.test(url));
return debuggableUrlFunc((url) => urlRX.regex.test(url));
},
express: (targetString) => {
const urlRX = pathToRegexp(targetString);
return debuggableUrlFunc((url) => urlRX.test(getPath(url)));
const urlRX = regexparam.parse(targetString);
return debuggableUrlFunc((url) => urlRX.pattern.test(getPath(url)));
},
path: (targetString) =>
debuggableUrlFunc((url) => getPath(url) === targetString),
Expand Down Expand Up @@ -142,15 +142,14 @@ const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => {
}
debug(' Expected path parameters:', expectedParams);
const expectedKeys = Object.keys(expectedParams);
const keys = [];
const re = pathToRegexp(matcherUrl.replace(/^express:/, ''), keys);
const re = regexparam.parse(matcherUrl.replace(/^express:/, ''));
return (url) => {
debug('Attempting to match path parameters');
const vals = re.exec(getPath(url)) || [];
const vals = re.pattern.exec(getPath(url)) || [];
vals.shift();
const params = keys.reduce(
(map, { name }, i) =>
vals[i] ? Object.assign(map, { [name]: vals[i] }) : map,
const params = re.keys.reduce(
(map, paramName, i) =>
vals[i] ? Object.assign(map, { [paramName]: vals[i] }) : map,
{},
);
debug(' Expected path parameters:', expectedParams);
Expand Down

0 comments on commit 499d94e

Please sign in to comment.