All files / src/tree add.ts

100% Statements 63/63
100% Branches 23/23
100% Functions 1/1
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 206 207 208 209 210 2111x 1x 1x 1x                                               1x     1x 5x 5x 5x 5x 5x 5x 4x 4x 1x 3x   4x 4x 5x 5x 5x                                           1x     1x 1x 1x 1x 1x                                             1x     1x 1x 1x 9x 3x 6x 1x                                             1x     1x 1x 1x 8x 3x 5x 1x                                               1x     1x 1x 1x 4x 3x 1x 2x 1x 1x                                               1x     1x 1x 1x 4x 3x 1x 2x 1x 1x  
import {Array} from '#util'
import {dual, pipe} from '#Function'
import {branch, getValue, isLeaf, match} from './index.js'
import {length, modBranchForest, modForest, setForest} from './data.js'
import type {Branch, ForestOf, Tree} from './types.js'
 
/**
 * Insert a list of trees before Nth child of the given tree. The list is
 * inserted so that the head element of the inserted list becomes the Nth child
 * of the tree, and the previous Nth child is pushed after the inserted list.
 *
 * Negative indexes are handled as offsets from the final tree in the forest so
 * that inserting a list to index `-1` inserts the list _before_ the last
 * tree of the forest. Use {@link appendAll} to append _after_ the last tree
 * in the forest.
 *
 * If `self` is a _leaf_, it is converted into a branch.
 *
 * If the index is out-of-bounds, I.E.: negative or greater than `forest length
 * - 1`, the list is appended to the _end_ of the forest.
 * @typeParam A Tree underlying type.
 * @param self The tree to modify.
 * @param children Non-empty list of child trees to insert.
 * @returns A new updated tree with the new child trees inserted.
 * @category basic
 * @function
 */
export const insertAllAt: {
  <A>(self: Tree<A>, n: number, children: ForestOf<A>): Branch<A>
  <A>(n: number, children: ForestOf<A>): (self: Tree<A>) => Branch<A>
} = dual(3, <A>(self: Tree<A>, n: number, children: ForestOf<A>): Branch<A> => {
  const index = n + (n < 0 ? length(self) : 0)
  return pipe(
    self,
    match({
      onLeaf: branch(children),
      onBranch: (value, oldForest) => {
        const forest =
          index >= oldForest.length
            ? [...oldForest, ...children]
            : oldForest.toSpliced(index, 0, ...children)
 
        return branch(value, forest as unknown as ForestOf<A>)
      },
    }),
  )
})
 
/**
 * Insert a tree before Nth child of the given tree. The tree is inserted so
 * that it becomes the Nth child of the tree, and the previous Nth child becomes
 * moves over to position N+1.
 *
 * Negative indexes are handled as offsets from the final tree in the forest so
 * that inserting a tree to index `-1` inserts the tree _before_ the last
 * tree of the forest. Use {@link append} to append _after_ the last tree.
 *
 * If `self` is a _leaf_, it is converted into a branch.
 *
 * If the index is out-of-bounds, I.E.: negative or greater than `forest length
 * - 1`, the tree is appended to the _end_ of the forest.
 * @typeParam A Tree underlying type.
 * @param self The tree to modify.
 * @param child- Child to insert.
 * @returns A new updated tree with the new node inserted.
 * @category basic
 * @function
 */
export const insertAt: {
  <A>(self: Tree<A>, child: Tree<A>, n: number): Branch<A>
  <A>(child: Tree<A>): (self: Tree<A>, n: number) => Branch<A>
} = dual(
  3,
  <A>(self: Tree<A>, child: Tree<A>, n: number): Branch<A> =>
    insertAllAt(self, n, [child]),
)
 
/**
 * Append a tree to the children of the root node. If `self` is a _leaf_, it is
 * converted into a branch.
 * @example
 * import * as Tree from 'effect-tree'
 *
 * const tree = Tree.tree(1, [Tree.of(2), Tree.of(3)])
 * const changed = Tree.append(tree, Tree.of(4))
 *
 * expect(Tree.getForest(changed)).toEqual([
 *   Tree.of(2),
 *   Tree.of(3),
 *   Tree.of(4),
 * ])
 * @typeParam A Tree underlying type.
 * @param self The tree to modify.
 * @param child Child to append.
 * @returns A new updated tree with the new node appended.
 * @category basic
 * @function
 */
export const append: {
  <A>(self: Tree<A>, child: Tree<A>): Branch<A>
  <A>(child: Tree<A>): (self: Tree<A>) => Branch<A>
} = dual(
  2,
  <A>(self: Tree<A>, child: Tree<A>): Branch<A> =>
    isLeaf(self)
      ? pipe(self, getValue, branch([child]))
      : pipe(self, modBranchForest(Array.append(child))),
)
 
/**
 * Prepend a tree to the children of the root node. If `self` is a _leaf_, it is
 * converted into a branch.
 * @example
 * import * as Tree from 'effect-tree'
 *
 * const tree = Tree.tree(1, [Tree.of(2), Tree.of(3)])
 * const changed = Tree.prepend(tree, Tree.of(4))
 *
 * expect(Tree.getForest(changed)).toEqual([
 *   Tree.of(4),
 *   Tree.of(2),
 *   Tree.of(3),
 * ])
 * @typeParam A Tree underlying type.
 * @param self The tree to modify.
 * @param child Child to prepend.
 * @returns A new updated tree with the new node prepended.
 * @category basic
 * @function
 */
export const prepend: {
  <A>(self: Tree<A>, child: Tree<A>): Branch<A>
  <A>(child: Tree<A>): (self: Tree<A>) => Branch<A>
} = dual(
  2,
  <A>(self: Tree<A>, child: Tree<A>): Branch<A> =>
    isLeaf(self)
      ? pipe(self, getValue, branch([child]))
      : pipe(self, modBranchForest(Array.prepend(child))),
)
 
/**
 * Append a list of trees to the children of the root node. If `self` is a
 * _leaf_, it is converted into a branch.
 * @example
 * import * as Tree from 'effect-tree'
 *
 * const tree = Tree.tree(1, [Tree.of(2), Tree.of(3)])
 * const changed = Tree.appendAll(tree, [Tree.of(4), Tree.of(5)])
 *
 * expect(Tree.getForest(changed)).toEqual([
 *   Tree.of(2),
 *   Tree.of(3),
 *   Tree.of(4),
 *   Tree.of(5),
 * ])
 * @typeParam A Tree underlying type.
 * @param self The tree to modify.
 * @param children A non-empty list of trees to append to the tree.
 * @returns A new updated tree with the new nodes appended.
 * @category basic
 * @function
 */
export const appendAll: {
  <A>(self: Tree<A>, children: Tree<A>[]): Tree<A>
  <A>(children: Tree<A>[]): (self: Tree<A>) => Tree<A>
} = dual(
  2,
  <A>(self: Tree<A>, children: Tree<A>[]): Tree<A> =>
    Array.isNonEmptyArray(children)
      ? isLeaf(self)
        ? pipe(self, setForest(children))
        : pipe(self, modForest(Array.appendAll(children)))
      : self,
)
 
/**
 * Prepend a list of trees to the children of the root node. If `self` is a
 * _leaf_, it is converted into a branch.
 * @example
 * import * as Tree from 'effect-tree'
 *
 * const tree = Tree.tree(1, [Tree.of(2), Tree.of(3)])
 * const changed = Tree.prependAll(tree, [Tree.of(4), Tree.of(5)])
 *
 * expect(Tree.getForest(changed)).toEqual([
 *   Tree.of(4),
 *   Tree.of(5),
 *   Tree.of(2),
 *   Tree.of(3),
 * ])
 * @typeParam A Tree underlying type.
 * @param self The tree to modify.
 * @param children A non-empty list of trees to prepend to the tree.
 * @returns A new updated tree with the new nodes prepended.
 * @category basic
 * @function
 */
export const prependAll: {
  <A>(self: Tree<A>, children: Tree<A>[]): Tree<A>
  <A>(children: Tree<A>[]): (self: Tree<A>) => Tree<A>
} = dual(
  2,
  <A>(self: Tree<A>, children: Tree<A>[]): Tree<A> =>
    Array.isNonEmptyArray(children)
      ? isLeaf(self)
        ? pipe(self, setForest(children))
        : pipe(self, modForest(Array.prependAll(children)))
      : self,
)