-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Add support for custom claim paths (#1197)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1d138cb
commit 104d214
Showing
7 changed files
with
90 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { getValueByPath } from "./get-value-by-path"; | ||
|
||
describe("getValueByPath", () => { | ||
describe("when the path is not nested", () => { | ||
it("should return the value of the key", () => { | ||
const path = "key"; | ||
const obj = { key: "value" }; | ||
const value = getValueByPath(obj, path); | ||
expect(value).toBe("value"); | ||
}); | ||
}); | ||
|
||
describe("when the path is nested", () => { | ||
it("should return the value of the nested key", () => { | ||
const path = "nested.key"; | ||
const obj = { nested: { key: "value" } }; | ||
const value = getValueByPath(obj, path); | ||
expect(value).toBe("value"); | ||
}); | ||
}); | ||
|
||
describe("when the path is deeply nested", () => { | ||
it("should return the value of the deeply nested key", () => { | ||
const path = "deeply.nested.key"; | ||
const obj = { deeply: { nested: { key: "value" } } }; | ||
const value = getValueByPath(obj, path); | ||
expect(value).toBe("value"); | ||
}); | ||
}); | ||
|
||
describe("when the path is not found", () => { | ||
it("should return undefined", () => { | ||
const path = "key"; | ||
const obj = {}; | ||
const value = getValueByPath(obj, path); | ||
expect(value).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function getValueByPath<O extends Record<string, unknown>>( | ||
obj: O, | ||
path: string, | ||
): unknown { | ||
const pathArray = path.split("."); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let curr: any = obj; | ||
for (const part of pathArray) { | ||
if (curr[part] === undefined) { | ||
return undefined; | ||
} | ||
curr = curr[part]; | ||
} | ||
return curr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters