npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pad

v3.2.0

Published

Left and right string padding

Downloads

941,495

Readme

Node.js pad

Build Status

Node Pad is a simple and elegant function to pad strings in both left and right directions.

Usage

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 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+.
  • UMD: dis/pad.umd.js Bundle in the Universal Module Definition (UMD), a format compatible with both AMD and CommonJS.

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.

var pad = require('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.

var pad = require('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/wdavidw/node-pad.git

Testing

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

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