All files / src/zipper modify.ts

100% Statements 63/63
100% Branches 11/11
100% Functions 5/5
100% Lines 63/63

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 2061x 1x 1x 1x 1x 1x 1x                                               1x     1x 1x 1x 7x 7x 7x 1x   1x 5x 5x 5x 5x 5x 5x   1x 6x 6x 6x 6x 6x 6x                                                                     1x       1x 1x 1x 2x 2x 1x                                                                     1x       1x 1x 1x 2x 2x 1x                                                                       1x   5x 1x     1x 1x   4x 5x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
import {isNonEmptyArray, lastInit} from '#Array'
import {dual, type EndoOf} from '#Function'
import * as Tree from '#tree'
import {Option} from 'effect'
import {hasRights} from './data.js'
import {type OptionalZipper, type Zipper} from './index.js'
import {head, last, next} from './navigate.js'
 
/**
 * Replace the focus tree node of the zipper with the given tree node.
 * @example
 * import {Zipper, from, of} from 'effect-tree'
 * import {pipe} from 'effect'
 *
 * const tree = from(1, of(2))
 *
 * const changed = pipe(
 *   tree,
 *   Zipper.fromTree,
 *   Zipper.head,
 *   Zipper.replace(from(3, of(4))),
 *   Zipper.toTree,
 * )
 *
 * expect(changed).toEqual(from(1, from(3, of(4))))
 * @typeParam A The underlying type of the tree.
 * @returns A function that takes a zipper and returns an updated zipper where the focus node has been replaced.
 * @category zipper
 * @function
 */
export const replace: {
  <A>(zipper: Zipper<A>, newNode: Tree.Tree<A>): Zipper<A>
  <A>(newNode: Tree.Tree<A>): (zipper: Zipper<A>) => Zipper<A>
} = dual(
  2,
  <A>({focus: _, ...rest}: Zipper<A>, newNode: Tree.Tree<A>): Zipper<A> => ({
    ...rest,
    focus: newNode,
  }),
)
 
const _prepend = <A>(
  {focus, ...rest}: Zipper<A>,
  newNode: Tree.Tree<A>,
): Zipper<A> => ({
  ...rest,
  focus: Tree.prepend(focus, newNode),
})
 
const _append = <A>(
  {focus, ...rest}: Zipper<A>,
  newNode: Tree.Tree<A>,
): Zipper<A> => ({
  ...rest,
  focus: Tree.append(focus, newNode),
})
 
/**
 * Insert the given node as the first child of the given focus.
 *
 * At the key `move` you will find a version that moves the focus to the newly
 * inserted node.
 * @example
 * import {Zipper, from, of} from 'effect-tree'
 * import {pipe} from 'effect'
 *
 * const tree = from(1, of(2))
 *
 * const changed = pipe(
 *   tree,
 *   Zipper.fromTree,
 *   Zipper.prepend(of(1.5)),
 *   Zipper.toTree,
 * )
 *
 * expect(changed, 'changed').toEqual(from(1, of(1.5), of(2)))
 *
 * // This time we move to the newly inserted tree
 * const moved = pipe(
 *   tree,
 *   Zipper.fromTree,
 *   Zipper.prepend.move(of(1.5)),
 * )
 *
 * expect(Zipper.getValue(moved), 'moved').toBe(1.5)
 * @typeParam A The underlying type of the tree.
 * @returns A function that takes a zipper and returns an updated zipper where the focus node has been replaced.
 * @category zipper
 * @function
 */
export const prepend: {
  <A>(self: Zipper<A>, newNode: Tree.Tree<A>): Zipper<A>
  <A>(newNode: Tree.Tree<A>): (self: Zipper<A>) => Zipper<A>
  move: <A>(newNode: Tree.Tree<A>) => EndoOf<Zipper<A>>
} = Object.assign(dual(2, _prepend), {
  move:
    <A>(newNode: Tree.Tree<A>): EndoOf<Zipper<A>> =>
    self =>
      head(_prepend(self, newNode)),
})
 
/**
 * Append the given node as the last child of the given focus.
 *
 * At the key `move` you will find a version where the focus has been _moved_ to
 * the newly appended node.
 * @example
 * import {Zipper, from, of} from 'effect-tree'
 * import {pipe} from 'effect'
 *
 * const tree = from(1, of(2))
 *
 * const changed = pipe(
 *   tree,
 *   Zipper.fromTree,
 *   Zipper.append(of(3)),
 *   Zipper.toTree,
 * )
 *
 * expect(changed, 'changed').toEqual(from(1, of(2), of(3)))
 *
 * // This time we move to the newly appended tree
 * const moved = pipe(
 *   tree,
 *   Zipper.fromTree,
 *   Zipper.append.move(of(3)),
 * )
 *
 * expect(Zipper.getValue(moved), 'moved').toBe(3)
 * @typeParam A The underlying type of the tree.
 * @returns A function that takes a zipper and returns an updated zipper where the focus node has been replaced.
 * @category zipper
 * @function
 */
export const append: {
  <A>(self: Zipper<A>, newNode: Tree.Tree<A>): Zipper<A>
  <A>(newNode: Tree.Tree<A>): (self: Zipper<A>) => Zipper<A>
  move: <A>(newNode: Tree.Tree<A>) => EndoOf<Zipper<A>>
} = Object.assign(dual(2, _append), {
  move:
    <A>(newNode: Tree.Tree<A>): EndoOf<Zipper<A>> =>
    self =>
      last(_append(self, newNode)),
})
 
/**
 * Remove the current focused tree node from the tree, and return the zipper
 * focused on the _next_ sibling.
 *
 * When no next sibling exists, for example if the node is the last in the
 * forest, focuses _up_ on the parent of the removed node.
 *
 * When the zipper is focused on tree root returns `Option.none()`.
 * @example
 * import {Zipper, from, of} from 'effect-tree'
 * import {Option, pipe} from 'effect'
 *
 * const tree = from(1, of(2))
 *
 * const changed = pipe(
 *   tree,
 *   Zipper.fromTree,
 *   Zipper.head,
 *   Zipper.remove,
 *   Option.map(Zipper.toTree),
 * )
 * expect(changed, 'leaf').toEqual(Option.some(of(1)))
 *
 * const removeRoot = pipe(
 *   changed,
 *   Option.map(Zipper.fromTree),
 *   Option.flatMap(Zipper.remove),
 * )
 * expect(removeRoot, 'root').toEqual(Option.none())
 * @typeParam A The underlying type of the tree.
 * @returns A zipper without the previously focused node focused on next node, or failing that, on the parent node. If the zipper is focused on the tree root returns `Option.none()`.
 * @category zipper
 * @function
 */
export const remove: OptionalZipper = <A>(self: Zipper<A>) => {
  // Try to remove, then move _next_ in forest.
  if (hasRights(self)) {
    const {lefts, ...rest} = next(self)
    // Discard final element in “lefts” because we just moved right and that is
    // where the focus node we need to remove will be found.
    return Option.some({...rest, lefts: lefts.splice(0, -1)})
  }
 
  const {levels: previousLevels, parent: previousParent, lefts, rights} = self
  if (!isNonEmptyArray(previousLevels)) {
    return Option.none()
  }
 
  // Remove and move _up_.
  const [lastLevel, levels] = lastInit(previousLevels)
  return Option.some({
    focus: Tree.from(
      (previousParent as Option.Some<A>).value,
      ...lefts, // Previous focus node is NOT added.
      ...rights,
    ),
    levels,
    ...lastLevel,
  })
}