All files / src/codec Isomorphism.ts

100% Statements 27/27
100% Branches 3/3
100% Functions 3/3
100% Lines 27/27

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 751x 1x 1x     1x         1x       1x 1x                                         1x 3x 3x 3x             1x 2x 2x 2x 2x 2x             1x 1x 1x 1x             1x 2x 2x 2x 2x 2x  
import {type Branch, type Tree} from '#tree'
import {Array} from '#util'
import {Number, type Order} from 'effect'
import type {Isomorphism} from 'effect-ts-laws/typeclass'
import type {NonEmptyArray} from 'effect/Array'
import {
  decode as decodeEdges,
  encode as encodeEdges,
  type EdgeList,
} from './edges.js'
import {
  decode as decodeIndented,
  encode as encodeIndented,
} from './indented/index.js'
import {decode as decodePaths, encode as encodePaths} from './paths.js'
import {decode as decodePrufer, encode as encodePrufer} from './prufer.js'
 
/**
 * An isomorphism between a tree of underlying type `A` and the type `B`.
 * @category codec
 * @category instances
 */
export type TreeIsomorphism<A, B> = Isomorphism.Isomorphism<Tree<A>, B>
 
/**
 * Encode/decode losslessly tree ↔ edge list.
 * @category codec
 * @category instances
 */
export type BranchIsomorphism<A, B> = Isomorphism.Isomorphism<Branch<A>, B>
 
/**
 * Encode/decode losslessly tree ↔ edge list.
 * @category codec
 * @category instances
 */
export const EdgeListIsomorphism = <A>(): TreeIsomorphism<A, EdgeList<A>> => ({
  to: encodeEdges,
  from: decodeEdges,
})
 
/**
 * Encode/decode losslessly tree ↔ nested arrays.
 * @category codec
 * @category instances
 */
export const ArraysIsomorphism = <A>(
  order: Order.Order<A>,
): TreeIsomorphism<A, Array.NonEmptyArray2<A>> => ({
  to: encodePaths,
  from: decodePaths(order),
})
 
/**
 * Encode/decode losslessly a numeric tree ↔ prüfer code.
 * @category codec
 * @category instances
 */
export const PruferIsomorphism: BranchIsomorphism<number, number[]> = {
  to: encodePrufer(Number.Order),
  from: decodePrufer,
}
 
/**
 * Encode/decode losslessly a string tree ↔ indented tree.
 * @category codec
 * @category instances
 */
export const IndentedIsomorphism = (
  indent: number,
): TreeIsomorphism<string, NonEmptyArray<string>> => ({
  to: encodeIndented.curried(indent),
  from: decodeIndented,
})