1 |
|
2 | # Node.js pad
|
3 |
|
4 | [![Build Status](https://secure.travis-ci.org/adaltas/node-pad.png)](http://travis-ci.org/adaltas/node-pad)
|
5 |
|
6 | Node Pad is a simple and elegant function to pad strings in both left and right directions.
|
7 |
|
8 | ## Usage
|
9 |
|
10 | ```javascript
|
11 | pad('pad', 5) // "pad "
|
12 | pad(5, 'pad') // " pad"
|
13 | pad('pad', 5, '+') // "pad++"
|
14 | pad(5, 'pad', '+') // "++pad"
|
15 | ```
|
16 |
|
17 | ## Options
|
18 |
|
19 | Options are provided as a third argument and are all optional. A string argument
|
20 | it is interpreted as the "char" option. Accepted options include:
|
21 |
|
22 | * `char` (string)
|
23 | The character used to fill the gap.
|
24 | * `colors` (boolean)
|
25 | Ajust to hidden terminal color characters, you may also use
|
26 | `require 'pad/lib/colors'` to avoid passing this option.
|
27 | * `strip` (boolean)
|
28 | Remove characters from text if length smaller than text length, default to
|
29 | "false".
|
30 |
|
31 | ## Left padding: `pad(length, text, [options])`
|
32 |
|
33 | Left padding occurs when the first argument is a number and the second
|
34 | argument is a string.
|
35 |
|
36 | ```javascript
|
37 | var pad = require('pad');
|
38 | pad(5, 'pad', '-').should.eql('-pad');
|
39 | ```
|
40 |
|
41 | ## Right padding: `pad(text, length, [options])`
|
42 |
|
43 | Right padding occurs when the first argument is a string and the second
|
44 | argument is a number.
|
45 |
|
46 | ```javascript
|
47 | var pad = require('pad');
|
48 | pad('pad', 6).should.eql('pad ');
|
49 | ```
|
50 |
|
51 | ## Installing
|
52 |
|
53 | Via [npm](http://github.com/isaacs/npm):
|
54 |
|
55 | ```bash
|
56 | npm install pad
|
57 | ```
|
58 |
|
59 | Via git (or downloaded tarball), copy or link the project from a discoverable Node directory:
|
60 |
|
61 | ```bash
|
62 | git clone http://github.com/wdavidw/node-pad.git
|
63 | ```
|
64 |
|
65 | ## Testing
|
66 |
|
67 | Clone the repo, install the development dependencies and run the suite:
|
68 |
|
69 | ```bash
|
70 | git clone http://github.com/wdavidw/node-pad.git .
|
71 | npm install
|
72 | make test
|
73 | ```
|