Skip to content

Commit 90adcc9

Browse files
committed
more tests
1 parent 22add16 commit 90adcc9

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, it, expect } from "vitest";
2+
3+
import { getValue, setValue, toObjPath } from "./objPath";
4+
import { mapObject } from "@/lib/utils/mapObject";
5+
6+
describe("Test that mapObject()", () => {
7+
it("works", () => {
8+
const obj = { a: 1, b: "4", c: true };
9+
expect(mapObject(obj, (x) => Number(x))).toEqual({ a: 1, b: 4, c: 1 });
10+
});
11+
});

dashi/src/lib/utils/objPath.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect } from "vitest";
22

3-
import { getValue, setValue } from "./objPath";
3+
import { getValue, setValue, toObjPath } from "./objPath";
44

55
describe("Test that getValue()", () => {
66
it("works on 0th level", () => {
@@ -71,3 +71,30 @@ describe("Test that setValue()", () => {
7171
expect(obj2).toEqual({ a: [1, 2, 3], b: "x" });
7272
});
7373
});
74+
75+
describe("Test that toObjPath()", () => {
76+
it("does not convert arrays", () => {
77+
const value = [1, 2, 3];
78+
expect(toObjPath(value)).toBe(value);
79+
expect(toObjPath(value)).toEqual([1, 2, 3]);
80+
});
81+
82+
it("converts undefined", () => {
83+
expect(toObjPath(undefined)).toEqual([]);
84+
});
85+
86+
it("converts null", () => {
87+
expect(toObjPath(null)).toEqual([]);
88+
});
89+
90+
it("converts numbers", () => {
91+
expect(toObjPath(3)).toEqual([3]);
92+
});
93+
94+
it("converts strings", () => {
95+
expect(toObjPath("")).toEqual([]);
96+
expect(toObjPath("colors")).toEqual(["colors"]);
97+
expect(toObjPath("colors.6")).toEqual(["colors", 6]);
98+
expect(toObjPath("colors.6.red")).toEqual(["colors", 6, "red"]);
99+
});
100+
});

dashi/src/lib/utils/objPath.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,18 @@ export function toObjPath(
6666
): ObjPath {
6767
if (Array.isArray(property)) {
6868
return property as ObjPath;
69-
}
70-
if (property === "" || !property) {
69+
} else if (property === "" || !property) {
7170
return [];
72-
}
73-
if (typeof property === "number") {
71+
} else if (typeof property === "number") {
7472
return [property];
75-
}
76-
const objPath: ObjPath = property.split(".");
77-
for (let i = 0; i < objPath.length; i++) {
78-
const index = Number(objPath[i]);
79-
if (Number.isInteger(index)) {
80-
objPath[i] = index;
73+
} else {
74+
const objPath: ObjPath = property.split(".");
75+
for (let i = 0; i < objPath.length; i++) {
76+
const index = Number(objPath[i]);
77+
if (Number.isInteger(index)) {
78+
objPath[i] = index;
79+
}
8180
}
81+
return objPath;
8282
}
83-
return objPath;
8483
}

0 commit comments

Comments
 (0)