UNPKG

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