Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "smartwrap", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Textwrap for javascript/nodejs. Correctly handles wide characters (宽字符) and emojis (😃). Wraps strings with option to break on words.", | ||
@@ -5,0 +5,0 @@ "main": "src/main.js", |
@@ -90,12 +90,14 @@ # smartwrap | ||
```sh | ||
--breakword Choose whether or not to break words when wrapping a string | ||
--breakword Choose whether or not to break words when wrapping a string | ||
[default: false] | ||
--minWidth Never change this unless you are certin you are not using | ||
--errorChar Placeholder for wide characters when minWidth < 2 | ||
[default: �] | ||
--minWidth Never change this unless you are certin you are not using | ||
wide characters and you want a column 1 space wide. Then | ||
change to 1. [choices: 1, 2] [default: 2] | ||
--paddingLeft Set the left padding of the output [default: 0] | ||
--paddingRight Set the right padding of the output [default: 0] | ||
--splitAt Characters at which to split input [default: [" ","\t"]] | ||
--trim Trim the whitespace from end of input [default: true] | ||
--width, -w Set the line width of the output (in spaces) | ||
--paddingLeft Set the left padding of the output [default: 0] | ||
--paddingRight Set the right padding of the output [default: 0] | ||
--splitAt Characters at which to split input [default: [" ","\t"]] | ||
--trim Trim the whitespace from end of input [default: true] | ||
--width, -w Set the line width of the output (in spaces) | ||
[required] [default: 10] | ||
@@ -102,0 +104,0 @@ ``` |
"use strict" | ||
let Wcwidth = require("wcwidth") | ||
let Breakword = require("breakword") | ||
let wcwidth = require("wcwidth") | ||
let breakword = require("breakword") | ||
@@ -32,2 +32,3 @@ function smartWrap(input, options) { | ||
obj.paddingRight = 0 | ||
obj.errorChar = "�" | ||
obj.returnFormat = "string" //or 'array' | ||
@@ -47,2 +48,12 @@ obj.skipPadding = false //set to true when padding set too wide for line length | ||
options = options || {} | ||
if (options.errorChar) { | ||
// only allow a single errorChar | ||
options.errorChar = options.errorChar.split('')[0] | ||
// errorChar must not be wide character | ||
if (wcwidth(options.errorChar) > 1) | ||
throw new Error(`Error character cannot be a wide character (${options.errorChar})`) | ||
} | ||
let wrapObj = Object.assign({},defaults(),options) | ||
@@ -100,10 +111,16 @@ | ||
word = wrapObj.words.shift() | ||
wordlength = Wcwidth(word) | ||
let wordLength = wcwidth(word) | ||
switch(true) { | ||
//1- Word is too long for an empty line and must be broken | ||
case(wrapObj.lineLength < wordlength): | ||
// Too long for an empty line and is a single character | ||
case(wrapObj.lineLength < wordLength && word.split('').length === 1): | ||
wrapObj.words.unshift(wrapObj.errorChar) | ||
break | ||
// Too long for an empty line, must be broken between 2 lines | ||
case(wrapObj.lineLength < wordLength): | ||
//Break it, then re-insert its parts into wrapObj.words | ||
//so can loop back to re-handle each word | ||
splitIndex = Breakword(word,wrapObj.lineLength) | ||
splitIndex = breakword(word,wrapObj.lineLength) | ||
wrapObj.words.unshift(word.substr(0,splitIndex + 1)) //+1 for substr fn | ||
@@ -113,4 +130,4 @@ wrapObj.words.splice(1,0,word.substr(splitIndex + 1))//+1 for substr fn | ||
//2- Word is too long for current line and must be wrapped | ||
case(spaceRemaining < wordlength): | ||
// Not enough space remaining in line, must be wrapped to next line | ||
case(spaceRemaining < wordLength): | ||
//add a new line to our array of lines | ||
@@ -124,4 +141,3 @@ wrapObj.lines.push([]) | ||
//3- Word fits on current line | ||
//caution: falls through | ||
// Fits on current line | ||
default: | ||
@@ -131,3 +147,3 @@ //add word to line | ||
//reduce space remaining (add a space between words) | ||
wrapObj.spacesUsed += wordlength + 1 | ||
wrapObj.spacesUsed += wordLength + 1 | ||
} | ||
@@ -134,0 +150,0 @@ } |
@@ -10,2 +10,6 @@ #!/usr/bin/env node | ||
}) | ||
yargs.option("errorChar", { | ||
default: "�", | ||
describe: "Placeholder for wide characters when minWidth < 2" | ||
}) | ||
yargs.option("minWidth", { | ||
@@ -12,0 +16,0 @@ choices: [1,2], |
@@ -10,7 +10,7 @@ 'use strict'; | ||
const should = chai.should(); | ||
const smartwrap = require(__dirname+'/../src/main.js'); | ||
const filepath = __dirname+'/tests.json'; | ||
const smartwrap = require("../"); | ||
const filepath = `${__dirname}/tests.json`; | ||
let test = function(testResult,savedResult){ | ||
it(`'${testResult}' should match '${savedResult}'`,function(){ | ||
it(`'${testResult}' should match '${savedResult}'`, () => { | ||
testResult.should.equal(savedResult); | ||
@@ -21,5 +21,3 @@ }) | ||
//get test list | ||
let str = fs.readFileSync(filepath,{ | ||
encoding : 'utf-8' | ||
}); | ||
let str = fs.readFileSync(filepath, {encoding : 'utf-8'}); | ||
@@ -39,3 +37,3 @@ let obj = JSON.parse(str); | ||
'breakword' | ||
].forEach(function(element){ | ||
].forEach( element => { | ||
if (typeof obj[i][element] !== 'undefined') { | ||
@@ -53,2 +51,3 @@ options[element] = obj[i][element]; | ||
console.log("END-----------------\n"); | ||
switch(true){ | ||
@@ -64,4 +63,4 @@ case(typeof global.save !== 'undefined' && global.save): | ||
//run tests | ||
describe('Test '+i,function(){ | ||
test(testResult,obj[i].output); | ||
describe('Test '+i, () => { | ||
test(testResult, obj[i].output); | ||
}) | ||
@@ -73,3 +72,3 @@ } | ||
//write saved object to file | ||
fs.writeFileSync(filepath,JSON.stringify(obj,null,2),'utf8'); | ||
fs.writeFileSync(filepath, JSON.stringify(obj, null, 2), 'utf8'); | ||
console.log("Tests saved to file."); | ||
@@ -76,0 +75,0 @@ } |
[ | ||
{ | ||
"input": "特abc制", | ||
"width": 1, | ||
"trim": true, | ||
"minWidth": 1, | ||
"output": "�\na\nb\nc\n�" | ||
}, | ||
{ | ||
"input": "aaa bbb ccc", | ||
"width": 1, | ||
"trim": true, | ||
"minWidth": 1, | ||
"output": "a\na\na\nb\nb\nb\nc\nc\nc" | ||
}, | ||
{ | ||
"input": "2.0", | ||
@@ -4,0 +18,0 @@ "width": 1, |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
44768
16
513
113
1