Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | "use strict"; |
Tim van der Lippe | 3820890 | 2021-05-11 16:37:59 +0100 | [diff] [blame^] | 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"); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 9 | const alignLeft = (subject, width) => { |
Tim van der Lippe | 3820890 | 2021-05-11 16:37:59 +0100 | [diff] [blame^] | 10 | return subject + ' '.repeat(width); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 11 | }; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 12 | const alignRight = (subject, width) => { |
Tim van der Lippe | 3820890 | 2021-05-11 16:37:59 +0100 | [diff] [blame^] | 13 | return ' '.repeat(width) + subject; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 14 | }; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 15 | const alignCenter = (subject, width) => { |
Tim van der Lippe | 3820890 | 2021-05-11 16:37:59 +0100 | [diff] [blame^] | 16 | return ' '.repeat(Math.floor(width / 2)) + subject + ' '.repeat(Math.ceil(width / 2)); |
| 17 | }; |
| 18 | const alignJustify = (subject, width) => { |
| 19 | const spaceSequenceCount = utils_1.countSpaceSequence(subject); |
| 20 | if (spaceSequenceCount === 0) { |
| 21 | return alignLeft(subject, width); |
| 22 | } |
| 23 | const addingSpaces = 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 | }); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 31 | }; |
| 32 | /** |
| 33 | * Pads a string to the left and/or right to position the subject |
| 34 | * text in a desired alignment within a container. |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 35 | */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 36 | const alignString = (subject, containerWidth, alignment) => { |
Tim van der Lippe | 3820890 | 2021-05-11 16:37:59 +0100 | [diff] [blame^] | 37 | const subjectWidth = 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); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 58 | }; |
Tim van der Lippe | 3820890 | 2021-05-11 16:37:59 +0100 | [diff] [blame^] | 59 | exports.alignString = alignString; |