Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Left pad a string with zeros or a specified string. Fastest implementation.
The pad-left npm package is a utility that allows you to pad the left side of a string with a specified character or set of characters until the string reaches a desired length. This can be useful for formatting output, aligning text, or ensuring consistent string lengths.
Basic Padding
Pads the left side of the string '123' with spaces until the total length is 5.
const padLeft = require('pad-left');
const result = padLeft('123', 5);
console.log(result); // ' 123'
Custom Padding Character
Pads the left side of the string '123' with the character '0' until the total length is 5.
const padLeft = require('pad-left');
const result = padLeft('123', 5, '0');
console.log(result); // '00123'
Padding with Multiple Characters
Pads the left side of the string '123' with the string 'abc' repeated until the total length is 7.
const padLeft = require('pad-left');
const result = padLeft('123', 7, 'abc');
console.log(result); // 'abca123'
The left-pad package provides similar functionality to pad-left, allowing you to pad the left side of a string with a specified character or set of characters. It is widely known and used in the JavaScript community.
The string-pad package offers more comprehensive padding options, including both left and right padding. It allows for more flexibility in padding strings compared to pad-left.
The pad package provides both left and right padding functionalities. It is a versatile package that can be used for various string padding needs, making it a more general-purpose tool compared to pad-left.
Left pad a string with zeros or a specified string. Fastest implementation.
You might also be interested in word-wrap.
Install with npm:
$ npm install pad-left --save
var pad = require('pad-left');
pad( '4', 4, '0') // 0004
pad( '35', 4, '0') // 0035
pad('459', 4, '0') // 0459
Benchmarks for node.js v6.1.0 versus left-pad.
# benchmark/fixtures/10-custom-char.js (37 bytes)
pad-left x 14,940,947 ops/sec ±0.81% (87 runs sampled)
left-pad x 7,901,604 ops/sec ±1.17% (86 runs sampled)
# benchmark/fixtures/10.js (32 bytes)
pad-left x 13,305,123 ops/sec ±1.53% (84 runs sampled)
left-pad x 6,979,536 ops/sec ±1.13% (84 runs sampled)
# benchmark/fixtures/100-custom-char.js (38 bytes)
pad-left x 14,227,637 ops/sec ±1.41% (81 runs sampled)
left-pad x 1,352,240 ops/sec ±1.51% (86 runs sampled)
# benchmark/fixtures/100.js (34 bytes)
pad-left x 15,664,561 ops/sec ±0.99% (83 runs sampled)
left-pad x 1,247,316 ops/sec ±0.96% (87 runs sampled)
# benchmark/fixtures/1000-custom-char.js (40 bytes)
pad-left x 15,210,294 ops/sec ±1.15% (87 runs sampled)
left-pad x 159,958 ops/sec ±1.01% (88 runs sampled)
# benchmark/fixtures/1000.js (35 bytes)
pad-left x 14,157,425 ops/sec ±1.10% (88 runs sampled)
left-pad x 143,805 ops/sec ±1.08% (87 runs sampled)
Benchmarks for node.js v6.1.0 versus stevemao/left-pad.
# benchmark/fixtures/10-custom-char.js (37 bytes)
pad-left x 13,251,037 ops/sec ±1.40% (84 runs sampled)
left-pad x 10,745,530 ops/sec ±1.18% (86 runs sampled)
# benchmark/fixtures/10.js (32 bytes)
pad-left x 13,644,357 ops/sec ±1.33% (87 runs sampled)
left-pad x 20,107,245 ops/sec ±1.00% (88 runs sampled)
# benchmark/fixtures/100-custom-char.js (38 bytes)
pad-left x 15,650,330 ops/sec ±1.01% (86 runs sampled)
left-pad x 9,706,877 ops/sec ±1.13% (87 runs sampled)
# benchmark/fixtures/1000-custom-char.js (40 bytes)
pad-left x 17,255,593 ops/sec ±0.99% (88 runs sampled)
left-pad x 6,312,637 ops/sec ±1.16% (87 runs sampled)
# benchmark/fixtures/1000.js (35 bytes)
pad-left x 15,679,410 ops/sec ±0.99% (90 runs sampled)
left-pad x 6,439,580 ops/sec ±1.08% (86 runs sampled)
You might also be interested in these projects:
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on May 07, 2016.
FAQs
Left pad a string with zeros or a specified string. Fastest implementation.
The npm package pad-left receives a total of 152,678 weekly downloads. As such, pad-left popularity was classified as popular.
We found that pad-left demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.