1# align-text [![NPM version](https://badge.fury.io/js/align-text.svg)](http://badge.fury.io/js/align-text)  [![Build Status](https://travis-ci.org/jonschlinkert/align-text.svg)](https://travis-ci.org/jonschlinkert/align-text)
2
3> Align the text in a string.
4
5**Examples**
6
7Align text values in an array:
8
9```js
10align([1, 2, 3, 100]);
11//=> ['  1', '  2', '  3', '100']
12```
13
14Or [do stuff like this](./example.js):
15
16[![screen shot 2015-06-09 at 2 08 34 am](https://cloud.githubusercontent.com/assets/383994/8051597/7b716fbc-0e4c-11e5-9aef-4493fd22db58.png)](./example.js)
17
18Visit [the example](./example.js) to see how this works.
19
20## Install
21
22Install with [npm](https://www.npmjs.com/)
23
24```sh
25$ npm i align-text --save
26```
27
28## Usage
29
30```js
31var align = require('align-text');
32align(text, callback_function_or_integer);
33```
34
35**Params**
36
37* `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.
38* `callback|integer`: if an integer, the text will be indented by that amount. If a function, it must return an integer representing the amount of leading indentation to use as `align` loops over each line.
39
40**Example**
41
42```js
43align(text, 4);
44```
45
46Would align:
47
48```
49abc
50abc
51abc
52```
53
54To:
55
56```
57    abc
58    abc
59    abc
60```
61
62## callback
63
64### params
65
66The callback is used to determine the indentation of each line and gets the following params:
67
68* `len` the length of the "current" line
69* `longest` the length of the longest line
70* `line` the current line (string) being aligned
71* `lines` the array of all lines
72
73### return
74
75The callback may return:
76
77* an integer that represents the number of spaces to use for padding,
78* or an object with the following properties:
79  - `indent`: **{Number}** the amount of indentation to use. Default is `0` when an object is returned.
80  - `character`: **{String}** the character to use for indentation. Default is `''` (empty string) when an object is returned.
81  - `prefix`: **{String}** leading characters to use at the beginning of each line. `''` (empty string) when an object is returned.
82
83**Integer example:**
84
85```js
86// calculate half the difference between the length
87// of the current line and the longest line
88function centerAlign(len, longest, line, lines) {
89  return Math.floor((longest - len) / 2);
90}
91```
92
93**Object example:**
94
95```js
96function centerAlign(len, longest, line, lines) {
97  return {
98    character: '\t',
99    indent: Math.floor((longest - len) / 2),
100    prefix: '~ ',
101  }
102}
103```
104
105## Usage examples
106
107### Center align
108
109Using the `centerAlign` function from above:
110
111```js
112align(text, centerAlign);
113```
114
115Would align this text:
116
117```js
118Lorem ipsum dolor sit amet
119consectetur adipiscin
120elit, sed do eiusmod tempor incididun
121ut labore et dolor
122magna aliqua. Ut enim ad mini
123veniam, quis
124```
125
126Resulting in this:
127
128```
129     Lorem ipsum dolor sit amet,
130        consectetur adipiscing
131elit, 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
139If you wanted to add more padding on the left, just pass the number in the callback.
140
141For example, to add 4 spaces before every line:
142
143```js
144function centerAlign(len, longest, line, lines) {
145  return 4 + Math.floor((longest - len) / 2);
146}
147```
148
149Would 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
163align(text, function (len, max, line, lines) {
164  return {prefix: ' - '};
165});
166```
167
168Would return:
169
170```
171- Lorem ipsum dolor sit amet,
172- consectetur adipiscing
173- elit, sed do eiusmod tempor incididunt
174- ut labore et dolore
175- magna aliqua. Ut enim ad minim
176- veniam, quis
177```
178
179### Different indent character
180
181```js
182align(text, function (len, max, line, lines) {
183  return {
184    indent: Math.floor((max - len) / 2),
185    character: '~',
186  };
187});
188```
189
190Would return
191
192```
193~~~~~Lorem ipsum dolor sit amet,
194~~~~~~~~consectetur adipiscing
195elit, sed do eiusmod tempor incididunt
196~~~~~~~~~ut labore et dolore
197~~~~magna aliqua. Ut enim ad minim
198~~~~~~~~~~~~~veniam, quis
199```
200
201## Related projects
202
203* [center-align](https://github.com/jonschlinkert/center-align): Center-align the text in a string.
204* [justify](https://github.com/bahamas10/node-justify): Left or right (or both) justify text using a custom width and character
205* [longest](https://github.com/jonschlinkert/longest): Get the longest item in an array.
206* [right-align](https://github.com/jonschlinkert/right-align): Right-align the text in a string.
207* [repeat-string](https://github.com/jonschlinkert/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string.
208* [word-wrap](https://github.com/jonschlinkert/word-wrap): Wrap words to a specified length.
209
210## Running tests
211
212Install dev dependencies:
213
214```sh
215$ npm i -d && npm test
216```
217
218## Contributing
219
220Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/align-text/issues/new)
221
222## Author
223
224**Jon Schlinkert**
225
226+ [github/jonschlinkert](https://github.com/jonschlinkert)
227+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
228
229## License
230
231Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
232Released under the MIT license.
233
234***
235
236_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on June 09, 2015._
237