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

string-padder

v1.0.3

Published

A (tiny!) utility module for string padding methods.

Downloads

66

Readme

string-padder - Another string padding module.


A (tiny!) utility module for string padding methods.

I created this after seeing the code here: http://www.lm-tech.it/Blog/post/2012/12/01/String-Padding-in-Javascript.aspx

I didn't want to mess with the String prototype however... so here we are!

This is a module which (Currently) exports 3 functions, padLeft, padRight and padBoth. All functions take the string which is to be padded, the length the string must be padded to and optionally the string of chars or a single char to pad the string with, and returns the padded string. If no string of chars/single char is passed into the function to pad with, it defaults to a space char: ' '.

If a string is entered to be used as the pad, that string is repeated until the return string length is the pad length passed into the function, cutting the pad string off mid string. This can cause unexpected behaviour if using padBoth. Please refer to the padBoth example to see the behaviour expected. The left side pad is given precedence, but only by a single character, meaning the function will try to pad each side equally.

Installing

npm install string-padder

padLeft Example

	var padLeft = require('string-padder').padLeft;

	// basic example of a single char used as padding
	var stringToBePadded = "hey I'm only 27 chars long!";
	var leftPaddedString = padLeft(stringToBePadded, 35, '_');
	leftPaddedString  === "________hey I'm only 27 chars long!"

	// example of repeating string padding
	var str = "123";
	var tenLong = padLeft(str, 10, "abcde");
	tenLong === "abcdeab123";

padRight Example

	var padRight = require('string-padder').padRight;

	// basic example of a single char used as padding
	var stringToBePadded = "hey I'm only 27 chars long!";
	var rightPaddedString = padRight(stringToBePadded, 35, '_');
	rightPaddedString  === "hey I'm only 27 chars long!________";

	// example of repeating string padding
	var str = "123";
	var tenLong = padRight(str, 10, 'abcde');
	tenLong === "123abcdeab";

padBoth Example

	var padBoth = require('string-padder').padBoth;

	// basic example of a single char used as padding
	var stringToBePadded = "hey I'm only 27 chars long!";
	var bothPaddedString = padBoth(stringToBePadded, 35, '_');
	bothPaddedString  === "____hey I'm only 27 chars long!____";

	var leftSidePrecedence = padBoth(stringToBePadded, 36, '_');
	leftSidePrecedence  === "_____hey I'm only 27 chars long!____";

	// example of repeating string padding
	var str = "123";
	var tenLong = padBoth(str, 10, "abcde");
	tenLong === "abcd123abc";

	var twentyLong = padBoth(str, 20, "abcde");
	twentyLong === "abcdeabcd123abcdeabc";

Happy Padding!

Copyright 2015 - Glen Keane - MIT Licence