Home

Awesome

Node.js pad

Build Status

Node Pad is a simple and elegant function to pad strings in both left and right directions. It is written in Typescript and it support both CommonJS and ESM.

Usage

The API is quite simple:

import pad from "pad";
// Or 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 files are located in "./lib/index.d.ts" and declared inside the "package.json" file.

Bundles

Node Pad comes in multiple flavours depending on your target environment:

The CommonJS syntax to import Node Pad is:

const pad = require("pad/dist/pad.cjs.js");
// Or simply
const pad = require("pad");

While the ES Modules syntax is:

import pad from "pad/dist/pad.esm.js";
// Or for ESM-aware tools
import pad from "pad";

Options

Options are provided as a third argument and are all optional. A string argument it is interpreted as the "char" option. Accepted options include:

Left padding: pad(length, text, [options])

Left padding occurs when the first argument is a number and the second argument is a string.

import pad from "pad";
pad(5, "pad", "-").should.eql("--pad");

Right padding: pad(text, length, [options])

Right padding occurs when the first argument is a string and the second argument is a number.

import pad from "pad";
pad("pad", 6).should.eql("pad   ");

Installing

Starting with version 1.1.0, Node 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/adaltas/node-pad.git

Testing

Clone the repo, install the development dependencies and run the suite:

git clone http://github.com/adaltas/node-pad.git .
npm install
make test

Contributors

The project is sponsored by Adaltas based in Paris, France. Adaltas offers support and consulting on distributed systems, big data and open source.