Giter Site home page Giter Site logo

string-padder's Introduction

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

string-padder's People

Contributors

glentiki avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.