1 | # align-text [![NPM version](https://img.shields.io/npm/v/align-text.svg?style=flat)](https://www.npmjs.com/package/align-text) [![NPM monthly downloads](https://img.shields.io/npm/dm/align-text.svg?style=flat)](https://npmjs.org/package/align-text) [![NPM total downloads](https://img.shields.io/npm/dt/align-text.svg?style=flat)](https://npmjs.org/package/align-text) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/align-text.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/align-text)
|
2 |
|
3 | > Align the text in a string.
|
4 |
|
5 | Follow this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), for updates on this project and others.
|
6 |
|
7 | ## Install
|
8 |
|
9 | Install with [npm](https://www.npmjs.com/):
|
10 |
|
11 | ```sh
|
12 | $ npm install --save align-text
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ```js
|
18 | var align = require('align-text');
|
19 |
|
20 | align(text, function_or_integer);
|
21 | ```
|
22 |
|
23 | **Params**
|
24 |
|
25 | * `text` can be a **string or array**. If a string is passed, a string will be returned. If an array is passed, an array will be returned.
|
26 | * `function|integer`: if an integer, the text will be indented by that amount. If a transform function is passed, it must return an object with an `integer` property or an integer representing the amount of leading indentation to use as `align` loops over each line.
|
27 |
|
28 | **Example**
|
29 |
|
30 | ```js
|
31 | align(text, 4);
|
32 | ```
|
33 |
|
34 | Would align:
|
35 |
|
36 | ```
|
37 | abc
|
38 | abc
|
39 | abc
|
40 | ```
|
41 | To:
|
42 |
|
43 | ```
|
44 | abc
|
45 | abc
|
46 | abc
|
47 | ```
|
48 |
|
49 | ## Transform function
|
50 |
|
51 | ### params
|
52 |
|
53 | The callback is used to determine the indentation of each line and gets the following params:
|
54 |
|
55 | * `len` the length of the "current" line
|
56 | * `longest` the length of the longest line
|
57 | * `line` the current line (string) being aligned
|
58 | * `lines` the array of all lines
|
59 |
|
60 | ### return
|
61 |
|
62 | The callback may return:
|
63 |
|
64 | * an integer that represents the number of spaces to use for padding,
|
65 | * or an object with the following properties:
|
66 | - `indent`: **{Number}** the amount of indentation to use. Default is `0` when an object is returned.
|
67 | - `character`: **{String}** the character to use for indentation. Default is `''` (empty string) when an object is returned.
|
68 | - `prefix`: **{String}** leading characters to use at the beginning of each line. `''` (empty string) when an object is returned.
|
69 |
|
70 | **Integer example:**
|
71 |
|
72 | ```js
|
73 | // calculate half the difference between the length
|
74 | // of the current line and the longest line
|
75 | function centerAlign(len, longest, line, lines) {
|
76 | return Math.floor((longest - len) / 2);
|
77 | }
|
78 | ```
|
79 |
|
80 | **Object example:**
|
81 |
|
82 | ```js
|
83 | function centerAlign(len, longest, line, lines) {
|
84 | return {
|
85 | character: '\t',
|
86 | indent: Math.floor((longest - len) / 2),
|
87 | prefix: '~ ',
|
88 | }
|
89 | }
|
90 | ```
|
91 |
|
92 | ## Usage examples
|
93 |
|
94 | Align text values in an array:
|
95 |
|
96 | ```js
|
97 | align([1, 2, 3, 100]);
|
98 | //=> [' 1', ' 2', ' 3', '100']
|
99 | ```
|
100 |
|
101 | Or [do stuff like this](./example.js):
|
102 |
|
103 | ![screen shot 2015-06-09 at 2 08 34 am](https://cloud.githubusercontent.com/assets/383994/8051597/7b716fbc-0e4c-11e5-9aef-4493fd22db58.png)
|
104 |
|
105 | Visit [the example](./example.js) to see how this works.
|
106 |
|
107 | ### Center align
|
108 |
|
109 | Using the `centerAlign` function from above:
|
110 |
|
111 | ```js
|
112 | align(text, centerAlign);
|
113 | ```
|
114 |
|
115 | Would align this text:
|
116 |
|
117 | ```js
|
118 | Lorem ipsum dolor sit amet
|
119 | consectetur adipiscin
|
120 | elit, sed do eiusmod tempor incididun
|
121 | ut labore et dolor
|
122 | magna aliqua. Ut enim ad mini
|
123 | veniam, quis
|
124 | ```
|
125 |
|
126 | Resulting in this:
|
127 |
|
128 | ```
|
129 | Lorem ipsum dolor sit amet,
|
130 | consectetur adipiscing
|
131 | elit, sed do eiusmod tempor incididunt
|
132 | ut labore et dolore
|
133 | magna aliqua. Ut enim ad minim
|
134 | veniam, quis
|
135 | ```
|
136 |
|
137 | **Customize**
|
138 |
|
139 | If you wanted to add more padding on the left, just pass the number in the callback.
|
140 |
|
141 | For example, to add 4 spaces before every line:
|
142 |
|
143 | ```js
|
144 | function centerAlign(len, longest, line, lines) {
|
145 | return 4 + Math.floor((longest - len) / 2);
|
146 | }
|
147 | ```
|
148 |
|
149 | Would result in:
|
150 |
|
151 | ```
|
152 | Lorem ipsum dolor sit amet,
|
153 | consectetur adipiscing
|
154 | elit, sed do eiusmod tempor incididunt
|
155 | ut labore et dolore
|
156 | magna aliqua. Ut enim ad minim
|
157 | veniam, quis
|
158 | ```
|
159 |
|
160 | ### Bullets
|
161 |
|
162 | ```js
|
163 | align(text, function (len, max, line, lines) {
|
164 | return {prefix: ' - '};
|
165 | });
|
166 | ```
|
167 | Would return:
|
168 |
|
169 | ```
|
170 | - Lorem ipsum dolor sit amet,
|
171 | - consectetur adipiscing
|
172 | - elit, sed do eiusmod tempor incididunt
|
173 | - ut labore et dolore
|
174 | - magna aliqua. Ut enim ad minim
|
175 | - veniam, quis
|
176 | ```
|
177 |
|
178 | ### Different indent character
|
179 |
|
180 | ```js
|
181 | align(text, function (len, max, line, lines) {
|
182 | return {
|
183 | indent: Math.floor((max - len) / 2),
|
184 | character: '~',
|
185 | };
|
186 | });
|
187 | ```
|
188 | Would return
|
189 |
|
190 | ```
|
191 | ~~~~~Lorem ipsum dolor sit amet,
|
192 | ~~~~~~~~consectetur adipiscing
|
193 | elit, sed do eiusmod tempor incididunt
|
194 | ~~~~~~~~~ut labore et dolore
|
195 | ~~~~magna aliqua. Ut enim ad minim
|
196 | ~~~~~~~~~~~~~veniam, quis
|
197 | ```
|
198 |
|
199 | ## About
|
200 |
|
201 | ### Contributing
|
202 |
|
203 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
204 |
|
205 | ### Contributors
|
206 |
|
207 | | **Commits** | **Contributor** |
|
208 | | --- | --- |
|
209 | | 14 | [jonschlinkert](https://github.com/jonschlinkert) |
|
210 | | 2 | [shinnn](https://github.com/shinnn) |
|
211 |
|
212 | ### Building docs
|
213 |
|
214 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
|
215 |
|
216 | To generate the readme, run the following command:
|
217 |
|
218 | ```sh
|
219 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
220 | ```
|
221 |
|
222 | ### Running tests
|
223 |
|
224 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
|
225 |
|
226 | ```sh
|
227 | $ npm install && npm test
|
228 | ```
|
229 |
|
230 | ### Author
|
231 |
|
232 | **Jon Schlinkert**
|
233 |
|
234 | * [github/jonschlinkert](https://github.com/jonschlinkert)
|
235 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
236 |
|
237 | ### License
|
238 |
|
239 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
240 | Released under the [MIT License](LICENSE).
|
241 |
|
242 | ***
|
243 |
|
244 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 13, 2017._ |
\ | No newline at end of file |