Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
sudo: false
node_js:
- "6"
- "12"
- "14"
- "16"
- "16"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ console.
### Usage

```
var align = require('wide-align')
const align = require('wide-align')

// Note that if you view this on a unicode console, all of the slashes are
// aligned. This is because on a console, all narrow characters are
Expand Down
60 changes: 36 additions & 24 deletions align.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
'use strict'
var stringWidth = require('string-width')

exports.center = alignCenter
exports.left = alignLeft
exports.right = alignRight
const stringWidth = require('string-width')

// lodash's way of generating pad characters.

function createPadding (width) {
var result = ''
var string = ' '
var n = width
let result = ''
let string = ' '
let n = width
do {
if (n % 2) {
result += string;
result += string
}
n = Math.floor(n / 2);
string += string;
} while (n);
n = Math.floor(n / 2)
string += string
} while (n)

return result;
return result
}

/**
* @param {string} str
* @param {number} width
*/
function alignLeft (str, width) {
var trimmed = str.trimRight()
const trimmed = str.trimEnd()
if (trimmed.length === 0 && str.length >= width) return str
var padding = ''
var strWidth = stringWidth(trimmed)
let padding = ''
const strWidth = stringWidth(trimmed)

if (strWidth < width) {
padding = createPadding(width - strWidth)
Expand All @@ -35,11 +35,15 @@ function alignLeft (str, width) {
return trimmed + padding
}

/**
* @param {string} str
* @param {number} width
*/
function alignRight (str, width) {
var trimmed = str.trimLeft()
const trimmed = str.trimStart()
if (trimmed.length === 0 && str.length >= width) return str
var padding = ''
var strWidth = stringWidth(trimmed)
let padding = ''
const strWidth = stringWidth(trimmed)

if (strWidth < width) {
padding = createPadding(width - strWidth)
Expand All @@ -48,18 +52,26 @@ function alignRight (str, width) {
return padding + trimmed
}

/**
* @param {string} str
* @param {number} width
*/
function alignCenter (str, width) {
var trimmed = str.trim()
const trimmed = str.trim()
if (trimmed.length === 0 && str.length >= width) return str
var padLeft = ''
var padRight = ''
var strWidth = stringWidth(trimmed)
let padLeft = ''
let padRight = ''
const strWidth = stringWidth(trimmed)

if (strWidth < width) {
var padLeftBy = parseInt((width - strWidth) / 2, 10)
const padLeftBy = Math.floor((width - strWidth) / 2)
padLeft = createPadding(padLeftBy)
padRight = createPadding(width - (strWidth + padLeftBy))
}

return padLeft + trimmed + padRight
}

exports.center = alignCenter
exports.left = alignLeft
exports.right = alignRight
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"pad",
"align"
],
"engines": {
"node": ">=6"
},
"author": "Rebecca Turner <[email protected]> (http://re-becca.org/)",
"license": "ISC",
"repository": {
Expand Down
52 changes: 26 additions & 26 deletions test/align.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
'use strict'
var test = require('tap').test
var align = require('..')
const {test} = require('tap')
const align = require('../align.js')

test('align', function (t) {
t.is(align.center('abc', 10), ' abc ', 'center narrow')
t.is(align.center('古古古', 10), ' 古古古 ', 'center wide')
t.is(align.left('abc', 10), 'abc ', 'left narrow')
t.is(align.left('古古古', 10), '古古古 ', 'left wide')
t.is(align.right('abc', 10), ' abc', 'right narrow')
t.is(align.right('古古古', 10), ' 古古古', 'right wide')
t.equal(align.center('abc', 10), ' abc ', 'center narrow')
t.equal(align.center('古古古', 10), ' 古古古 ', 'center wide')
t.equal(align.left('abc', 10), 'abc ', 'left narrow')
t.equal(align.left('古古古', 10), '古古古 ', 'left wide')
t.equal(align.right('abc', 10), ' abc', 'right narrow')
t.equal(align.right('古古古', 10), ' 古古古', 'right wide')

t.is(align.center('abc', 2), 'abc', 'center narrow overflow')
t.is(align.center('古古古', 4), '古古古', 'center wide overflow')
t.is(align.left('abc', 2), 'abc', 'left narrow overflow')
t.is(align.left('古古古', 4), '古古古', 'left wide overflow')
t.is(align.right('abc', 2), 'abc', 'right narrow overflow')
t.is(align.right('古古古', 4), '古古古', 'right wide overflow')
t.equal(align.center('abc', 2), 'abc', 'center narrow overflow')
t.equal(align.center('古古古', 4), '古古古', 'center wide overflow')
t.equal(align.left('abc', 2), 'abc', 'left narrow overflow')
t.equal(align.left('古古古', 4), '古古古', 'left wide overflow')
t.equal(align.right('abc', 2), 'abc', 'right narrow overflow')
t.equal(align.right('古古古', 4), '古古古', 'right wide overflow')

t.is(align.left('', 5), ' ', 'left align nothing')
t.is(align.center('', 5), ' ', 'center align nothing')
t.is(align.right('', 5), ' ', 'right align nothing')
t.equal(align.left('', 5), ' ', 'left align nothing')
t.equal(align.center('', 5), ' ', 'center align nothing')
t.equal(align.right('', 5), ' ', 'right align nothing')

t.is(align.left(' ', 5), ' ', 'left align whitespace')
t.is(align.center(' ', 5), ' ', 'center align whitespace')
t.is(align.right(' ', 5), ' ', 'right align whitespace')
t.equal(align.left(' ', 5), ' ', 'left align whitespace')
t.equal(align.center(' ', 5), ' ', 'center align whitespace')
t.equal(align.right(' ', 5), ' ', 'right align whitespace')

t.is(align.left(' ', 2), ' ', 'left align whitespace overflow')
t.is(align.center(' ', 2), ' ', 'center align whitespace overflow')
t.is(align.right(' ', 2), ' ', 'right align whitespace overflow')
t.equal(align.left(' ', 2), ' ', 'left align whitespace overflow')
t.equal(align.center(' ', 2), ' ', 'center align whitespace overflow')
t.equal(align.right(' ', 2), ' ', 'right align whitespace overflow')

t.is(align.left(' x ', 10), ' x ', 'left align whitespace mix')
t.is(align.center(' x ', 10), ' x ', 'center align whitespace mix')
t.is(align.right(' x ', 10), ' x ', 'right align whitespace mix')
t.equal(align.left(' x ', 10), ' x ', 'left align whitespace mix')
t.equal(align.center(' x ', 10), ' x ', 'center align whitespace mix')
t.equal(align.right(' x ', 10), ' x ', 'right align whitespace mix')

t.end()
})