Skip to content

Commit

Permalink
refactor(iges): update to use @thi.ng/strings
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 8, 2018
1 parent 40781eb commit df067d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/iges/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@thi.ng/api": "^4.0.6",
"@thi.ng/defmulti": "^0.3.7",
"@thi.ng/strings": "^0.0.1",
"@thi.ng/transducers": "^1.15.0"
},
"keywords": [
Expand All @@ -44,4 +45,4 @@
"publishConfig": {
"access": "public"
}
}
}
30 changes: 10 additions & 20 deletions packages/iges/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { defmulti } from "@thi.ng/defmulti";
import { float } from "@thi.ng/strings/float";
import { padLeft } from "@thi.ng/strings/pad-left";
import { padRight } from "@thi.ng/strings/pad-right";
import {
comp,
iterator,
Expand All @@ -12,7 +15,6 @@ import {
wordWrap,
wrap
} from "@thi.ng/transducers";

import {
DEFAULT_GLOBALS,
DictEntry,
Expand All @@ -28,25 +30,13 @@ import {
const LINEWIDTH_GLOBALS = 72;
const LINEWIDTH_PARAMS = 64;

const padl = (n: number, ch: string) => {
const buf = new Array(n).fill(ch).join("");
return (x: any) => (x = x.toString(), x.length < n ? buf.substr(x.length) + x : x);
};

const padr = (n: number, ch: string) => {
const buf = new Array(n).fill(ch).join("");
return (x: any) => (x = x.toString(), x.length < n ? x + buf.substr(x.length) : x);
};

const ff = (prec = 3) => (x: number) => x.toFixed(prec);

const $Z2 = padl(2, "0");
const $Z7 = padl(7, "0");
const $F8 = padl(8, " ");
const $Z2 = padLeft(2, "0");
const $Z7 = padLeft(7, "0");
const $F8 = padLeft(8, " ");
const $STR = (x: any) => (x != null ? (x = x.toString(), `${x.length}H${x}`) : "");
const $SEQ = padl(7, " ");
const $BODY = padr(72, " ");
const $PBODY = padr(64, " ");
const $SEQ = padLeft(7, " ");
const $BODY = padRight(72, " ");
const $PBODY = padRight(64, " ");
const $DATE = (d: Date) =>
$STR([
d.getUTCFullYear(),
Expand All @@ -60,7 +50,7 @@ const $DATE = (d: Date) =>

export const newDocument = (g?: Partial<GlobalParams>, start?: string[]): IGESDocument => {
const globals = <GlobalParams>{ ...DEFAULT_GLOBALS, ...g };
const $FF = ff(globals.precision);
const $FF = float(globals.precision);
const $PARAM = defmulti<any[], string>((x) => x[1]);
$PARAM.add(Type.INT, (x) => x[0].toString());
$PARAM.add(Type.FLOAT, (x) => $FF(x[0]));
Expand Down

0 comments on commit df067d8

Please sign in to comment.