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 4.8.2 to 4.8.3

4

lib/ColoredConsoleSerializer.js

@@ -79,6 +79,6 @@ var cssStyles = require('./cssStyles');

ColoredConsoleSerializer.prototype.raw = function (content) {
return String(content);
ColoredConsoleSerializer.prototype.raw = function (options) {
return String(options.content(this));
};
module.exports = ColoredConsoleSerializer;

@@ -68,6 +68,6 @@ var cssStyles = require('./cssStyles');

HtmlSerializer.prototype.raw = function (content) {
return String(content);
HtmlSerializer.prototype.raw = function (options) {
return String(options.content(this));
};
module.exports = HtmlSerializer;

@@ -7,3 +7,2 @@ /*global window*/

var cssStyles = require('./cssStyles');
var RawContentConverter = require('./RawContentConverter');

@@ -17,2 +16,6 @@ function MagicPen(options) {

if (typeof options === "string") {
options = {format: options };
}
var indentationWidth = 'indentationWidth' in options ?

@@ -28,2 +31,5 @@ options.indentationWidth : 2;

this.preferredWidth = (!process.browser && process.stdout.columns) || 80;
if (options.format) {
this.format = options.format;
}
}

@@ -103,22 +109,2 @@

MagicPen.prototype.getContentFromValue = function (value) {
var clone;
if (value.isMagicPen) {
return value;
} else if (typeof value === 'function') {
clone = this.clone();
value.call(clone, clone);
return clone;
} else if (typeof value === 'string') {
clone = this.clone();
clone.text(value);
return clone;
} else {
throw new Error('Requires the arguments to be:\n' +
'a pen or\n' +
'a callback appending content to a pen or\n' +
'a text');
}
};
MagicPen.prototype.write = function (options) {

@@ -171,3 +157,7 @@ if (this.styles[options.style]) {

MagicPen.prototype.toString = function (format) {
format = format || 'text';
if (format && this.format && format !== this.format) {
throw new Error('A pen with format: ' + this.format + ' cannot be serialized to: ' + format);
}
format = this.format || format || 'text';
if (format === 'auto') {

@@ -178,5 +168,3 @@ format = MagicPen.defaultFormat;

var serializer = new MagicPen.serializers[format](theme);
var rawContentConverter = new RawContentConverter(serializer);
var lines = rawContentConverter.convertLines(this.output);
return serializer.serialize(lines);
return serializer.serialize(this.output);
};

@@ -228,2 +216,3 @@

if (args[0].isMagicPen) {
this.ensureCompatibleFormat(args[0].format);
return args[0];

@@ -256,2 +245,8 @@ } else if (typeof args[0] === 'function') {

MagicPen.prototype.ensureCompatibleFormat = function (format) {
if (format && this.format && format !== this.format) {
throw new Error('This pen is only compatible with the format: ' + this.format);
}
};
MagicPen.prototype.block = function () {

@@ -265,70 +260,61 @@ var pen = this.getContentFromArguments(arguments);

};
function isRawOutput(options) {
return typeof options.width === 'number' &&
typeof options.height === 'number' && (
typeof options.content === 'function' ||
typeof options.content === 'string'
);
}
MagicPen.prototype.getContentFromValue = function (value) {
var clone;
if (value.isMagicPen) {
return value;
} else if (typeof value === 'function') {
clone = this.clone();
value.call(clone, clone);
return clone;
} else if (typeof value === 'string') {
clone = this.clone();
clone.text(value);
return clone;
} else {
throw new Error('Requires the arguments to be:\n' +
'a pen or\n' +
'a callback appending content to a pen or\n' +
'a text');
}
};
MagicPen.prototype.raw = function (options) {
if (!options || typeof options.fallback === 'undefined') {
throw new Error('Requires the argument to be an object with atleast an fallback key');
var format = this.format;
if (!format) {
throw new Error('The raw method is only supported on pen where the format has already been set');
}
var that = this;
options = extend({}, options);
Object.keys(options).forEach(function (mode) {
if (mode !== 'fallback') {
var propertyType = typeof options[mode];
if (propertyType === 'function') {
var outputMethod = options[mode];
options[mode] = function () {
var args = Array.prototype.slice.call(arguments);
var pen = that.clone();
var originalRaw = pen.raw;
pen.raw = function (arg) {
if (typeof arg !== 'object') {
var content = arg;
arg = {
fallback: ''
};
arg[mode] = content;
}
return originalRaw.call(this, arg);
};
args.unshift(pen);
var result = outputMethod.apply(pen, args);
if (typeof result !== 'undefined' && !pen.isEmpty()) {
throw new Error('Raw content for modes must eigther write to a pen or return a raw string');
} else if (typeof result === 'undefined') {
return pen;
} else {
return result;
}
};
} else {
var output = options[mode];
options[mode] = function () {
return output;
};
var outputProperty = options[format] || options.fallback;
if (!outputProperty) {
throw new Error('Raw output is not specified for format: ' + format + ' and no fallback method is given');
}
var propertyType = typeof outputProperty;
if (outputProperty && outputProperty.isMagicPen) {
return this.append(outputProperty);
}
if (propertyType === 'string') {
return this.text(outputProperty);
}
if (propertyType === 'function') {
var pen = this.clone();
var originalRaw = pen.raw;
pen.raw = function (arg) {
if (isRawOutput(arg) || typeof arg !== 'object') {
var content = arg;
arg = {};
arg[format] = content;
}
return originalRaw.call(this, arg);
};
outputProperty.call(pen, pen);
return this.append(pen);
}
if (isRawOutput(outputProperty)) {
if (typeof outputProperty.content === 'string') {
outputProperty = extend({}, outputProperty);
var content = outputProperty.content;
outputProperty.content = function () {
return content;
};
}
});
options.fallback = that.getContentFromValue(options.fallback);
return this.write({ style: 'raw', args: [options]});
return this.write({ style: 'raw', args: [outputProperty] });
}
throw new Error('Properties on a raw object must be a pen, a function that writes to a pen, a string or an object with the structure\n' +
'{ width: <number>, height: <number>, content: <string function() {}|string> }');
};

@@ -435,3 +421,5 @@

MagicPen.prototype.clone = function () {
MagicPen.prototype.clone = function (format) {
this.ensureCompatibleFormat(format);
function MagicPenClone() {}

@@ -445,2 +433,3 @@ MagicPenClone.prototype = this;

clonedPen._themes = this._themes;
clonedPen.format = format || this.format;
return clonedPen;

@@ -447,0 +436,0 @@ };

@@ -28,4 +28,4 @@ var flattenBlocksInLines = require('./flattenBlocksInLines');

TextSerializer.prototype.raw = function (content) {
return String(content);
TextSerializer.prototype.raw = function (options) {
return String(options.content(this));
};

@@ -32,0 +32,0 @@

@@ -18,2 +18,5 @@ var utils = {

return utils.calculateSize(outputEntry.args[0]);
case 'raw':
var arg = outputEntry.args[0];
return { width: arg.width, height: arg.height };
default: return { width: 0, height: 0 };

@@ -20,0 +23,0 @@ }

@@ -25,7 +25,7 @@ /*!

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),(o.weknowhow||(o.weknowhow={})).MagicPen=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var utils = require(12);
var TextSerializer = require(6);
var colorDiff = require(17);
var rgbRegexp = require(10);
var themeMapper = require(11);
var utils = require(11);
var TextSerializer = require(5);
var colorDiff = require(16);
var rgbRegexp = require(9);
var themeMapper = require(10);

@@ -35,3 +35,3 @@ var cacheSize = 0;

var ansiStyles = utils.extend({}, require(13));
var ansiStyles = utils.extend({}, require(12));
Object.keys(ansiStyles).forEach(function (styleName) {

@@ -171,6 +171,6 @@ ansiStyles[styleName.toLowerCase()] = ansiStyles[styleName];

},{}],2:[function(require,module,exports){
var cssStyles = require(7);
var flattenBlocksInLines = require(9);
var rgbRegexp = require(10);
var themeMapper = require(11);
var cssStyles = require(6);
var flattenBlocksInLines = require(8);
var rgbRegexp = require(9);
var themeMapper = require(10);

@@ -250,4 +250,4 @@ function ColoredConsoleSerializer(theme) {

ColoredConsoleSerializer.prototype.raw = function (content) {
return String(content);
ColoredConsoleSerializer.prototype.raw = function (options) {
return String(options.content(this));
};

@@ -258,5 +258,5 @@

},{}],3:[function(require,module,exports){
var cssStyles = require(7);
var rgbRegexp = require(10);
var themeMapper = require(11);
var cssStyles = require(6);
var rgbRegexp = require(9);
var themeMapper = require(10);

@@ -326,4 +326,4 @@ function HtmlSerializer(theme) {

HtmlSerializer.prototype.raw = function (content) {
return String(content);
HtmlSerializer.prototype.raw = function (options) {
return String(options.content(this));
};

@@ -336,8 +336,7 @@

/*global window*/
var utils = require(12);
var utils = require(11);
var extend = utils.extend;
var duplicateText = require(8);
var rgbRegexp = require(10);
var cssStyles = require(7);
var RawContentConverter = require(5);
var duplicateText = require(7);
var rgbRegexp = require(9);
var cssStyles = require(6);

@@ -351,2 +350,6 @@ function MagicPen(options) {

if (typeof options === "string") {
options = {format: options };
}
var indentationWidth = 'indentationWidth' in options ?

@@ -362,2 +365,5 @@ options.indentationWidth : 2;

this.preferredWidth = (!process.browser && process.stdout.columns) || 80;
if (options.format) {
this.format = options.format;
}
}

@@ -371,3 +377,3 @@

}
} else if (require(19)) {
} else if (require(18)) {
MagicPen.defaultFormat = 'ansi'; // colored console

@@ -395,3 +401,3 @@ } else {

[
require(6),
require(5),
require(3),

@@ -439,22 +445,2 @@ require(1),

MagicPen.prototype.getContentFromValue = function (value) {
var clone;
if (value.isMagicPen) {
return value;
} else if (typeof value === 'function') {
clone = this.clone();
value.call(clone, clone);
return clone;
} else if (typeof value === 'string') {
clone = this.clone();
clone.text(value);
return clone;
} else {
throw new Error('Requires the arguments to be:\n' +
'a pen or\n' +
'a callback appending content to a pen or\n' +
'a text');
}
};
MagicPen.prototype.write = function (options) {

@@ -507,3 +493,7 @@ if (this.styles[options.style]) {

MagicPen.prototype.toString = function (format) {
format = format || 'text';
if (format && this.format && format !== this.format) {
throw new Error('A pen with format: ' + this.format + ' cannot be serialized to: ' + format);
}
format = this.format || format || 'text';
if (format === 'auto') {

@@ -514,5 +504,3 @@ format = MagicPen.defaultFormat;

var serializer = new MagicPen.serializers[format](theme);
var rawContentConverter = new RawContentConverter(serializer);
var lines = rawContentConverter.convertLines(this.output);
return serializer.serialize(lines);
return serializer.serialize(this.output);
};

@@ -564,2 +552,3 @@

if (args[0].isMagicPen) {
this.ensureCompatibleFormat(args[0].format);
return args[0];

@@ -592,2 +581,8 @@ } else if (typeof args[0] === 'function') {

MagicPen.prototype.ensureCompatibleFormat = function (format) {
if (format && this.format && format !== this.format) {
throw new Error('This pen is only compatible with the format: ' + this.format);
}
};
MagicPen.prototype.block = function () {

@@ -601,70 +596,61 @@ var pen = this.getContentFromArguments(arguments);

};
function isRawOutput(options) {
return typeof options.width === 'number' &&
typeof options.height === 'number' && (
typeof options.content === 'function' ||
typeof options.content === 'string'
);
}
MagicPen.prototype.getContentFromValue = function (value) {
var clone;
if (value.isMagicPen) {
return value;
} else if (typeof value === 'function') {
clone = this.clone();
value.call(clone, clone);
return clone;
} else if (typeof value === 'string') {
clone = this.clone();
clone.text(value);
return clone;
} else {
throw new Error('Requires the arguments to be:\n' +
'a pen or\n' +
'a callback appending content to a pen or\n' +
'a text');
}
};
MagicPen.prototype.raw = function (options) {
if (!options || typeof options.fallback === 'undefined') {
throw new Error('Requires the argument to be an object with atleast an fallback key');
var format = this.format;
if (!format) {
throw new Error('The raw method is only supported on pen where the format has already been set');
}
var that = this;
options = extend({}, options);
Object.keys(options).forEach(function (mode) {
if (mode !== 'fallback') {
var propertyType = typeof options[mode];
if (propertyType === 'function') {
var outputMethod = options[mode];
options[mode] = function () {
var args = Array.prototype.slice.call(arguments);
var pen = that.clone();
var originalRaw = pen.raw;
pen.raw = function (arg) {
if (typeof arg !== 'object') {
var content = arg;
arg = {
fallback: ''
};
arg[mode] = content;
}
return originalRaw.call(this, arg);
};
args.unshift(pen);
var result = outputMethod.apply(pen, args);
if (typeof result !== 'undefined' && !pen.isEmpty()) {
throw new Error('Raw content for modes must eigther write to a pen or return a raw string');
} else if (typeof result === 'undefined') {
return pen;
} else {
return result;
}
};
} else {
var output = options[mode];
options[mode] = function () {
return output;
};
var outputProperty = options[format] || options.fallback;
if (!outputProperty) {
throw new Error('Raw output is not specified for format: ' + format + ' and no fallback method is given');
}
var propertyType = typeof outputProperty;
if (outputProperty && outputProperty.isMagicPen) {
return this.append(outputProperty);
}
if (propertyType === 'string') {
return this.text(outputProperty);
}
if (propertyType === 'function') {
var pen = this.clone();
var originalRaw = pen.raw;
pen.raw = function (arg) {
if (isRawOutput(arg) || typeof arg !== 'object') {
var content = arg;
arg = {};
arg[format] = content;
}
return originalRaw.call(this, arg);
};
outputProperty.call(pen, pen);
return this.append(pen);
}
if (isRawOutput(outputProperty)) {
if (typeof outputProperty.content === 'string') {
outputProperty = extend({}, outputProperty);
var content = outputProperty.content;
outputProperty.content = function () {
return content;
};
}
});
options.fallback = that.getContentFromValue(options.fallback);
return this.write({ style: 'raw', args: [options]});
return this.write({ style: 'raw', args: [outputProperty] });
}
throw new Error('Properties on a raw object must be a pen, a function that writes to a pen, a string or an object with the structure\n' +
'{ width: <number>, height: <number>, content: <string function() {}|string> }');
};

@@ -771,3 +757,5 @@

MagicPen.prototype.clone = function () {
MagicPen.prototype.clone = function (format) {
this.ensureCompatibleFormat(format);
function MagicPenClone() {}

@@ -781,2 +769,3 @@ MagicPenClone.prototype = this;

clonedPen._themes = this._themes;
clonedPen.format = format || this.format;
return clonedPen;

@@ -967,46 +956,6 @@ };

}).call(this,require(14))
}).call(this,require(13))
},{}],5:[function(require,module,exports){
function RawContentConverter(serializer) {
var that = this;
var flattenBlocksInLines = require(8);
that.convertOutputEntry = function(outputEntry) {
var content = outputEntry.args[0];
switch (outputEntry.style) {
case 'raw':
var format = serializer.format;
if (format in content) {
var result = content[format](serializer);
if (result.isMagicPen) {
return { style: 'block', args: [that.convertLines(result.output)] };
} else {
return { style: 'raw', args: [result] };
}
} else {
return { style: 'block', args: [that.convertLines(content.fallback.output)] };
}
break;
case 'block': return {
style: 'block',
args: [that.convertLines(content)]
};
default: return outputEntry;
}
};
that.convertLine = function(line) {
return line.map(that.convertOutputEntry);
};
that.convertLines = function(lines) {
var that = this;
return lines.map(that.convertLine);
};
}
module.exports = RawContentConverter;
},{}],6:[function(require,module,exports){
var flattenBlocksInLines = require(9);
function TextSerializer() {}

@@ -1037,4 +986,4 @@

TextSerializer.prototype.raw = function (content) {
return String(content);
TextSerializer.prototype.raw = function (options) {
return String(options.content(this));
};

@@ -1045,3 +994,3 @@

},{}],7:[function(require,module,exports){
},{}],6:[function(require,module,exports){
var cssStyles = {

@@ -1082,3 +1031,3 @@ bold: 'font-weight: bold',

},{}],8:[function(require,module,exports){
},{}],7:[function(require,module,exports){
var whitespaceCacheLength = 256;

@@ -1119,5 +1068,5 @@ var whitespaceCache = [''];

},{}],9:[function(require,module,exports){
var utils = require(12);
var duplicateText = require(8);
},{}],8:[function(require,module,exports){
var utils = require(11);
var duplicateText = require(7);

@@ -1204,6 +1153,6 @@ function createPadding(length) {

},{}],10:[function(require,module,exports){
},{}],9:[function(require,module,exports){
module.exports = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
},{}],11:[function(require,module,exports){
},{}],10:[function(require,module,exports){
module.exports = function (theme, args) {

@@ -1232,3 +1181,3 @@ if (args.length === 2) {

},{}],12:[function(require,module,exports){
},{}],11:[function(require,module,exports){
var utils = {

@@ -1251,2 +1200,5 @@ extend: function (target) {

return utils.calculateSize(outputEntry.args[0]);
case 'raw':
var arg = outputEntry.args[0];
return { width: arg.width, height: arg.height };
default: return { width: 0, height: 0 };

@@ -1302,3 +1254,3 @@ }

},{}],13:[function(require,module,exports){
},{}],12:[function(require,module,exports){
'use strict';

@@ -1361,3 +1313,3 @@

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

@@ -1422,3 +1374,3 @@

},{}],15:[function(require,module,exports){
},{}],14:[function(require,module,exports){
/**

@@ -1538,3 +1490,3 @@ * @author Markus Ekholm

},{}],16:[function(require,module,exports){
},{}],15:[function(require,module,exports){
/**

@@ -1705,8 +1657,8 @@ * @author Markus Ekholm

},{}],17:[function(require,module,exports){
},{}],16:[function(require,module,exports){
'use strict';
var diff = require(16);
var convert = require(15);
var palette = require(18);
var diff = require(15);
var convert = require(14);
var palette = require(17);

@@ -1736,3 +1688,3 @@ var color = module.exports = {};

},{}],18:[function(require,module,exports){
},{}],17:[function(require,module,exports){
/**

@@ -1775,4 +1727,4 @@ * @author Markus Ekholm

*/
var color_diff = require(16);
var color_convert = require(15);
var color_diff = require(15);
var color_convert = require(14);

@@ -1847,3 +1799,3 @@ /**

},{}],19:[function(require,module,exports){
},{}],18:[function(require,module,exports){
(function (process){

@@ -1890,4 +1842,4 @@ 'use strict';

}).call(this,require(14))
}).call(this,require(13))
},{}]},{},[4])(4)
});
{
"name": "magicpen",
"version": "4.8.2",
"version": "4.8.3",
"description": "Styled output in both consoles and browsers",

@@ -5,0 +5,0 @@ "main": "./lib/MagicPen.js",

@@ -107,5 +107,9 @@ # MagicPen

Currently there is only on option: `indentationWidth` which defaults
to 2.
Currently there is only two options: `indentationWidth` which defaults
to 2 and `format`.
Given magicpen a string is equivalent to specifying the format in the options object:
Specifying the format is useful in combination with the `raw` method.
Example:

@@ -116,2 +120,4 @@

magicpen();
// Pen with indentation width 2 and in ansi format
magicpen('ansi');
// Pen with indentation width 4

@@ -175,3 +181,3 @@ magicpen({ indentationWidth: 4 });

### toString(format = 'text')
### toString([format])

@@ -182,2 +188,7 @@ Returns the content of the pen in the specified format.

It fails if the pen has another format set already.
If no format is specified, it will use the format of the pen or `text`
if the pen does not have a format set.
### newline(count = 1), nl(count = 1)

@@ -406,3 +417,3 @@

### clone()
### clone([format])

@@ -413,2 +424,6 @@ Returns a clone of the current pen with an empty output buffer. This

If a format is given, the pen cloned pen will have that format. This
is useful in combination with the `raw` method. It will fail if the
format of the pen has already been set.
### addStyle(style, handler)

@@ -459,11 +474,28 @@

If you need something completely custom, you can specify the actual
string that will be serialized for each of the different modes. You
need to specify a fallback, for the modes that are not specified.
string content that will be used for each format. You can even output
raw content to the serializer.
The custom output is generated at serialization time and can be a
string or a function returning a string with raw content. Furthermore
you can write to the pen bound to `this` that will also be given as
the first parameter. The fallback can be a pen, a function where
this is a pen or a string.
Notice that you must specify the format of the pen before using this method
either at construction time or when cloning.
The format specific content can be of the following forms:
* A string, that will just be appended to the pen without styling.
* A method that will append content to the pen bound to `this`.
* A method that will append to the pen given as the first parameter.
* An raw object of the following structure
{
width: <number>,
height: <number>,
content: <string function() {}|string>
}
If a method is given as the content it will be called at serialization time.
If the format of the pen is not defined the fallback property will be used.
The fallback method can be specified the following way:
* As string, that will just be appended to the pen without styling.
* As method that will append content to the pen bound to `this`.
* As method that will append to the pen given as the first parameter.
```js

@@ -479,8 +511,13 @@ var pen = magicpen();

},
html: function () {
return '<a href="' + url + '" alt="' + label + '">' + label + '</a>';
html: {
height: 1,
width: label.length,
content: '<a href="' + url + '" alt="' + label + '">' + label + '</a>'
}
});
});
pen.link('magicpen', 'https://github.com/sunesimonsen/magicpen');
pen.clone('text').link('magicpen', 'https://github.com/sunesimonsen/magicpen');
pen.clone('ansi').link('magicpen', 'https://github.com/sunesimonsen/magicpen');
pen.clone('html').link('magicpen', 'https://github.com/sunesimonsen/magicpen');
```

@@ -515,7 +552,11 @@

this.text('Hello');
this.raw('<canvas id="whoa"></canvas>').nl();
this.raw({
height: 100,
width: 100,
content: '<canvas id="whoa"></canvas>'
}).nl();
this.indentLines()
this.i().block(function () {
this.raw(function () {
return 'it even works in blocks';
this.text('it even works in blocks');
});

@@ -522,0 +563,0 @@ });

Sorry, the diff of this file is too big to display

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