Skip to content

Commit

Permalink
refactor(transducers): deprecate hex(), update hexDump() xform & deps
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 8, 2018
1 parent 5864f2c commit b1ea9a5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
5 changes: 3 additions & 2 deletions packages/transducers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"@thi.ng/checks": "^1.5.7",
"@thi.ng/compare": "^0.1.6",
"@thi.ng/equiv": "^0.1.7",
"@thi.ng/errors": "^0.1.6"
"@thi.ng/errors": "^0.1.6",
"@thi.ng/strings": "^0.1.1"
},
"keywords": [
"ES6",
Expand All @@ -48,4 +49,4 @@
"publishConfig": {
"access": "public"
}
}
}
15 changes: 8 additions & 7 deletions packages/transducers/src/func/hex.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const ZEROES = "00000000";
import { radix } from "@thi.ng/strings/radix";

export function hex(digits = 2, prefix = "") {
return (x: number) => {
const s = x.toString(16);
return prefix + (s.length >= digits ? s : ZEROES.substring(0, digits - s.length) + s);
}
}
/**
* @deprecated use thi.ng/strings `radix()` instead
*
* @param digits
* @param prefix
*/
export const hex = (digits = 2, prefix = "") => radix(16, digits, prefix);
7 changes: 4 additions & 3 deletions packages/transducers/src/xform/hex-dump.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { U8, U32 } from "@thi.ng/strings/radix";

import { Transducer } from "../api";

import { comp } from "../func/comp";
import { hex } from "../func/hex";
import { juxt } from "../func/juxt";

import { map } from "./map";
Expand Down Expand Up @@ -29,14 +30,14 @@ import { partition } from "./partition";
export function hexDump(cols = 16, addr = 0): Transducer<number, string> {
return comp(
padLast(cols, 0),
map(juxt(hex(), (x) => x > 31 && x < 128 ? String.fromCharCode(x) : ".")),
map(juxt(U8, (x) => x > 31 && x < 128 ? 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]) => `${hex(8)(addr + i * cols)} | ${h} | ${a}`)
mapIndexed((i, [h, a]) => `${U32(addr + i * cols)} | ${h} | ${a}`)
);
}

0 comments on commit b1ea9a5

Please sign in to comment.