Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Padding and aligning output


It is a common requirement to pad our display values to a specified size. This may involve padding to the left, right, or even center values. This is especially useful when generating fixed-width output. In this recipe, we will see how to pad our values by creating a useful, multipurpose padding function.

Getting ready

We will be using the basic tools provided by Node.

We want to create a function that will accept a string value, which will pad it to a specified size (number of characters) with a padding character. It should be able to pad our values to the left or right.

Padding can also provide some alignment functionality. For example, if we pad a value to the right, it will be left aligned, while padding to the left will right align our value.

Let's look at how we can accomplish this.

How to do it...

In this example, we will create a function called pad() to perform padding and alignment:

  1. Create the function signature. This function will accept a value, a desired...