fix: Task2 质量修复(TS2774/降级分支测试/跨平台checksize/显式lib)
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+2
-1
@@ -6,7 +6,8 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "node build.mjs",
|
"build": "node build.mjs",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"checksize": "node build.mjs && gzip -c dist/v1.js | wc -c"
|
"typecheck": "tsc --noEmit",
|
||||||
|
"checksize": "node build.mjs && node -e \"const z=require('zlib'),f=require('fs');console.log('gzip bytes:',z.gzipSync(f.readFileSync('dist/v1.js')).length)\""
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"esbuild": "^0.24.0",
|
"esbuild": "^0.24.0",
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
export function uuid(): string {
|
export function uuid(): string {
|
||||||
if (globalThis.crypto?.randomUUID) return crypto.randomUUID();
|
const c = globalThis.crypto as Crypto | undefined;
|
||||||
|
if (c?.randomUUID) return c.randomUUID();
|
||||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||||
const r = (Math.random() * 16) | 0;
|
const r = (Math.random() * 16) | 0;
|
||||||
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect, vi } from "vitest";
|
||||||
import { uuid } from "../src/util";
|
import { uuid } from "../src/util";
|
||||||
describe("uuid", () => {
|
describe("uuid", () => {
|
||||||
it("生成 36 位且不重复", () => {
|
it("生成 36 位且不重复", () => {
|
||||||
@@ -6,4 +6,11 @@ describe("uuid", () => {
|
|||||||
expect(a).toMatch(/^[0-9a-f-]{36}$/);
|
expect(a).toMatch(/^[0-9a-f-]{36}$/);
|
||||||
expect(a).not.toBe(b);
|
expect(a).not.toBe(b);
|
||||||
});
|
});
|
||||||
|
it("Math.random 降级分支:格式正确且唯一", () => {
|
||||||
|
vi.stubGlobal("crypto", {});
|
||||||
|
const a = uuid(), b = uuid();
|
||||||
|
expect(a).toMatch(/^[0-9a-f-]{36}$/);
|
||||||
|
expect(a).not.toBe(b);
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2018",
|
"target": "ES2018",
|
||||||
|
"lib": ["ES2018", "DOM"],
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user