package/package.json000644 000765 000024 0000001164 12644030473013022 0ustar00000000 000000 { "name": "wide-align", "version": "1.1.0", "description": "A wide-character aware text alignment function for use on the console or with fixed width fonts.", "main": "align.js", "scripts": { "test": "tap --coverage test/*.js" }, "keywords": [ "wide", "double", "unicode", "cjkv", "pad", "align" ], "author": "Rebecca Turner (http://re-becca.org/)", "license": "ISC", "repository": { "type": "git", "url": "https://github.com/iarna/wide-align" }, "dependencies": { "string-width": "^1.0.1" }, "devDependencies": { "tap": "^2.3.2" } } package/.npmignore000644 000765 000024 0000000054 12634114371012527 0ustar00000000 000000 *~ /node_modules .#* /.nyc_output /coverage package/README.md000644 000765 000024 0000003017 12634176463012023 0ustar00000000 000000 wide-align ---------- A wide-character aware text alignment function for use in terminals / on the console. ### Usage ``` var 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 // an en wide and all wide characters are an em. In browsers, this isn't // held to and wide characters like "古" can be less than two narrow // characters even with a fixed width font. console.log(align.center('abc', 10)) // ' abc ' console.log(align.center('古古古', 10)) // ' 古古古 ' console.log(align.left('abc', 10)) // 'abc ' console.log(align.left('古古古', 10)) // '古古古 ' console.log(align.right('abc', 10)) // ' abc' console.log(align.right('古古古', 10)) // ' 古古古' ``` ### Functions #### `align.center(str, length)` → `str` Returns *str* with spaces added to both sides such that that it is *length* chars long and centered in the spaces. #### `align.left(str, length)` → `str` Returns *str* with spaces to the right such that it is *length* chars long. ### `align.right(str, length)` → `str` Returns *str* with spaces to the left such that it is *length* chars long. ### Origins These functions were originally taken from [cliui](https://npmjs.com/package/cliui). Changes include switching to the MUCH faster pad generation function from [lodash](https://npmjs.com/package/lodash), making center alignment pad both sides and adding left alignment. package/LICENSE000644 000765 000024 0000001360 12634114550011535 0ustar00000000 000000 Copyright (c) 2015, Rebecca Turner Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package/align.js000644 000765 000024 0000002624 12644030410012155 0ustar00000000 000000 'use strict' var stringWidth = require('string-width') exports.center = alignCenter exports.left = alignLeft exports.right = alignRight // lodash's way of generating pad characters. function createPadding (width) { var result = '' var string = ' ' var n = width do { if (n % 2) { result += string; } n = Math.floor(n / 2); string += string; } while (n); return result; } function alignLeft (str, width) { var trimmed = str.trimRight() if (trimmed.length === 0 && str.length >= width) return str var padding = '' var strWidth = stringWidth(trimmed) if (strWidth < width) { padding = createPadding(width - strWidth) } return trimmed + padding } function alignRight (str, width) { var trimmed = str.trimLeft() if (trimmed.length === 0 && str.length >= width) return str var padding = '' var strWidth = stringWidth(trimmed) if (strWidth < width) { padding = createPadding(width - strWidth) } return padding + trimmed } function alignCenter (str, width) { var trimmed = str.trim() if (trimmed.length === 0 && str.length >= width) return str var padLeft = '' var padRight = '' var strWidth = stringWidth(trimmed) if (strWidth < width) { var padLeftBy = parseInt((width - strWidth) / 2, 10) padLeft = createPadding(padLeftBy) padRight = createPadding(width - (strWidth + padLeftBy)) } return padLeft + trimmed + padRight } package/test/align.js000644 000765 000024 0000003316 12641147715013151 0ustar00000000 000000 'use strict' var test = require('tap').test var align = require('..') 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.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.is(align.left('', 5), ' ', 'left align nothing') t.is(align.center('', 5), ' ', 'center align nothing') t.is(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.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.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.end() })