Guest User

Untitled

a guest
Jul 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // function path<R>(obj: R) {
  2. // function createKeyAccessor<O>(parent: O) {
  3. // const accessor = <K extends keyof O>(key: K) => {
  4. // const value = parent ? parent[key] : null
  5. // return createKeyAccessor(value)
  6. // }
  7. // accessor.get = () => parent
  8. // return accessor
  9. // }
  10.  
  11. // return createKeyAccessor(obj)
  12. // }
  13.  
  14. // const val = { x: { a: 1, y: { z: { a: 1 } } } }
  15.  
  16. // const yay = path(val)('x')('y')('z').get
  17.  
  18. // v1
  19.  
  20. // type INodeAccessor<INode> {
  21. // <IKey extends keyof INode, Out>(k: IKey, accessor: (d: INode | null) => Out): Out
  22. // <IKey extends keyof INode>(k: IKey): INodeAccessor<INode[IKey]>
  23. // }
  24.  
  25. // const path = <IRoot extends {}>(obj: IRoot) => {
  26. // const createChildAccessor = <INode>(parent: INode) => {
  27. // const childAccessor: INodeAccessor<INode> = (k, access) => {
  28. // const child = parent[k]
  29. // if (access) {
  30. // return child ? access(child) : null
  31. // }
  32. // const accessor = createChildAccessor(child)
  33. // // accessor.get = parent
  34. // return accessor
  35. // }
  36. // return childAccessor
  37. // }
  38. // return createChildAccessor(obj)
  39. // }
  40.  
  41. // const val = { x: { a: 1, y: { z: { a: 1, b: [0,1,2,3,4] } } } }
  42. // const yay2 = path(val)('x')('y', d => d)
  43. // const yay1 = path(val)('x')('y')('z')('a')
  44. // const yay3 = path(val)('x')('y')('z')('d')
Add Comment
Please, Sign In to add comment