varsize-string
Advanced tools
Comparing version 2.1.1 to 2.2.0
45
index.js
@@ -1,3 +0,2 @@ | ||
function preparePaddingSet (characterLookup, width, padding, override, fallback) { | ||
function preparePaddingSet (characterLookup, width, process, padding, override, fallback) { | ||
var result | ||
@@ -31,20 +30,39 @@ if (typeof padding === 'string') { | ||
if (result.left !== undefined && !(result.left instanceof VarSizeString)) { | ||
result.left = new VarSizeString(result.left, characterLookup) | ||
result.left = new VarSizeString(process(result.left), characterLookup) | ||
} | ||
if (result.right !== undefined && !(result.right instanceof VarSizeString)) { | ||
result.right = new VarSizeString(result.right, characterLookup) | ||
result.right = new VarSizeString(process(result.right), characterLookup) | ||
} | ||
var leftSize = result.left ? result.left.size() : 0 | ||
var rightSize = result.right ? result.right.size() : 0 | ||
while (rightSize > 0 && (leftSize + rightSize) >= width) { | ||
rightSize = result.right.pop() | ||
if (!isNaN(width)) { | ||
while (rightSize > 0 && (leftSize + rightSize) >= width) { | ||
rightSize = result.right.pop() | ||
} | ||
while (leftSize >= width) { | ||
leftSize = result.left.pop() | ||
} | ||
} | ||
while (leftSize >= width) { | ||
leftSize = result.left.pop() | ||
} | ||
return result | ||
} | ||
function preparePadding (padding, characterLookup, width) { | ||
var prepare = preparePaddingSet.bind(null, characterLookup, width) | ||
function preparePadding (characterLookup, process, width, padding) { | ||
if (arguments.length === 2) { | ||
padding = process | ||
process = undefined | ||
} else if (arguments.length === 3) { | ||
padding = width | ||
if (typeof process === 'function') { | ||
width = undefined | ||
} else { | ||
width = process | ||
process = undefined | ||
} | ||
} | ||
if (!process) { | ||
process = function (str) { | ||
return str | ||
} | ||
} | ||
var prepare = preparePaddingSet.bind(null, characterLookup, width, process) | ||
var override = prepare(padding) | ||
@@ -65,2 +83,3 @@ var regular = prepare(padding && padding.regular, override) | ||
} | ||
VarSizeString.padding = preparePadding | ||
VarSizeString.prototype.init = function () { | ||
@@ -198,3 +217,3 @@ if (isNaN(this._size)) { | ||
} | ||
VarSizeString.prototype.wrap = function (width, padding) { | ||
VarSizeString.prototype.wrap = function (width, padding, process) { | ||
const sep = ' ' | ||
@@ -204,3 +223,3 @@ const sepLength = 1 | ||
padding = preparePadding(padding, this.characterLookup, width) | ||
padding = preparePadding(this.characterLookup, process, width, padding) | ||
@@ -207,0 +226,0 @@ var hadLeftOver = false |
{ | ||
"name": "varsize-string", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "A package for working with strings where the different characters have different sizes.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -54,3 +54,3 @@ [![ISC License](https://img.shields.io/badge/license-ISC-blue.svg?style=flat)](LICENSE.md) | ||
Analogous to [`String.substring`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring). This method will return the **fully visible** characters between `startSize` and `endSize`. If `endSize` is not given it will assume a substring from `startSize` until the end of the string. | ||
**Unlike** the `String.substring` however this method returns an object with the properties `size` and `string` in order to know the size of the substring. | ||
_However:_ **Unlike** `String.substring`, this method returns an object with the properties `size` and `string` in order to know the size of the substring. | ||
@@ -178,1 +178,5 @@ Example: | ||
} | ||
``` | ||
### `vsstring.padding(characterLookup, [process], [width], padding)` | ||
Turns a flexible padding definition into a clear padding definition. You can pass in an optional `process` variable to process the strings before they are being turned into varsizes-strings. You can also pass-in a `width` to make sure that the padding will not exceed the width of, say, a wrapped string. |
@@ -118,2 +118,19 @@ var test = require('tape') | ||
}) | ||
test('padding', function (t) { | ||
t.equal(str.padding(vars, ' ').first.left.string, ' ', 'Turn string into variants') | ||
t.equal(str.padding(vars, 1, ' ').first.left.string, '', 'Turn string into variants') | ||
t.equal(str.padding(vars, function (str) { | ||
return 'x' | ||
}, ' ').first.left.string, 'x', 'Apply the template method') | ||
t.equal(str.padding(vars, function (str) { | ||
return 'x' | ||
}, ' ').regular.left.string, 'x', 'Apply the template method') | ||
t.equal(str.padding(vars, function (str) { | ||
return 'x' + str | ||
}, {first: 'a', regular: 'b'}).first.left.string, 'xa', 'Apply the template method for each string separtely') | ||
t.equal(str.padding(vars, function (str) { | ||
return 'x' + str | ||
}, {first: 'a', regular: 'b'}).regular.left.string, 'xb', 'Apply the template method for each string separtely') | ||
t.end() | ||
}) | ||
test('substr', function (t) { | ||
@@ -120,0 +137,0 @@ t.equal(substr('abcd', 1), 'bcd') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28298
569
181