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 | The API is quite simple:
|
11 |
|
12 | ```javascript
|
13 | const pad = require('pad')
|
14 | pad('pad', 5) // "pad "
|
15 | pad(5, 'pad') // " pad"
|
16 | pad('pad', 5, '+') // "pad++"
|
17 | pad(5, 'pad', '+') // "++pad"
|
18 | ```
|
19 |
|
20 | For TypeScript users, the type definition file is located in "./lib/index.d.ts"
|
21 | and declared inside the "package.json" file.
|
22 |
|
23 | This package is written for ES6 supported by Node.js version 7.5 and above. For
|
24 | older browsers or older versions of Node.js, use the modules inside "./lib/es5".
|
25 |
|
26 | ```javascript
|
27 | const pad = require('pad/lib/es5')
|
28 | pad('pad', 5) // "pad "
|
29 | ```
|
30 |
|
31 | ## Options
|
32 |
|
33 | Options are provided as a third argument and are all optional. A string argument
|
34 | it is interpreted as the "char" option. Accepted options include:
|
35 |
|
36 | * `char` (string)
|
37 | The character used to fill the gap.
|
38 | * `colors` (boolean)
|
39 | Ajust to hidden terminal color characters, you may also use
|
40 | `require 'pad/lib/colors'` to avoid passing this option.
|
41 | * `strip` (boolean)
|
42 | Remove characters from text if length smaller than text length, default to
|
43 | "false".
|
44 | * `fixed_width` (boolean)
|
45 | An optimization option to disable the usage of the wcwdith package to handle
|
46 | the discovery of characters using more than one column for display.
|
47 | one column to display
|
48 | * `wcwidth_options` (object)
|
49 | Options passed to the wcwidth package used to calculate the display width of
|
50 | characters using more than one column.
|
51 |
|
52 | ## Left padding: `pad(length, text, [options])`
|
53 |
|
54 | Left padding occurs when the first argument is a number and the second
|
55 | argument is a string.
|
56 |
|
57 | ```javascript
|
58 | var pad = require('pad');
|
59 | pad(5, 'pad', '-').should.eql('--pad');
|
60 | ```
|
61 |
|
62 | ## Right padding: `pad(text, length, [options])`
|
63 |
|
64 | Right padding occurs when the first argument is a string and the second
|
65 | argument is a number.
|
66 |
|
67 | ```javascript
|
68 | var pad = require('pad');
|
69 | pad('pad', 6).should.eql('pad ');
|
70 | ```
|
71 |
|
72 | ## Installing
|
73 |
|
74 | Starting with version 1.1.0, Node pad rely on Node.js 4.0.0 or more recent.
|
75 | Stick to version 1.0.x if using an older version of Node.js.
|
76 |
|
77 | Via [npm](http://github.com/isaacs/npm):
|
78 |
|
79 | ```bash
|
80 | npm install pad
|
81 | ```
|
82 |
|
83 | Via git (or downloaded tarball), copy or link the project from a discoverable
|
84 | Node.js directory:
|
85 |
|
86 | ```bash
|
87 | git clone http://github.com/wdavidw/node-pad.git
|
88 | ```
|
89 |
|
90 | ## Testing
|
91 |
|
92 | Clone the repo, install the development dependencies and run the suite:
|
93 |
|
94 | ```bash
|
95 | git clone http://github.com/wdavidw/node-pad.git .
|
96 | npm install
|
97 | make test
|
98 | ```
|