Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "smartwrap", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Textwrap for javascript/nodejs. Correctly handles wide characters (宽字符) and emojis (😃). Automatically breaks long words.", | ||
@@ -10,3 +10,5 @@ "main": "src/main.js", | ||
"scripts": { | ||
"test": "grunt t" | ||
"test": "grunt t", | ||
"lint": "./node_modules/.bin/eslint src/*", | ||
"lint-fix": "./node_modules/.bin/eslint src/* --fix" | ||
}, | ||
@@ -25,6 +27,7 @@ "repository": { | ||
"wcwidth": "^1.0.1", | ||
"yargs": "^13.3.0" | ||
"yargs": "^14.2.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"eslint": "^6.6.0", | ||
"grunt": "^1.0.4", | ||
@@ -31,0 +34,0 @@ "grunt-cli": "^1.3.2", |
163
src/main.js
@@ -1,9 +0,9 @@ | ||
'use strict'; | ||
"use strict" | ||
let Wcwidth = require('wcwidth'); | ||
let Breakword = require('breakword'); | ||
let Wcwidth = require("wcwidth") | ||
let Breakword = require("breakword") | ||
function smartWrap(input, options) { | ||
//in case a template literal was passed that has newling characters, | ||
//split string by newlines and process each resulting string | ||
//split string by newlines and process each resulting string | ||
const str = input.toString() | ||
@@ -17,83 +17,74 @@ const strArr = str.split("\n").map( string => { | ||
function wrap(text,options){ | ||
function wrap(text,options) { | ||
options = options || {}; | ||
let defaults = {}; | ||
options = options || {} | ||
let defaults = {} | ||
defaults.calculateSpaceRemaining = function(obj,i){ | ||
//i is in case someone wants to customize based on line index | ||
return Math.max(obj.lineLength - obj.spacesUsed - obj.paddingLeft - obj.paddingRight,0); | ||
}; //function to set starting line length | ||
defaults.currentLine = 0; //index of current line in 'lines[]' | ||
defaults.input = []; //input string split by whitespace | ||
defaults.calculateSpaceRemaining = function(obj) { | ||
return Math.max(obj.lineLength - obj.spacesUsed - obj.paddingLeft - obj.paddingRight, 0) | ||
} //function to set starting line length | ||
defaults.currentLine = 0 //index of current line in 'lines[]' | ||
defaults.input = [] //input string split by whitespace | ||
defaults.lines = [ | ||
[] | ||
]; //assume at least one line | ||
defaults.minWidth = 2; //fallback to if width set too narrow | ||
defaults.paddingLeft = 0; | ||
defaults.paddingRight = 0; | ||
defaults.returnFormat = 'string'; //or 'array' | ||
defaults.skipPadding = false; //set to true when padding set too wide for line length | ||
defaults.spacesUsed = 0; //spaces used so far on current line | ||
defaults.splitAt = [" ","\t"]; | ||
defaults.trim = true; | ||
defaults.width = 10; | ||
defaults.words = []; | ||
let wrapObj = Object.assign({},defaults,options); | ||
] //assume at least one line | ||
defaults.minWidth = 2 //fallback to if width set too narrow | ||
defaults.paddingLeft = 0 | ||
defaults.paddingRight = 0 | ||
defaults.returnFormat = "string" //or 'array' | ||
defaults.skipPadding = false //set to true when padding set too wide for line length | ||
defaults.spacesUsed = 0 //spaces used so far on current line | ||
defaults.splitAt = [" ","\t"] | ||
defaults.trim = true | ||
defaults.width = 10 | ||
defaults.words = [] | ||
let wrapObj = Object.assign({},defaults,options) | ||
//make sure correct sign on padding | ||
wrapObj.paddingLeft = Math.abs(wrapObj.paddingLeft); | ||
wrapObj.paddingRight = Math.abs(wrapObj.paddingRight); | ||
wrapObj.paddingLeft = Math.abs(wrapObj.paddingLeft) | ||
wrapObj.paddingRight = Math.abs(wrapObj.paddingRight) | ||
wrapObj.lineLength = wrapObj.width - | ||
wrapObj.paddingLeft - | ||
wrapObj.paddingRight; | ||
if(wrapObj.lineLength < wrapObj.minWidth){ | ||
wrapObj.paddingRight | ||
if(wrapObj.lineLength < wrapObj.minWidth) { | ||
//skip padding if lineLength too narrow | ||
wrapObj.skipPadding = true; | ||
wrapObj.lineLength = wrapObj.minWidth; | ||
wrapObj.skipPadding = true | ||
wrapObj.lineLength = wrapObj.minWidth | ||
} | ||
else{ | ||
//resize line length to include padding | ||
wrapObj.lineLength = wrapObj.lineLength; | ||
} | ||
//Break input into array of characters split by whitespace and/or tabs | ||
let unfilteredWords = []; | ||
let unfilteredWords = [] | ||
//to trim or not to trim... | ||
let modifiedText = text.toString(); | ||
if(wrapObj.trim){ | ||
modifiedText = modifiedText.trim(); | ||
let modifiedText = text.toString() | ||
if(wrapObj.trim) { | ||
modifiedText = modifiedText.trim() | ||
} | ||
if(wrapObj.splitAt.indexOf('\t')!==-1){ | ||
if(wrapObj.splitAt.indexOf("\t")!==-1) { | ||
//split at both spaces and tabs | ||
unfilteredWords = modifiedText.split(/ |\t/i); | ||
} | ||
else{ | ||
unfilteredWords = modifiedText.split(/ |\t/i) | ||
} else{ | ||
//split at whitespace | ||
unfilteredWords = modifiedText.split(' '); | ||
unfilteredWords = modifiedText.split(" ") | ||
} | ||
//remove empty array elements | ||
unfilteredWords.forEach(function(val){ | ||
if (val.length > 0){ | ||
wrapObj.words.push(val); | ||
unfilteredWords.forEach(function(val) { | ||
if (val.length > 0) { | ||
wrapObj.words.push(val) | ||
} | ||
}); | ||
}) | ||
let i, | ||
spaceRemaining, | ||
splitIndex, | ||
word, | ||
wordlength; | ||
let spaceRemaining, splitIndex, word, wordlength | ||
while(wrapObj.words.length > 0){ | ||
spaceRemaining = wrapObj.calculateSpaceRemaining(wrapObj); | ||
word = wrapObj.words.shift(); | ||
wordlength = Wcwidth(word); | ||
switch(true){ | ||
while(wrapObj.words.length > 0) { | ||
spaceRemaining = wrapObj.calculateSpaceRemaining(wrapObj) | ||
word = wrapObj.words.shift() | ||
wordlength = Wcwidth(word) | ||
switch(true) { | ||
//1- Word is too long for an empty line and must be broken | ||
@@ -103,6 +94,6 @@ case(wrapObj.lineLength < wordlength): | ||
//so can loop back to re-handle each word | ||
splitIndex = Breakword(word,wrapObj.lineLength); | ||
wrapObj.words.unshift(word.substr(0,splitIndex + 1)); //+1 for substr fn | ||
wrapObj.words.splice(1,0,word.substr(splitIndex + 1));//+1 for substr fn | ||
break; | ||
splitIndex = Breakword(word,wrapObj.lineLength) | ||
wrapObj.words.unshift(word.substr(0,splitIndex + 1)) //+1 for substr fn | ||
wrapObj.words.splice(1,0,word.substr(splitIndex + 1))//+1 for substr fn | ||
break | ||
@@ -112,40 +103,38 @@ //2- Word is too long for current line and must be wrapped | ||
//add a new line to our array of lines | ||
wrapObj.lines.push([]); | ||
wrapObj.lines.push([]) | ||
//note carriage to new line in counter | ||
wrapObj.currentLine++; | ||
wrapObj.currentLine++ | ||
//reset the spacesUsed to 0 | ||
wrapObj.spacesUsed = 0; | ||
wrapObj.spacesUsed = 0 | ||
/* falls through */ | ||
//3- Word fits on current line | ||
//caution: falls through | ||
default: | ||
//add word to line | ||
wrapObj.lines[wrapObj.currentLine].push(word); | ||
wrapObj.lines[wrapObj.currentLine].push(word) | ||
//reduce space remaining (add a space between words) | ||
wrapObj.spacesUsed += wordlength + 1; | ||
//increment iterator | ||
i++; | ||
wrapObj.spacesUsed += wordlength + 1 | ||
} | ||
} | ||
if(wrapObj.returnFormat === 'array'){ | ||
return wrapObj.lines; | ||
} | ||
else{ | ||
let lines = wrapObj.lines.map(function(line){ | ||
if(wrapObj.returnFormat === "array") { | ||
return wrapObj.lines | ||
} else{ | ||
let lines = wrapObj.lines.map(function(line) { | ||
//restore spaces to line | ||
line = line.join('\ '); | ||
line = line.join(" ") | ||
//add padding to ends of line | ||
if(!wrapObj.skipPadding){ | ||
line = Array(wrapObj.paddingLeft+1).join('\ ') + | ||
if(!wrapObj.skipPadding) { | ||
line = Array(wrapObj.paddingLeft+1).join(" ") + | ||
line + | ||
Array(wrapObj.paddingRight+1).join('\ '); | ||
Array(wrapObj.paddingRight+1).join(" ") | ||
} | ||
return line; | ||
}); | ||
return line | ||
}) | ||
//return as string | ||
return lines.join('\n'); | ||
return lines.join("\n") | ||
} | ||
} | ||
module.exports = smartWrap; | ||
module.exports = smartWrap |
#!/usr/bin/env node | ||
'use strict'; | ||
"use strict" | ||
let Smartwrap = require('./main.js'); | ||
let yargs = require('yargs'); | ||
yargs.option('minWidth', { | ||
let Smartwrap = require("./main.js") | ||
let yargs = require("yargs") | ||
yargs.option("minWidth", { | ||
choices: [1,2], | ||
default: 2, | ||
describe: '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.' | ||
}); | ||
yargs.option('paddingLeft', { | ||
describe: "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." | ||
}) | ||
yargs.option("paddingLeft", { | ||
default: 0, | ||
describe: 'Set the left padding of the output' | ||
}); | ||
yargs.option('paddingRight', { | ||
describe: "Set the left padding of the output" | ||
}) | ||
yargs.option("paddingRight", { | ||
default: 0, | ||
describe: 'Set the right padding of the output' | ||
}); | ||
yargs.option('splitAt', { | ||
describe: "Set the right padding of the output" | ||
}) | ||
yargs.option("splitAt", { | ||
default: [" ","\t"], | ||
describe: 'Characters at which to split input' | ||
}); | ||
yargs.option('trim', { | ||
describe: "Characters at which to split input" | ||
}) | ||
yargs.option("trim", { | ||
default: true, | ||
describe: 'Trim the whitespace from end of input' | ||
}); | ||
yargs.option('width', { | ||
alias: 'w', | ||
describe: "Trim the whitespace from end of input" | ||
}) | ||
yargs.option("width", { | ||
alias: "w", | ||
default: 10, | ||
describe: 'Set the line width of the output (in spaces)', | ||
describe: "Set the line width of the output (in spaces)", | ||
demandOption: true, | ||
coerce:function(arg){ | ||
coerce: function(arg) { | ||
if(isNaN(arg*1)) { | ||
throw new Error('Invalid width specified.'); | ||
throw new Error("Invalid width specified.") | ||
} | ||
return arg*1; | ||
return arg*1 | ||
} | ||
}); | ||
}) | ||
@@ -43,22 +43,22 @@ //create options object | ||
[ | ||
'minWidth', | ||
'paddingLeft', | ||
'paddingRight', | ||
'splitAt', | ||
'trim', | ||
'width' | ||
].forEach(function(key){ | ||
if(typeof yargs.argv[key] !== 'undefined'){ | ||
"minWidth", | ||
"paddingLeft", | ||
"paddingRight", | ||
"splitAt", | ||
"trim", | ||
"width" | ||
].forEach(function(key) { | ||
if(typeof yargs.argv[key] !== "undefined") { | ||
options[key] = yargs.argv[key] | ||
} | ||
}); | ||
}) | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function(chunk) { | ||
let out = Smartwrap(chunk,options); | ||
console.log(out); | ||
}); | ||
process.stdin.resume() | ||
process.stdin.setEncoding("utf8") | ||
process.stdin.on("data", function(chunk) { | ||
let out = Smartwrap(chunk,options) | ||
console.log(out) | ||
}) | ||
//yargs = yargs('h').argv; | ||
yargs.argv = yargs.help('h').argv; | ||
yargs.argv = yargs.help("h").argv |
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
42232
15
439
6
+ Addedyargs@14.2.3(transitive)
+ Addedyargs-parser@15.0.3(transitive)
- Removedyargs@13.3.2(transitive)
- Removedyargs-parser@13.1.2(transitive)
Updatedyargs@^14.2.0