You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importpadfrom"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:
constpad=require("pad/dist/pad.cjs.js");// Or simplyconstpad=require("pad");
While the ES Modules syntax is:
importpadfrom"pad/dist/pad.esm.js";// Or for ESM-aware toolsimportpadfrom"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.