Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magicpen

Package Overview
Dependencies
Maintainers
2
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magicpen - npm Package Compare versions

Comparing version 3.0.4 to 3.0.5

80

lib/TextSerializer.js
var duplicateText = require('./duplicateText');
var utils = require('./utils');
var ansiRegex = require('ansi-regex');
function stripAnsi(text) {
return text.replace(ansiRegex, '');
function createPadding(length) {
return { style: 'text', args: [duplicateText(' ', length)] };
}
function TextSerializer() {}
function flattenBlocksInOutputEntry(outputEntry) {
switch (outputEntry.style) {
case 'text': return String(outputEntry.args[0]).split('\n').map(function (line) {
var args = [line].concat(outputEntry.args.slice(1));
return [{ style: 'text', args: args }];
});
case 'block': return flattenBlocksInLines(outputEntry.args[0]);
default: return [];
}
}
TextSerializer.prototype.serialize = function (lines) {
return lines.map(this.serializeLine, this).join('\n');
};
function flattenBlocksInLine(line) {
if (line.length === 0) {
return [[]];
}
TextSerializer.prototype.serializeLine = function (line) {
var serializedLines = [''];
var result = [];
var startIndex = 0;
line.forEach(function (outputEntry, blockIndex) {
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry);
var inlineBlock = this[outputEntry.style] ?
this[outputEntry.style].apply(this, outputEntry.args) :
'';
var blockLines = flattenBlocksInOutputEntry(outputEntry);
var blockLinesLengths = blockLines.map(function (line) {
return utils.calculateLineSize(line).width;
});
var blockLines = String(inlineBlock).split('\n');
var longestLineLength = Math.max.apply(null, blockLinesLengths);
var padLines = blockIndex < line.length - 1;
blockLines.forEach(function (blockLine, index) {
if (!serializedLines[index]) {
serializedLines[index] = duplicateText(' ', startIndex);
if (!result[index]) {
result[index] = [createPadding(startIndex)];
}
var padding = '';
Array.prototype.push.apply(result[index], blockLine);
if (padLines) {
padding = duplicateText(' ', outputEntrySize.width - stripAnsi(blockLine).length);
result[index].push(createPadding(longestLineLength - blockLinesLengths[index]));
}
serializedLines[index] += blockLine + padding;
});
if (padLines) {
for (var i = blockLines.length; i < serializedLines.length; i += 1) {
var padding = duplicateText(' ', outputEntrySize.width);
serializedLines[i] += padding;
for (var i = blockLines.length; i < result.length; i += 1) {
result[i].push(createPadding(longestLineLength));
}
}
startIndex += outputEntrySize.width;
startIndex += longestLineLength;
}, this);
return result;
}
return serializedLines.join('\n');
function flattenBlocksInLines(lines) {
var result = [];
lines.forEach(function (line) {
flattenBlocksInLine(line).forEach(function (line) {
result.push(line);
});
});
return result;
}
function TextSerializer() {}
TextSerializer.prototype.serialize = function (lines) {
lines = flattenBlocksInLines(lines);
return lines.map(this.serializeLine, this).join('\n');
};
TextSerializer.prototype.serializeLine = function (line) {
return line.map(function (outputEntry) {
return this[outputEntry.style] ?
this[outputEntry.style].apply(this, outputEntry.args) :
'';
}, this).join('');
};
TextSerializer.prototype.text = function (content) {

@@ -55,0 +85,0 @@ return content;

@@ -27,5 +27,5 @@ /*!

var TextSerializer = require(4);
var colorDiff = require(12);
var colorDiff = require(11);
var ansiStyles = utils.extend({}, require(8));
var ansiStyles = utils.extend({}, require(7));
Object.keys(ansiStyles).forEach(function (styleName) {

@@ -268,3 +268,3 @@ ansiStyles[styleName.toLowerCase()] = ansiStyles[styleName];

MagicPen.defaultFormat = 'html'; // Browser
} else if (require(14)) {
} else if (require(13)) {
MagicPen.defaultFormat = 'ansi'; // colored console

@@ -640,53 +640,83 @@ } else {

var utils = require(6);
var ansiRegex = require(7);
function stripAnsi(text) {
return text.replace(ansiRegex, '');
function createPadding(length) {
return { style: 'text', args: [duplicateText(' ', length)] };
}
function TextSerializer() {}
function flattenBlocksInOutputEntry(outputEntry) {
switch (outputEntry.style) {
case 'text': return String(outputEntry.args[0]).split('\n').map(function (line) {
var args = [line].concat(outputEntry.args.slice(1));
return [{ style: 'text', args: args }];
});
case 'block': return flattenBlocksInLines(outputEntry.args[0]);
default: return [];
}
}
TextSerializer.prototype.serialize = function (lines) {
return lines.map(this.serializeLine, this).join('\n');
};
function flattenBlocksInLine(line) {
if (line.length === 0) {
return [[]];
}
TextSerializer.prototype.serializeLine = function (line) {
var serializedLines = [''];
var result = [];
var startIndex = 0;
line.forEach(function (outputEntry, blockIndex) {
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry);
var inlineBlock = this[outputEntry.style] ?
this[outputEntry.style].apply(this, outputEntry.args) :
'';
var blockLines = flattenBlocksInOutputEntry(outputEntry);
var blockLinesLengths = blockLines.map(function (line) {
return utils.calculateLineSize(line).width;
});
var blockLines = String(inlineBlock).split('\n');
var longestLineLength = Math.max.apply(null, blockLinesLengths);
var padLines = blockIndex < line.length - 1;
blockLines.forEach(function (blockLine, index) {
if (!serializedLines[index]) {
serializedLines[index] = duplicateText(' ', startIndex);
if (!result[index]) {
result[index] = [createPadding(startIndex)];
}
var padding = '';
Array.prototype.push.apply(result[index], blockLine);
if (padLines) {
padding = duplicateText(' ', outputEntrySize.width - stripAnsi(blockLine).length);
result[index].push(createPadding(longestLineLength - blockLinesLengths[index]));
}
serializedLines[index] += blockLine + padding;
});
if (padLines) {
for (var i = blockLines.length; i < serializedLines.length; i += 1) {
var padding = duplicateText(' ', outputEntrySize.width);
serializedLines[i] += padding;
for (var i = blockLines.length; i < result.length; i += 1) {
result[i].push(createPadding(longestLineLength));
}
}
startIndex += outputEntrySize.width;
startIndex += longestLineLength;
}, this);
return result;
}
return serializedLines.join('\n');
function flattenBlocksInLines(lines) {
var result = [];
lines.forEach(function (line) {
flattenBlocksInLine(line).forEach(function (line) {
result.push(line);
});
});
return result;
}
function TextSerializer() {}
TextSerializer.prototype.serialize = function (lines) {
lines = flattenBlocksInLines(lines);
return lines.map(this.serializeLine, this).join('\n');
};
TextSerializer.prototype.serializeLine = function (line) {
return line.map(function (outputEntry) {
return this[outputEntry.style] ?
this[outputEntry.style].apply(this, outputEntry.args) :
'';
}, this).join('');
};
TextSerializer.prototype.text = function (content) {

@@ -783,9 +813,3 @@ return content;

'use strict';
module.exports = function () {
return /(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/g;
};
},{}],8:[function(require,module,exports){
'use strict';
var styles = module.exports = {

@@ -846,3 +870,3 @@ modifiers: {

},{}],9:[function(require,module,exports){
},{}],8:[function(require,module,exports){
// shim for using process in browser

@@ -912,3 +936,3 @@

},{}],10:[function(require,module,exports){
},{}],9:[function(require,module,exports){
/**

@@ -1028,3 +1052,3 @@ * @author Markus Näsman

},{}],11:[function(require,module,exports){
},{}],10:[function(require,module,exports){
/**

@@ -1195,8 +1219,8 @@ * @author Markus Näsman

},{}],12:[function(require,module,exports){
},{}],11:[function(require,module,exports){
'use strict';
var diff = require(11);
var convert = require(10);
var palette = require(13);
var diff = require(10);
var convert = require(9);
var palette = require(12);

@@ -1226,3 +1250,3 @@ var color = module.exports = {};

},{}],13:[function(require,module,exports){
},{}],12:[function(require,module,exports){
/**

@@ -1265,4 +1289,4 @@ * @author Markus Näsman

*/
var color_diff = require(11);
var color_convert = require(10);
var color_diff = require(10);
var color_convert = require(9);

@@ -1337,3 +1361,3 @@ /**

},{}],14:[function(require,module,exports){
},{}],13:[function(require,module,exports){
(function (process){

@@ -1380,4 +1404,4 @@ 'use strict';

}).call(this,require(9))
}).call(this,require(8))
},{}]},{},[3])(3)
});
{
"name": "magicpen",
"version": "3.0.4",
"version": "3.0.5",
"description": "Styled output in both consoles and browsers",

@@ -34,3 +34,2 @@ "main": "./lib/MagicPen.js",

"dependencies": {
"ansi-regex": "^1.1.0",
"ansi-styles": "^2.0.0",

@@ -37,0 +36,0 @@ "color-diff": "^0.1.4",

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