UNPKG

1.45 kBJavaScriptView Raw
1import { hasAnsi } from '@spare/charset';
2import { lange } from '@spare/lange';
3import { isNumeric } from '@typen/num-strict';
4import { FullWidth } from '@spare/fullwidth';
5
6const ansiPadLength = (tx, pd) => hasAnsi(tx) ? tx.length + pd - lange(tx) : pd;
7const lpad = String.prototype.padStart;
8const rpad = String.prototype.padEnd;
9
10const LPad = ({
11 ansi = true,
12 fill
13} = {}) => ansi ? (tx, pd) => lpad.call(tx, ansiPadLength(tx, pd), fill) : (tx, pd) => lpad.call(tx, pd, fill);
14
15const RPad = ({
16 ansi = true,
17 fill
18} = {}) => ansi ? (tx, pd) => rpad.call(tx, ansiPadLength(tx, pd), fill) : (tx, pd) => rpad.call(tx, pd, fill);
19
20const Pad = ({
21 dock,
22 ansi = true,
23 fill
24} = {}) => {
25 if (!dock) {
26 return ansi ? (tx, pd, v) => (isNumeric(v) ? lpad : rpad).call(tx, ansiPadLength(tx, pd), fill) : (tx, pd, v) => (isNumeric(v) ? lpad : rpad).call(tx, pd, fill);
27 }
28
29 let padder = dock < 0 ? lpad : rpad;
30 return ansi ? (tx, pd) => padder.call(tx, ansiPadLength(tx, pd), fill) : (tx, pd) => padder.call(tx, pd, fill);
31};
32
33const PadFW = ({
34 dock,
35 ansi,
36 fill,
37 fwfill
38}) => {
39 const padHW = Pad({
40 dock,
41 ansi,
42 fill
43 }),
44 padFW = Pad({
45 dock,
46 ansi,
47 fill: fwfill
48 }),
49 toFW = FullWidth({
50 ansi
51 });
52 return (x, pd, fw, v) => fw ? padFW(toFW(x), pd, v) : padHW(x, pd, v);
53};
54
55const LEFT = -1;
56const RIGHT = 1;
57const CENTRE = 0;
58
59export { CENTRE, LEFT, LPad, Pad, PadFW, RIGHT, RPad };