refactor: remove as any of file jsonUtils.test.ts

refactor: extract `DataType` as the return type of function `getDataType`
This commit is contained in:
yushengchen
2025-04-09 09:24:42 +08:00
parent fe74dbea74
commit 068d43226f
2 changed files with 19 additions and 12 deletions

View File

@@ -149,17 +149,17 @@ describe("updateValueAtPath", () => {
}); });
test("initializes an empty object when input is null/undefined and path starts with a string", () => { test("initializes an empty object when input is null/undefined and path starts with a string", () => {
expect(updateValueAtPath(null as any, ["foo"], "bar")).toEqual({ expect(updateValueAtPath(null, ["foo"], "bar")).toEqual({
foo: "bar", foo: "bar",
}); });
expect(updateValueAtPath(undefined as any, ["foo"], "bar")).toEqual({ expect(updateValueAtPath(undefined, ["foo"], "bar")).toEqual({
foo: "bar", foo: "bar",
}); });
}); });
test("initializes an empty array when input is null/undefined and path starts with a number", () => { test("initializes an empty array when input is null/undefined and path starts with a number", () => {
expect(updateValueAtPath(null as any, ["0"], "bar")).toEqual(["bar"]); expect(updateValueAtPath(null, ["0"], "bar")).toEqual(["bar"]);
expect(updateValueAtPath(undefined as any, ["0"], "bar")).toEqual(["bar"]); expect(updateValueAtPath(undefined, ["0"], "bar")).toEqual(["bar"]);
}); });
// Object update tests // Object update tests
@@ -293,10 +293,8 @@ describe("getValueAtPath", () => {
}); });
test("returns default value when input is null/undefined", () => { test("returns default value when input is null/undefined", () => {
expect(getValueAtPath(null as any, ["foo"], "default")).toBe("default"); expect(getValueAtPath(null, ["foo"], "default")).toBe("default");
expect(getValueAtPath(undefined as any, ["foo"], "default")).toBe( expect(getValueAtPath(undefined, ["foo"], "default")).toBe("default");
"default",
);
}); });
test("handles array indices correctly", () => { test("handles array indices correctly", () => {

View File

@@ -25,10 +25,19 @@ export type JsonSchemaType = {
export type JsonObject = { [key: string]: JsonValue }; export type JsonObject = { [key: string]: JsonValue };
const typeofVariable = typeof "random variable"; export type DataType =
export function getDataType( | "string"
value: JsonValue, | "number"
): typeof typeofVariable | "array" | "null" { | "bigint"
| "boolean"
| "symbol"
| "undefined"
| "object"
| "function"
| "array"
| "null";
export function getDataType(value: JsonValue): DataType {
if (Array.isArray(value)) return "array"; if (Array.isArray(value)) return "array";
if (value === null) return "null"; if (value === null) return "null";
return typeof value; return typeof value;