Skip to content

Commit 0d163a5

Browse files
committed
style: format code for consistency and readability
1 parent 51ab57e commit 0d163a5

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

index.js

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,111 +2,111 @@
22
const createMatchHandler = (updateParams) =>
33
updateParams
44
? (req, res, params) => {
5-
req.params = params;
6-
return true;
5+
req.params = params
6+
return true
77
}
8-
: () => true;
8+
: () => true
99

10-
const defaultHandler = () => false;
10+
const defaultHandler = () => false
1111

1212
// Router cache for reusing router instances
13-
const routerCache = new WeakMap();
13+
const routerCache = new WeakMap()
1414

15-
function normalizeEndpoint(endpoint) {
16-
if (typeof endpoint === "string") {
17-
return { url: endpoint, methods: ["GET"], updateParams: false };
15+
function normalizeEndpoint (endpoint) {
16+
if (typeof endpoint === 'string') {
17+
return { url: endpoint, methods: ['GET'], updateParams: false }
1818
}
1919
return {
20-
methods: endpoint.methods || ["GET"],
20+
methods: endpoint.methods || ['GET'],
2121
url: endpoint.url,
2222
version: endpoint.version,
23-
updateParams: endpoint.updateParams || false,
24-
};
23+
updateParams: endpoint.updateParams || false
24+
}
2525
}
2626

27-
module.exports = function (routerOpts = {}, routerFactory = require("find-my-way")) {
28-
function exec(options, isIff = true) {
29-
const middleware = this;
30-
let router = null;
31-
let customFn = null;
27+
module.exports = function (routerOpts = {}, routerFactory = require('find-my-way')) {
28+
function exec (options, isIff = true) {
29+
const middleware = this
30+
let router = null
31+
let customFn = null
3232

3333
// Process options efficiently
34-
if (typeof options === "function") {
35-
customFn = options;
34+
if (typeof options === 'function') {
35+
customFn = options
3636
} else {
37-
const endpoints = Array.isArray(options) ? options : options?.endpoints;
37+
const endpoints = Array.isArray(options) ? options : options?.endpoints
3838

3939
if (endpoints?.length) {
4040
// Try to get cached router first
41-
let cache = routerCache.get(routerOpts);
41+
let cache = routerCache.get(routerOpts)
4242
if (!cache) {
43-
cache = new Map();
44-
routerCache.set(routerOpts, cache);
43+
cache = new Map()
44+
routerCache.set(routerOpts, cache)
4545
}
4646

47-
const cacheKey = JSON.stringify(endpoints);
48-
router = cache.get(cacheKey);
47+
const cacheKey = JSON.stringify(endpoints)
48+
router = cache.get(cacheKey)
4949

5050
if (!router) {
51-
router = routerFactory({ ...routerOpts, defaultRoute: defaultHandler });
51+
router = routerFactory({ ...routerOpts, defaultRoute: defaultHandler })
5252

5353
// Normalize and register routes
54-
const normalized = endpoints.map(normalizeEndpoint);
54+
const normalized = endpoints.map(normalizeEndpoint)
5555
for (const { methods, url, version, updateParams } of normalized) {
56-
const handler = createMatchHandler(updateParams);
56+
const handler = createMatchHandler(updateParams)
5757

5858
if (version) {
59-
router.on(methods, url, { constraints: { version } }, handler);
59+
router.on(methods, url, { constraints: { version } }, handler)
6060
} else {
61-
router.on(methods, url, handler);
61+
router.on(methods, url, handler)
6262
}
6363
}
6464

65-
cache.set(cacheKey, router);
65+
cache.set(cacheKey, router)
6666
}
6767
}
6868

6969
if (options?.custom) {
70-
customFn = options.custom;
70+
customFn = options.custom
7171
}
7272
}
7373

7474
// Optimized execution function
7575
const result = function (req, res, next) {
76-
let shouldExecute = false;
76+
let shouldExecute = false
7777

7878
if (customFn) {
79-
shouldExecute = customFn(req);
79+
shouldExecute = customFn(req)
8080
} else if (router) {
81-
shouldExecute = router.lookup(req, res);
81+
shouldExecute = router.lookup(req, res)
8282
}
8383

8484
// Simplified logic: execute middleware if conditions match
8585
if ((isIff && shouldExecute) || (!isIff && !shouldExecute)) {
86-
return middleware(req, res, next);
86+
return middleware(req, res, next)
8787
}
8888

89-
return next();
90-
};
89+
return next()
90+
}
9191

9292
// Allow chaining
93-
result.iff = iff;
94-
result.unless = unless;
93+
result.iff = iff
94+
result.unless = unless
9595

96-
return result;
96+
return result
9797
}
9898

99-
function iff(options) {
100-
return exec.call(this, options, true);
99+
function iff (options) {
100+
return exec.call(this, options, true)
101101
}
102-
function unless(options) {
103-
return exec.call(this, options, false);
102+
function unless (options) {
103+
return exec.call(this, options, false)
104104
}
105105

106106
return function (middleware) {
107-
middleware.iff = iff;
108-
middleware.unless = unless;
107+
middleware.iff = iff
108+
middleware.unless = unless
109109

110-
return middleware;
111-
};
112-
};
110+
return middleware
111+
}
112+
}

0 commit comments

Comments
 (0)