@reffuse/extension-query 0.1.4 #15
@@ -1,3 +1,6 @@
|
||||
import { Option, Predicate } from "effect"
|
||||
|
||||
|
||||
export type Paths<T> = T extends object
|
||||
? {
|
||||
[K in keyof T as K extends string | number | symbol ? K : never]:
|
||||
@@ -26,9 +29,19 @@ const persons = [
|
||||
]
|
||||
|
||||
|
||||
const getFromPath = <T, const P extends Paths<T>>(value: T, path: P): ValueFromPath<T, P> => (
|
||||
path.reduce((acc: any, key: any) => acc?.[key], value)
|
||||
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)
|
||||
? Predicate.hasProperty(acc.value, key)
|
||||
? Option.some(acc.value[key])
|
||||
: Option.none()
|
||||
: acc,
|
||||
|
||||
Option.some(parent),
|
||||
)
|
||||
|
||||
const res = getFromPath(persons, [1, "name"])
|
||||
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)
|
||||
)
|
||||
|
||||
const res = get(persons, [1, "name"])
|
||||
console.log(res)
|
||||
|
||||
Reference in New Issue
Block a user