Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x | import * as Tree from '#tree'
import {Option} from 'effect'
import {type Zipper} from './index.js'
/**
* Get the current focus of the zipper.
* @example
* import {Zipper, from, of} from 'effect-tree'
* import {pipe} from 'effect'
*
* const tree = from(1, of(2))
*
* const zipper = pipe(
* tree,
* Zipper.fromTree,
* Zipper.head,
* )
*
* expect(Zipper.getFocus(zipper)).toEqual(of(2))
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns The focus of the zipper.
* @category zipper
* @function
*/
export const getFocus = <A>({focus}: Zipper<A>): Tree.Tree<A> => focus
/**
* Get the value of the tree node under focus.
* @example
* import {Zipper, from, of} from 'effect-tree'
* import {pipe} from 'effect'
*
* const tree = from(1, of(2))
*
* const zipper = pipe(
* tree,
* Zipper.fromTree,
* Zipper.head,
* )
*
* expect(Zipper.getValue(zipper)).toBe(2)
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns Value of the focus node.
* @category zipper
* @function
*/
export const getValue = <A>({focus}: Zipper<A>): A => Tree.getValue(focus)
/**
* Get the forest of the tree node under focus. Result could be an empty list if
* the focused node is a {@link Leaf}.
* @example
* import {Zipper, from, of} from 'effect-tree'
*
* const tree = from(1, of(2))
* const zipper = Zipper.fromTree(tree)
*
* expect(Zipper.getForest(zipper)).toEqual([of(2)])
* @typeParam A Underlying tree type.
* @param self The tree being queried.
* @returns A possibly empty list of trees.
* @category zipper
* @function
*/
export const getForest = <A>({focus}: Zipper<A>): readonly Tree.Tree<A>[] =>
Tree.getForest(focus)
/**
* True if there are trees to the _left_ of the focus, false if focused node is
* head in its forest.
* @example
* import {Zipper, from, of} from 'effect-tree'
* import {pipe} from 'effect'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = pipe(
* tree,
* Zipper.fromTree,
* Zipper.head
* )
*
* expect(Zipper.hasLefts(zipper)).toBe(false)
*
* const moved = Zipper.next(zipper)
*
* expect(Zipper.hasLefts(moved)).toBe(true)
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns True if there are trees to the _left_ of the focus.
* @category zipper
* @function
*/
export const hasLefts = <A>({lefts}: Zipper<A>): boolean => lefts.length > 0
/**
* True if there are trees to the _right_ of the focus, false if focused node is
* last in its forest.
* @example
* import {Zipper, from, of} from 'effect-tree'
* import {pipe} from 'effect'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = pipe(
* tree,
* Zipper.fromTree,
* Zipper.head
* )
*
* expect(Zipper.hasRights(zipper)).toBe(true)
*
* const moved = Zipper.next(zipper)
*
* expect(Zipper.hasRights(moved)).toBe(false)
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns True if there are trees to the _right_ of the focus.
* @category zipper
* @function
*/
export const hasRights = <A>({rights}: Zipper<A>): boolean => rights.length > 0
/**
* Get the nodes to the left of the focus node.
* @example
* import {Zipper, from, of} from 'effect-tree'
* import {pipe} from 'effect'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = pipe(
* tree,
* Zipper.fromTree,
* Zipper.head
* )
*
* expect(Zipper.getLefts(zipper)).toEqual([])
*
* const moved = Zipper.next(zipper)
*
* expect(Zipper.getLefts(moved)).toEqual([of(2)])
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns Possibly empty list of trees.
* @category zipper
* @function
*/
export const getLefts = <A>({lefts}: Zipper<A>): Tree.Tree<A>[] => lefts
/**
* Get the nodes to the left of the focus node.
* @example
* import {Zipper, from, of} from 'effect-tree'
* import {pipe} from 'effect'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = pipe(
* tree,
* Zipper.fromTree,
* Zipper.head
* )
*
* expect(Zipper.getRights(zipper)).toEqual([of(3)])
*
* const moved = Zipper.next(zipper)
*
* expect(Zipper.getRights(moved)).toEqual([])
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns Possibly empty list of trees.
* @category zipper
* @function
*/
export const getRights = <A>({rights}: Zipper<A>): Tree.Tree<A>[] => rights
/**
* Get the depth of the zipper, where a zipper focused on the root node gets a
* depth of `0`.
* @example
* import {Zipper, from, of} from 'effect-tree'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = Zipper.fromTree(tree)
*
* expect(Zipper.getDepth(zipper)).toBe(0)
*
* const moved = Zipper.head(zipper)
*
* expect(Zipper.getDepth(moved)).toBe(1)
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns Integer depth of the zipper.
* @category zipper
* @function
*/
export const getDepth = <A>({levels}: Zipper<A>): number => levels.length
/**
* True if the zipper is currently focused on the tree root node.
* @example
* import {Zipper, from, of} from 'effect-tree'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = Zipper.fromTree(tree)
*
* expect(Zipper.isRoot(zipper)).toBe(true)
*
* const moved = Zipper.head(zipper)
*
* expect(Zipper.isRoot(moved)).toBe(false)
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns True if zipper is at root else false.
* @category zipper
* @function
*/
export const isRoot = <A>({parent}: Zipper<A>): boolean => Option.isNone(parent)
/**
* True if the zipper is currently focused on a leaf.
* @example
* import {Zipper, from, of} from 'effect-tree'
*
* const tree = from(1, of(2), of(3))
*
* const zipper = Zipper.fromTree(tree)
*
* expect(Zipper.isLeaf(zipper)).toBe(false)
*
* const moved = Zipper.head(zipper)
*
* expect(Zipper.isLeaf(moved)).toBe(true)
* @typeParam A Underlying tree type.
* @param zipper Zipper to be queried.
* @returns True if zipper is at leaf else false.
* @category zipper
* @function
*/
export const isLeaf = <A>({focus}: Zipper<A>): boolean => Tree.isLeaf(focus)
|