1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.alignString = void 0;
|
7 | const string_width_1 = __importDefault(require("string-width"));
|
8 | const utils_1 = require("./utils");
|
9 | const alignLeft = (subject, width) => {
|
10 | return subject + ' '.repeat(width);
|
11 | };
|
12 | const alignRight = (subject, width) => {
|
13 | return ' '.repeat(width) + subject;
|
14 | };
|
15 | const alignCenter = (subject, width) => {
|
16 | return ' '.repeat(Math.floor(width / 2)) + subject + ' '.repeat(Math.ceil(width / 2));
|
17 | };
|
18 | const alignJustify = (subject, width) => {
|
19 | const spaceSequenceCount = (0, utils_1.countSpaceSequence)(subject);
|
20 | if (spaceSequenceCount === 0) {
|
21 | return alignLeft(subject, width);
|
22 | }
|
23 | const addingSpaces = (0, utils_1.distributeUnevenly)(width, spaceSequenceCount);
|
24 | if (Math.max(...addingSpaces) > 3) {
|
25 | return alignLeft(subject, width);
|
26 | }
|
27 | let spaceSequenceIndex = 0;
|
28 | return subject.replace(/\s+/g, (groupSpace) => {
|
29 | return groupSpace + ' '.repeat(addingSpaces[spaceSequenceIndex++]);
|
30 | });
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const alignString = (subject, containerWidth, alignment) => {
|
37 | const subjectWidth = (0, string_width_1.default)(subject);
|
38 | if (subjectWidth === containerWidth) {
|
39 | return subject;
|
40 | }
|
41 | if (subjectWidth > containerWidth) {
|
42 | throw new Error('Subject parameter value width cannot be greater than the container width.');
|
43 | }
|
44 | if (subjectWidth === 0) {
|
45 | return ' '.repeat(containerWidth);
|
46 | }
|
47 | const availableWidth = containerWidth - subjectWidth;
|
48 | if (alignment === 'left') {
|
49 | return alignLeft(subject, availableWidth);
|
50 | }
|
51 | if (alignment === 'right') {
|
52 | return alignRight(subject, availableWidth);
|
53 | }
|
54 | if (alignment === 'justify') {
|
55 | return alignJustify(subject, availableWidth);
|
56 | }
|
57 | return alignCenter(subject, availableWidth);
|
58 | };
|
59 | exports.alignString = alignString;
|
60 |
|
\ | No newline at end of file |