All files / src/codec/paths ops.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 1/1
100% Lines 13/13

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 391x 1x 1x 1x 1x               1x 1x                           1x         1x 246x 88x   1x 1x  
import {treeAna, treeCata, type Tree} from '#tree'
import {Number, Order, pipe, String} from 'effect'
import {type NonEmptyArray2} from '#Array'
import {pathListFold} from './fold.js'
import {pathListUnfold} from './unfold.js'
 
/**
 * Encode a tree of type `A` into a non-empty list of paths to every leaf in the
 * encoded tree.
 * @category codec
 * @function
 */
export const encode: <A>(tree: Tree<A>) => NonEmptyArray2<A> =
  treeCata(pathListFold)
 
/**
 * Decode a list of leaf paths into a tree where. Every leaf given will appear
 * in the tree at the given path.
 *
 * An [Order](https://effect.website/docs/behaviour/order/) for your type is
 * required as the first argument.
 *
 * Under the keys `string` and `number` you will find version for string and
 * numeric trees that already provide the required order.
 * @category codec
 * @function
 */
export const decode: {
  <A>(order: Order.Order<A>): (pathList: NonEmptyArray2<A>) => Tree<A>
  number: (pathList: NonEmptyArray2<number>) => Tree<number>
  string: (pathList: NonEmptyArray2<string>) => Tree<string>
} =
  <A>(order: Order.Order<A>) =>
  (pathList: NonEmptyArray2<A>): Tree<A> =>
    pipe(pathList, treeAna(pathListUnfold(order)))
 
decode.number = decode(Number.Order)
decode.string = decode(String.Order)