Simple and elegant function to pad strings in both left and right directions

pad("Hello", 8, ".")
pad(8, "pad!", ".")
Arrow right
Arrow right
Hello...
....pad!
NPM versionTravis build status

Key features

Simple APISimple API
Light weightLight weight, 1 dependency
CJS, ESM and UMD bundlesCJS, ESM and UMD bundles
Documented and testedDocumented and tested
Well tested, matureWell tested, mature
MIT LicenseMIT License

Installing

Starting with version 1.1.0, Node.js PAD rely on Node.js 4.0.0 or more recent. Stick to version 1.0.x if using an older version of Node.js.

Via npm:

npm install pad

Via git (or downloaded tarball), copy or link the project from a discoverable Node.js directory:

git clone http://github.com/wdavidw/node-pad.git

Getting started

The API is quite simple:

const pad = require('pad')
pad('pad', 5)      // "pad  "
pad(5, 'pad')      // "  pad"
pad('pad', 5, '+') // "pad++"
pad(5, 'pad', '+') // "++pad"

For TypeScript users, the type definition file is located in "./lib/index.d.ts" and declared inside the "package.json" file.

Bundles in multiple formats are available: CommonJS, ESM and UMD. Node.js and ESM-aware tools will automatically discover the appropriate bundle format from the `package.json` declaration file.