A small library for padding strings in JavaScript. Marmalade-free.
paddington.pad('foo', 5, '_'); // _foo_
paddington.left('foo', 5, '_'); // __foo
paddington.right('foo', 5, '_'); // foo__
Paddington requires the following to run:
Paddington is easiest to use when installed with npm:
npm install paddington
Then you can load the module into your code with a require
call:
var paddington = require('paddington');
The paddington
object has the following methods.
Pad a string, distributing the padding equally on the left and right.
string
is the string we want to pad (String).
length
is the length we want to pad it to (Number).
character
is an optional character to pad with (String, defaults to " "
).
return
is the padded string (String).
// Example
paddington.pad('foo', 5); // returns " foo "
paddington.pad('foo', 5, '_'); // returns "_foo_"
Pad a string on the left hand side. This method has the same signature as paddington.pad
.
// Example
paddington.left('foo', 5); // returns " foo"
paddington.left('foo', 5, '_'); // returns "__foo"
Pad a string on the right hand side. This method has the same signature as paddington.pad
.
// Example
paddington.right('foo', 5); // returns "foo "
paddington.right('foo', 5, '_'); // returns "foo__"
When a string is longer than the specified pad length, it will not be trimmed. In this case the string will be returned as-is:
paddington.pad('foobar', 5); // returns "foobar"
All of the methods documented above will throw a TypeError
if an argument is not of the expected type.
To contribute to Paddington, clone this repo locally and commit your code on a separate branch. Please write unit tests for your code, and run the linter before opening a pull-request:
make test # run all unit tests
make lint # run the linter
You can find more detail in our contributing guide. Participation in this open source project is subject to a Code of Conduct.
Paddington major versions are normally supported for 6 months after their last minor release. This means that patch-level changes will be added and bugs will be fixed. The table below outlines the end-of-support dates for major versions, and the last minor release for that version.
We also maintain a migration guide to help you migrate.
❔ | Major Version | Last Minor Release | Support End Date |
---|---|---|---|
❤️ | 3 | N/A | N/A |
⌛ | 2 | 2.1 | 2016-07-04 |
🚫 | 1 | 1.4 | 2015-01-26 |
If you're opening issues related to these, please mention the version that the issue relates to.
Paddington is licensed under the MIT license.
Copyright © 2016, Rowan Manning