UNPKG

2.63 kBMarkdownView Raw
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
6Node Pad is a simple and elegant function to pad strings in both left and right directions.
7
8## Usage
9
10The API is quite simple:
11
12```javascript
13const pad = require('pad')
14pad('pad', 5) // "pad "
15pad(5, 'pad') // " pad"
16pad('pad', 5, '+') // "pad++"
17pad(5, 'pad', '+') // "++pad"
18```
19
20For TypeScript users, the type definition file is located in "./lib/index.d.ts"
21and declared inside the "package.json" file.
22
23This package is written for ES6 supported by Node.js version 7.5 and above. For
24older browsers or older versions of Node.js, use the modules inside "./lib/es5".
25
26```javascript
27const pad = require('pad/lib/es5')
28pad('pad', 5) // "pad "
29```
30
31## Options
32
33Options are provided as a third argument and are all optional. A string argument
34it 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
54Left padding occurs when the first argument is a number and the second
55argument is a string.
56
57```javascript
58var pad = require('pad');
59pad(5, 'pad', '-').should.eql('--pad');
60```
61
62## Right padding: `pad(text, length, [options])`
63
64Right padding occurs when the first argument is a string and the second
65argument is a number.
66
67```javascript
68var pad = require('pad');
69pad('pad', 6).should.eql('pad ');
70```
71
72## Installing
73
74Starting with version 1.1.0, Node pad rely on Node.js 4.0.0 or more recent.
75Stick to version 1.0.x if using an older version of Node.js.
76
77Via [npm](http://github.com/isaacs/npm):
78
79```bash
80npm install pad
81```
82
83Via git (or downloaded tarball), copy or link the project from a discoverable
84Node.js directory:
85
86```bash
87git clone http://github.com/wdavidw/node-pad.git
88```
89
90## Testing
91
92Clone the repo, install the development dependencies and run the suite:
93
94```bash
95git clone http://github.com/wdavidw/node-pad.git .
96npm install
97make test
98```