SearchPaths work
All checks were successful
Lint / lint (push) Successful in 14s

This commit is contained in:
Julien Valverdé
2025-04-22 22:59:50 +02:00
parent 58752253b3
commit 8917f84952

View File

@@ -29,6 +29,10 @@ const persons = [
] ]
export const unsafeGet = <T, const P extends Paths<T>>(parent: T, path: P): ValueFromPath<T, P> => (
path.reduce((acc: any, key: any) => acc?.[key], parent)
)
export const get = <T, const P extends Paths<T>>(parent: T, path: P): Option.Option<ValueFromPath<T, P>> => path.reduce( export const get = <T, const P extends Paths<T>>(parent: T, path: P): Option.Option<ValueFromPath<T, P>> => path.reduce(
(acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc) (acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc)
? Predicate.hasProperty(acc.value, key) ? Predicate.hasProperty(acc.value, key)
@@ -39,9 +43,18 @@ export const get = <T, const P extends Paths<T>>(parent: T, path: P): Option.Opt
Option.some(parent), Option.some(parent),
) )
export const getOrUndefined = <T, const P extends Paths<T>>(parent: T, path: P): ValueFromPath<T, P> => (
path.reduce((acc: any, key: any) => acc?.[key], parent) export const unsafeImmutableSet = <T, const P extends Paths<T>>(parent: T, path: P): Option.Option<ValueFromPath<T, P>> => path.reduce(
(acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc)
? Predicate.hasProperty(acc.value, key)
? Option.some(acc.value[key])
: Option.none()
: acc,
Option.some(parent),
) )
const res = get(persons, [1, "name"]) const res = get(persons, [1, "name"])
console.log(res) console.log(res)