Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // function path<R>(obj: R) {
- // function createKeyAccessor<O>(parent: O) {
- // const accessor = <K extends keyof O>(key: K) => {
- // const value = parent ? parent[key] : null
- // return createKeyAccessor(value)
- // }
- // accessor.get = () => parent
- // return accessor
- // }
- // return createKeyAccessor(obj)
- // }
- // const val = { x: { a: 1, y: { z: { a: 1 } } } }
- // const yay = path(val)('x')('y')('z').get
- // v1
- // type INodeAccessor<INode> {
- // <IKey extends keyof INode, Out>(k: IKey, accessor: (d: INode | null) => Out): Out
- // <IKey extends keyof INode>(k: IKey): INodeAccessor<INode[IKey]>
- // }
- // const path = <IRoot extends {}>(obj: IRoot) => {
- // const createChildAccessor = <INode>(parent: INode) => {
- // const childAccessor: INodeAccessor<INode> = (k, access) => {
- // const child = parent[k]
- // if (access) {
- // return child ? access(child) : null
- // }
- // const accessor = createChildAccessor(child)
- // // accessor.get = parent
- // return accessor
- // }
- // return childAccessor
- // }
- // return createChildAccessor(obj)
- // }
- // const val = { x: { a: 1, y: { z: { a: 1, b: [0,1,2,3,4] } } } }
- // const yay2 = path(val)('x')('y', d => d)
- // const yay1 = path(val)('x')('y')('z')('a')
- // const yay3 = path(val)('x')('y')('z')('d')
Add Comment
Please, Sign In to add comment