1.0.4 • Published 7 years ago

ascii-string-align v1.0.4

Weekly downloads
3
License
ISC
Repository
github
Last release
7 years ago

ascii-string-align Build Status

Description

Using Node.js, trim then pad an ASCII string to a specific width. Optionally specify a given alignment of the text: left, right, center, justify.

Please note that the library may not format multi-byte strings nicely, so ASCII is all that is really supported.

Installation

Add this library to your current Node.js project using npm:

npm install --save ascii-string-align

Or, checkout the source:

git clone https://github.com/tcowley/ascii-string-align

Code Example

Here is a basic code example. Full behaviour is documented in the API Reference below

var asciiStringAlign = require('ascii-string-align');

var myString = 'a b c d e';
var width = 20;

['left', 'right', 'center', 'justify'].forEach(function(alignment) {
    console.log('[' + asciiStringAlign(myString, width, alignment) + ']');
});

This example will output:

[a b c d e           ] 
[           a b c d e] 
[     a b c d e      ] 
[a   b    c    d    e] 

API Reference

Basics

The library exports one method, which accepts two parameters:

ParamValuesDescription
asciiStringAny length of ASCII stringThe string to be padded with spaces to the specified width.
widthAny positive integerThe width, in characters, that the resulting string should be. If width is shorter than the length of asciiString, asciiString will be returned unchanged.
alignmentOptional. One of 'left', 'right', 'center' or 'justify'. Default is 'left'Determines how spaces are added to the string. See String Alignment, below for examples.

The return value is always a string. Illegal inputs throw an error.

Tests

Tests are run automatically using Travis CI, but you can run them yourself quite easily.

$ git clone https://github.com/tcowley/ascii-string-align
$ cd ascii-string-align
$ npm install
$ npm test

// test output displays here
 

The project is using Tape for writing tests. For a small project like this, tape is very easy to use.

License

ISC

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago