Socket
Socket
Sign inDemoInstall

smartwrap

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smartwrap - npm Package Compare versions

Comparing version 1.2.5 to 2.0.0

test/data.js

5

package.json
{
"name": "smartwrap",
"version": "1.2.5",
"version": "2.0.0",
"description": "Textwrap for javascript/nodejs. Correctly handles wide characters (宽字符) and emojis (😃). Wraps strings with option to break on words.",
"main": "src/main.js",
"engines": {
"node": ">=11.0.0"
},
"bin": {

@@ -7,0 +10,0 @@ "smartwrap": "src/terminal-adapter.js"

46

src/main.js

@@ -5,3 +5,2 @@ const breakword = require("breakword")

const ANSIPattern = [

@@ -153,6 +152,7 @@ "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",

lines = lines.map( line => {
lines = lines.map( line => {
// restore spaces to line
line = line.join(" ")
// add padding to ends of line

@@ -164,6 +164,6 @@ if(!config.skipPadding) {

}
return line
})
return lines.join("\n")

@@ -264,32 +264,28 @@ }

module.exports = (input, options) => {
// in case a template literal was passed that has newling characters,
// split string by newlines and process each resulting string
let str = input.toString()
// save input ANSI escape codes to be restored later
const savedANSI = splitAnsiInput(str)
// process each existing line separately to respect existing line breaks
const processedLines = input.toString().split("\n").map( string => {
// strip ANSI
str = stripansi(str)
// save input ANSI escape codes to be restored later
const savedANSI = splitAnsiInput(string)
// convert input to array, each element a line
const linesArr = str.split("\n").map( string => {
return wrap(string, options)
})
// strip ANSI
string = stripansi(string)
// return linesArr.join("\n")
// add newlines to string
string = wrap(string, options)
// --- following code re-applies ANSI
// convert line arrays to a single string broken by return characters
const wrappedStr = linesArr.join("\n")
// convert into array of characters
let charArr = [...string]
// and an ANSI closing character, so style never bleeds
// const wrappedStr = linesArr.join(`\u001b\[0m\n`)
// restore input ANSI escape codes
charArr = (savedANSI.length > 0) ? restoreANSI(savedANSI, charArr) : charArr
// break that line into single characters
let wrappedArr = [...wrappedStr]
// convert array of single characters into array of lines
let outArr = charArr.join("").split("\n")
// restore input ANSI escape codes if needed
wrappedArr = (savedANSI.length > 0) ? restoreANSI(savedANSI, wrappedArr) : wrappedArr
return wrappedArr.join("")
return outArr
})
return processedLines.flat(2).join("\n")
}

@@ -1,81 +0,20 @@

// iterate array pointer in both arrays to first matching element
// savedArr: remove and copy all elements (e0) preceding match
// processedArr: insert (e0) before pointer
// repeat
//
// if savedArr has elements remaining after loop complete, append to processedArr
let smartwrap = require("../")
let chalk = require("chalk")
let string = `The use of the secure-rm CLI is deprecated.
Migrate over -> secure-rm-cli. Run:
let processedArr = ["r", "e", "\n", "d"]
// let savedArr = [
// "\033[34m", "r", "\033[0m",
// "\033[34m", "e", "\033[0m",
// "\033[34m", "d", "\033[0m",
// ]
npm un secure-rm -g
npm i secure-rm-cli -g`
const ANSIPattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
].join('|');
const ANSIRegex = new RegExp(ANSIPattern, "g")
const ANSICloseRegex = new RegExp(`\\u001b\\[0m`)
let string2 = "The use of the secure-rm CLI is deprecated.\nMigrate over -> secure-rm-cli. Run:\nnpm un secure-rm -g\nnpm i secure-rm-cli -g"
string = chalk['yellow'](string)
const outstring = smartwrap(string, {
minWidth: 1,
trim: true,
width: 70
})
let text = "\033[34mr\033[39m\033[33me\033[0m\033[34md\033[0m"
// get start and end positions for matches
let matches = []
while((result = ANSIRegex.exec(text)) !== null) {
matches.push({
start: result.index,
end: result.index + result[0].length,
match: result[0]
})
}
// add start and end positions for non matches
matches = matches.reduce((prev, curr) => {
// check if space exists between this and last match
// get end of previous match
let prevEnd = prev[prev.length -1]
if (prevEnd.end < curr.start) {
prev.push({ start: prevEnd.end, end: curr.start }, curr)
}
else {
prev.push(curr)
}
return prev
},[{start:0, end:0}])
.splice(1) // removes starting accumulator object
// get regex pattern for each split point
let splitStr = matches.map( value => `(.{${value.end - value.start}})` )
.join('')
// now we have an array of all ansi escaped and non-ansi escaped strings
let savedArr = new RegExp(`^${splitStr}`).exec(text).splice(1)
console.log(savedArr, processedArr)
// process.exit()
let restoredANSI = processedArr.map((char) => {
if (char === '\n') return char
let splicePoint = savedArr.findIndex(element => element === char) + 1
let result = savedArr.splice(0, splicePoint)
// add all consecutive closing tags
while (ANSICloseRegex.test(savedArr[0])){
result.push(savedArr.shift())
}
return result.join("")
}).concat(savedArr)
console.log(restoredANSI)
console.log(restoredANSI.join(""))
console.log(outstring)

@@ -11,3 +11,3 @@ 'use strict';

const smartwrap = require("../");
const filepath = `${__dirname}/tests.json`;
const data = require("./data")

@@ -20,8 +20,3 @@ let test = function(testResult,savedResult){

//get test list
let str = fs.readFileSync(filepath, {encoding : 'utf-8'});
let obj = JSON.parse(str);
for(let i in obj){
for(let i in data){

@@ -38,10 +33,10 @@ //generate new output

].forEach( element => {
if (typeof obj[i][element] !== 'undefined') {
options[element] = obj[i][element];
if (typeof data[i][element] !== 'undefined') {
options[element] = data[i][element];
}
});
let testResult = smartwrap(obj[i].input,options);
let testResult = smartwrap(data[i].input,options);
console.log("Test Properties:",obj[i]);
console.log("Test Properties:",data[i]);
console.log("12345678901234567890");

@@ -55,3 +50,3 @@ console.log("BEGIN---------------");

//save tests
obj[i].output = testResult;
data[i].output = testResult;
break;

@@ -64,3 +59,3 @@ case(typeof global.display !== 'undefined' && global.display):

describe('Test '+i, () => {
test(testResult, obj[i].output);
test(testResult, data[i].output);
})

@@ -71,4 +66,4 @@ }

if(typeof global.save !== 'undefined' && global.save){
//write saved object to file
fs.writeFileSync(filepath, JSON.stringify(obj, null, 2), 'utf8');
//write saved dataect to file
fs.writeFileSync(filepath, JSON.stringify(data, null, 2), 'utf8');
console.log("Tests saved to file.");

@@ -75,0 +70,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc