Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pad

adaltas1.2mBSD-3-Clause3.3.0TypeScript support: included

Left and right string padding

pad, string

readme

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:

  • CommonJS: dist/pad.cjs.js
    Bundle used by Node.js and compatible with ES5. It is declared inside the package.json by the main property and used by default with require("pad") in a Node.js environment.
  • ES module: dist/pad.esm.js
    Bundle using the ECMAScript standard defined in ES6 for working with modules. The path to the ES module is declared inside the package.json by the module property for ESM-aware tools like Rollup and webpack 2+.

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:

  • char (string)
    The character used to fill the gap.
  • colors (boolean)
    Ajust to hidden terminal color characters, you may also use require 'pad/lib/colors' to avoid passing this option.
  • strip (boolean)
    Remove characters from text if length smaller than text length, default to "false".
  • fixed_width (boolean)
    An optimization option to disable the usage of the wcwdith package to handle the discovery of characters using more than one column for display. one column to display
  • wcwidth_options (object)
    Options passed to the wcwidth package used to calculate the display width of characters using more than one column.

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.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.3.0 (2024-09-29)

Features

Bug Fixes

  • add ts allowSyntheticDefaultImports (a53517e)

Changelog

Trunk

  • package: update author url
  • ts: api tests
  • src: rewrite in js

Version 3.2.0

  • package: fix rollup dev dependencies

Version 3.1.0

  • package: declare "dist" in files filed

Version 3.0.1

  • rollup: use commonjs and node-resolve plugins
  • ts: move definition file into dist/pad.d.ts

Version 3.0.0

Backward incompatibility:

  • bundles: exported modules are no longer in lib but in dist
  • api: remove the colors module, use directly the option instead

New feature:

  • dist: generate cjs, esm and umd bundles

Version 2.3.0

  • project: use files instead of npm ignore
  • project: ignore lock file

Version 2.2.3

  • wcwidth: pass configuration

Version 2.2.2

  • babel: fix es5 generation and upgrade to version 7

Version 2.2.1

  • readme: es5

Version 2.2.0

New feature:

  • package: generate es5 modules

Cleanup

  • package: latest dependencies
  • package: exclude package-lock file

Version 2.1.0

  • package: add TypeScript definition file

Version 2.0.3

  • package: declare coffee as dev dependency

Version 2.0.2

  • package: release commands

v2.0.1

src: js backward compatibility with coffee 2

v2.0.0

  • package: upgrade to CoffeeScript 2

v1.2.1

  • package revers to CoffeeScript 1

v1.2.0

  • src: handle characters using multi columns for display
  • package: new changelog file
  • package: upgrade to CoffeeScript 2

V1.1.0

  • src: use string.repeat instead of loop

v1.0.3

  • readme: multiple updates
  • package: set homepage
  • package: move to adaltas github owner

v1.0.2

  • package: doc simplification

v1.0.1

  • src: get rid of regex capture groups
  • package: clean up with fixpack

v1.0.0

  • package: dependencies with caret
  • src: size < str.length and new option strip fix #5