leftpad-everything
v1.0.1
Published
Left-pads everything, including strings, functions, events, promises, and more.
Maintainers
Readme
Left-Pad Everything
A comprehensive library that left-pads everything, including strings, numbers, arrays, dates, promises, functions, errors and so forth. This library is a reflection of the infamous left-pad incident in 2016, which demonstrated the fragility of relying on third-party libraries.
Install via NPM: npm install leftpad-everything
Features
- String Padding: Left-pad strings with specified characters
- Number Padding: Left-pad numbers with zeros or custom characters
- Array Padding: Left-pad arrays with specified values
- Date Padding: Add padding to date timestamps
- Boolean Padding: Left-pad boolean values (because why not?)
- BigInt Padding: Left-pad big integers with zeros or custom characters
- Multiline Padding: Left-pad each line of a multiline string
- Event Queue Padding: Left-pad events with priority-based execution
- Promise Padding: Delay promise resolution
- Function Padding: Prepend arguments to functions
- Error Padding: Add padding to error messages
- Object Padding: Add prefixes to object keys
- Time Padding: Format time components with leading zeros
Usage
Importing
// CommonJS
const { LeftPad } = require('leftpad-everything');
// ES Modules
import { LeftPad } from 'leftpad-everything';Examples
String Padding
LeftPad.string('test', 10); // ' test'
LeftPad.string('test', 10, '0'); // '000000test'Number Padding
LeftPad.number(42, 5); // '00042'
LeftPad.number(42, 5, 'x'); // 'xxx42'Array Padding
LeftPad.array([1, 2, 3], 5); // [undefined, undefined, 1, 2, 3]
LeftPad.array([1, 2, 3], 5, 0); // [0, 0, 1, 2, 3]Date Padding
const now = new Date();
LeftPad.date(now, 1000); // Date object 1 second in the futureBoolean Padding
LeftPad.boolean(true, 10); // ' true'
LeftPad.boolean(false, 10); // ' false'BigInt Padding
LeftPad.bigint(12345n, 10); // '0000012345'Multiline Padding
const multiline = 'Hello\nWorld';
LeftPad.multiline(multiline, 10); // ' Hello\n World'Event Queue Padding
let dismissEvent = void 0;
for (let i = 0; i < 10; i++) {
const eventFunc = () => console.log(`Event ${i}`);
if (i === 5) dismissEvent = eventFunc;
LeftPad.event(eventFunc, Number(i % 2));
}
LeftPad.events.off(dismissEvent); // Dismisses the specific event
LeftPad.invoke(); // Executes events in priority orderPromise Padding
const promise = new Promise(resolve => resolve('Done'));
LeftPad.promise(promise, 1000).then(console.log); // Delays resolution by 1 secondFunction Padding
const add = (a, b) => a + b;
const paddedAdd = LeftPad.function(add, 10);
paddedAdd(5); // Returns 15 (10 + 5)Error Padding
try {
LeftPad.error(new Error('Test error'), ' ');
} catch (err) {
console.log(err.message); // ' Test error'
}Object Padding
const obj = { a: 1, b: 2 };
LeftPad.object(obj, 'prefix_'); // { prefix_a: 1, prefix_b: 2 }Time Padding
LeftPad.time(9, 5, 3); // '09:05:03'
LeftPad.time(12, 30, 45); // '12:30:45'