Skip to content

Commit d7fe9c2

Browse files
committed
introduced equalObjPaths
1 parent 0d7067e commit d7fe9c2

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

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

Lines changed: 18 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, toObjPath } from "./objPath";
3+
import { equalObjPaths, getValue, setValue, toObjPath } from "./objPath";
44

55
describe("Test that getValue()", () => {
66
it("works on 0th level", () => {
@@ -98,3 +98,20 @@ describe("Test that toObjPath()", () => {
9898
expect(toObjPath("colors.6.red")).toEqual(["colors", 6, "red"]);
9999
});
100100
});
101+
102+
describe("Test that equalObjPaths()", () => {
103+
it("works with equal paths", () => {
104+
expect(equalObjPaths("a", "a")).toBe(true);
105+
expect(equalObjPaths(2, 2)).toBe(true);
106+
expect(equalObjPaths("2", 2)).toBe(true);
107+
expect(equalObjPaths([3, 1, 2], [3, 1, 2])).toBe(true);
108+
expect(equalObjPaths([3, 1, 2], "3.1.2")).toBe(true);
109+
});
110+
111+
it("works with non-equal paths", () => {
112+
expect(equalObjPaths("a", "b")).toBe(false);
113+
expect(equalObjPaths("a", 2)).toBe(false);
114+
expect(equalObjPaths("a", 2)).toBe(false);
115+
expect(equalObjPaths([3, 1, 2], [3, 1, 2, 0])).toBe(false);
116+
});
117+
});

dashi/src/lib/utils/objPath.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { isObject } from "@/lib/utils/isObject";
22

33
export type ObjPath = (string | number)[];
4+
export type ObjPathLike = ObjPath | string | number | undefined | null;
45

56
type Obj = Record<string | number, unknown>;
67

7-
export function getValue(
8-
obj: object | undefined,
9-
path: ObjPath | string,
10-
): unknown {
8+
export function getValue(obj: object | undefined, path: ObjPathLike): unknown {
119
path = toObjPath(path);
1210
let value: unknown = obj;
1311
for (const key of path) {
@@ -22,7 +20,7 @@ export function getValue(
2220

2321
export function setValue<S extends object | undefined>(
2422
obj: S,
25-
path: ObjPath | string,
23+
path: ObjPathLike,
2624
value: unknown,
2725
): S {
2826
return _setValue(obj, toObjPath(path), value);
@@ -65,17 +63,15 @@ export function _setValue<S extends object | undefined>(
6563
return obj;
6664
}
6765

68-
export function toObjPath(
69-
property: ObjPath | string | number | undefined | null,
70-
): ObjPath {
71-
if (Array.isArray(property)) {
72-
return property as ObjPath;
73-
} else if (!property || property === "") {
66+
export function toObjPath(pathLike: ObjPathLike): ObjPath {
67+
if (Array.isArray(pathLike)) {
68+
return pathLike as ObjPath;
69+
} else if (!pathLike || pathLike === "") {
7470
return [];
75-
} else if (typeof property === "number") {
76-
return [property];
71+
} else if (typeof pathLike === "number") {
72+
return [pathLike];
7773
} else {
78-
const objPath: ObjPath = property.split(".");
74+
const objPath: ObjPath = pathLike.split(".");
7975
for (let i = 0; i < objPath.length; i++) {
8076
const index = Number(objPath[i]);
8177
if (Number.isInteger(index)) {
@@ -85,3 +81,15 @@ export function toObjPath(
8581
return objPath;
8682
}
8783
}
84+
85+
export function equalObjPaths(pathLike1: ObjPathLike, pathLike2: ObjPathLike) {
86+
if (pathLike1 === pathLike2) {
87+
return true;
88+
}
89+
const path1 = toObjPath(pathLike1);
90+
const path2 = toObjPath(pathLike2);
91+
return (
92+
path1.length === path2.length &&
93+
path1.every((item, index) => item === path2[index])
94+
);
95+
}

0 commit comments

Comments
 (0)