Skip to content

Commit 5899c44

Browse files
committed
refactor: improve null proto obj
1 parent c7d3025 commit 5899c44

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

src/_utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
export const EmptyObject = /* @__PURE__ */ (() => {
2-
const C = function () {};
3-
C.prototype = Object.create(null);
4-
return C;
5-
})() as unknown as { new (): any };
1+
// prettier-ignore
2+
export const NullProtoObj = /* @__PURE__ */ (()=>{const e=function(){};return e.prototype=Object.create(null),Object.freeze(e.prototype),e})() as unknown as { new (): any };

src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EmptyObject } from "./_utils";
1+
import { NullProtoObj } from "./_utils";
22
import type { RouterContext } from "./types";
33

44
/**
@@ -7,7 +7,7 @@ import type { RouterContext } from "./types";
77
export function createRouter<T = unknown>(): RouterContext<T> {
88
const ctx: RouterContext<T> = {
99
root: { key: "" },
10-
static: new EmptyObject(),
10+
static: new NullProtoObj(),
1111
};
1212
return ctx;
1313
}

src/operations/_utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EmptyObject } from "../_utils";
1+
import { NullProtoObj } from "../_utils";
22
import type { MatchedRoute, ParamsIndexMap } from "../types";
33

44
export function splitPath(path: string) {
@@ -9,7 +9,7 @@ export function getMatchParams(
99
segments: string[],
1010
paramsMap: ParamsIndexMap,
1111
): MatchedRoute["params"] {
12-
const params = new EmptyObject();
12+
const params = new NullProtoObj();
1313
for (const [index, name] of paramsMap) {
1414
const segment =
1515
index < 0 ? segments.slice(-1 * index).join("/") : segments[index];

src/operations/add.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EmptyObject } from "../_utils";
1+
import { NullProtoObj } from "../_utils";
22
import type { RouterContext, ParamsIndexMap } from "../types";
33
import { splitPath } from "./_utils";
44

@@ -60,7 +60,7 @@ export function addRoute<T>(
6060
} else {
6161
const staticNode = { key: segment };
6262
if (!node.static) {
63-
node.static = new EmptyObject();
63+
node.static = new NullProtoObj();
6464
}
6565
node.static![segment] = staticNode;
6666
node = staticNode;
@@ -70,7 +70,7 @@ export function addRoute<T>(
7070
// Assign index, params and data to the node
7171
const hasParams = paramsMap.length > 0;
7272
if (!node.methods) {
73-
node.methods = new EmptyObject();
73+
node.methods = new NullProtoObj();
7474
}
7575
if (!node.methods![method]) {
7676
node.methods![method] = [];

0 commit comments

Comments
 (0)