import { juxt } from "@thi.ng/compose/juxt" ; import { U32 , U8 } from "@thi.ng/hex" ; import type { Transducer } from "@thi.ng/transducers" ; import { comp } from "@thi.ng/transducers/comp" ; import { iterator , __iter } from "@thi.ng/transducers/iterator" ; import { map } from "@thi.ng/transducers/map" ; import { mapIndexed } from "@thi.ng/transducers/map-indexed" ; import { padLast } from "@thi.ng/transducers/pad-last" ; import { partition } from "@thi.ng/transducers/partition" ; import type { HexDumpOpts } from "./api.js" ; /**
 * Transforms bytes into a sequence of hexdump lines with configurable
 * number of `columns` and `address` offset. Uses
 *  {@link @thi.ng/transducers#(partition:1)} internally, so new lines
 * are only produced once filled. If the input hasn't an exact multiple
 * of `cols` bytes, the last line will be padded with zeroes.
 *
 *  example param opts param src export function hexDump ( opts ? ) ; export function hexDump ( src ) ; export function hexDump ( opts , src ) ; export function hexDump ( ... args ) { const iter = __iter ( hexDump , args , iterator ) ; if ( iter ) { return iter ; } const { cols , address } = < HexDumpOpts > { cols : 16 , address : 0 , ... args [ 0 ] } ; return comp ( padLast ( cols , 0 ) , map ( juxt ( U8 , ( x ) => ( x > 31 && x < 127 ? String . fromCharCode ( x ) : "." ) ) ) , partition ( cols , true ) , map ( juxt ( ( x ) => x . map ( ( y ) => y [ 0 ] ) . join ( " " ) , ( x ) => x . map ( ( y ) => y [ 1 ] ) . join ( "" ) ) ) , mapIndexed ( ( i , [ h , a ] ) => `${ U32 ( address + i * cols ) } | ${ h } | ${ a }` ) ) ; } /**
 * Convenience wrapper for  hexDump , return the hexdump as a
 * single result string.
 *
 *  param opts param src export const hexDumpString = ( opts , src ) => [ ... hexDump ( opts , src ) ] . join ( "\n" ) ;