UNPKG

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