fix: bigint key values erroring (#2923)
Fixes https://github.com/denoland/fresh/issues/2802
This commit is contained in:
@@ -1336,3 +1336,26 @@ Deno.test("support numeric keys", async () => {
|
|||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
expect(text).toContain("ok");
|
expect(text).toContain("ok");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Issue https://github.com/denoland/fresh/issues/2802
|
||||||
|
Deno.test("support bigint keys", async () => {
|
||||||
|
const TestComponent = () => <div>foo</div>;
|
||||||
|
|
||||||
|
const server = await createServer({
|
||||||
|
"routes/index.tsx": {
|
||||||
|
default: () => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<TestComponent key={9007199254740991n} />
|
||||||
|
ok
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await server.get("/");
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toContain("ok");
|
||||||
|
expect(text).toContain("key:9007199254740991");
|
||||||
|
});
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ options[OptionsType.VNODE] = (vnode) => {
|
|||||||
if (typeof vnode.type === "function") {
|
if (typeof vnode.type === "function") {
|
||||||
if (vnode.type === Partial) {
|
if (vnode.type === Partial) {
|
||||||
const props = vnode.props as PartialProps;
|
const props = vnode.props as PartialProps;
|
||||||
const key = normalizeKey(vnode.key ?? "");
|
const key = normalizeKey(vnode.key);
|
||||||
const mode = !props.mode || props.mode === "replace"
|
const mode = !props.mode || props.mode === "replace"
|
||||||
? PartialMode.Replace
|
? PartialMode.Replace
|
||||||
: props.mode === "append"
|
: props.mode === "append"
|
||||||
@@ -165,11 +165,10 @@ options[OptionsType.ATTR] = (name, value) => {
|
|||||||
|
|
||||||
const PATCHED = new WeakSet<VNode>();
|
const PATCHED = new WeakSet<VNode>();
|
||||||
|
|
||||||
function normalizeKey(key: string | number) {
|
function normalizeKey(key: unknown): string {
|
||||||
if (typeof key === "number") {
|
const value = key ?? "";
|
||||||
key = key.toString();
|
const s = (typeof value !== "string") ? String(value) : value;
|
||||||
}
|
return s.replaceAll(":", "_");
|
||||||
return key.replaceAll(":", "_");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldDiff = options[OptionsType.DIFF];
|
const oldDiff = options[OptionsType.DIFF];
|
||||||
@@ -204,7 +203,7 @@ options[OptionsType.DIFF] = (vnode) => {
|
|||||||
if (island === undefined) {
|
if (island === undefined) {
|
||||||
// Not an island, but we might need to preserve keys
|
// Not an island, but we might need to preserve keys
|
||||||
if (vnode.key !== undefined) {
|
if (vnode.key !== undefined) {
|
||||||
const key = normalizeKey(vnode.key ?? "");
|
const key = normalizeKey(vnode.key);
|
||||||
const originalType = vnode.type;
|
const originalType = vnode.type;
|
||||||
vnode.type = (props) => {
|
vnode.type = (props) => {
|
||||||
const child = h(originalType, props);
|
const child = h(originalType, props);
|
||||||
@@ -241,7 +240,7 @@ options[OptionsType.DIFF] = (vnode) => {
|
|||||||
const child = h(originalType, props);
|
const child = h(originalType, props);
|
||||||
PATCHED.add(child);
|
PATCHED.add(child);
|
||||||
|
|
||||||
const key = normalizeKey(vnode.key ?? "");
|
const key = normalizeKey(vnode.key);
|
||||||
return wrapWithMarker(
|
return wrapWithMarker(
|
||||||
child,
|
child,
|
||||||
"island",
|
"island",
|
||||||
|
|||||||
Reference in New Issue
Block a user