Comparing version 1.0.1 to 2.0.0
@@ -90,4 +90,3 @@ var utils = require('./utils'); | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = stylesString.split(/\s*,\s*/); | ||
var styles = Array.prototype.slice.call(arguments, 1); | ||
styles.forEach(function (styleName) { | ||
@@ -94,0 +93,0 @@ if (ansiStyles[styleName]) { |
@@ -70,5 +70,5 @@ var htmlStyles = { | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = Array.prototype.slice.call(arguments, 1); | ||
var styleProperties = []; | ||
stylesString.split(/\s*,\s*/).forEach(function (styleName) { | ||
styles.forEach(function (styleName) { | ||
if (rgbRegexp.test(styleName)) { | ||
@@ -75,0 +75,0 @@ if (styleName.substring(0, 2) === 'bg') { |
@@ -6,6 +6,2 @@ /*global window*/ | ||
function isOutputEntry(obj) { | ||
return typeof obj === 'object' && 'style' in obj && 'args' in obj; | ||
} | ||
function MagicPen(options) { | ||
@@ -58,4 +54,16 @@ if (!(this instanceof MagicPen)) { | ||
function hasSameTextStyling(a, b) { | ||
if (!a || !b || a.style !== 'text' || b.style !== 'text') { | ||
return false; | ||
} | ||
return utils.arrayEquals(Array.prototype.slice.call(a.args, 1), | ||
Array.prototype.slice.call(b.args, 1)); | ||
} | ||
MagicPen.prototype.write = function () { | ||
if (arguments.length === 1 && isOutputEntry(arguments[0])) { | ||
if (typeof arguments[0] === 'string') { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return this.write({ style: arguments[0], args: args }); | ||
} else { | ||
var options = arguments[0]; | ||
@@ -67,7 +75,12 @@ if (this.styles[options.style]) { | ||
this.output[0] = this.output[0] || []; | ||
this.output[this.output.length - 1].push(options); | ||
var lastLine = this.output[this.output.length - 1]; | ||
var lastEntry = lastLine[lastLine.length - 1]; | ||
if (hasSameTextStyling(lastEntry, options)) { | ||
options.args[0] = lastEntry.args[0] + options.args[0]; | ||
lastLine[lastLine.length - 1] = options; | ||
} else { | ||
lastLine.push(options); | ||
} | ||
return this; | ||
} else { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return this.write({ style: arguments[0], args: args }); | ||
} | ||
@@ -108,3 +121,3 @@ }; | ||
if (format === 'auto') { | ||
format = MagicPen.defaultFormat(); | ||
format = MagicPen.defaultFormat; | ||
} | ||
@@ -111,0 +124,0 @@ var serializer = new MagicPen.serializers[format](); |
@@ -37,2 +37,21 @@ var utils = { | ||
return size; | ||
}, | ||
arrayEquals: function (a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (!a || a.length !== b.length) { | ||
return false; | ||
} | ||
for (var i = 0; i < a.length; i += 1) { | ||
if (a[i] !== b[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -39,0 +58,0 @@ }; |
@@ -114,4 +114,3 @@ /*! | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = stylesString.split(/\s*,\s*/); | ||
var styles = Array.prototype.slice.call(arguments, 1); | ||
styles.forEach(function (styleName) { | ||
@@ -224,5 +223,5 @@ if (ansiStyles[styleName]) { | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = Array.prototype.slice.call(arguments, 1); | ||
var styleProperties = []; | ||
stylesString.split(/\s*,\s*/).forEach(function (styleName) { | ||
styles.forEach(function (styleName) { | ||
if (rgbRegexp.test(styleName)) { | ||
@@ -252,6 +251,2 @@ if (styleName.substring(0, 2) === 'bg') { | ||
function isOutputEntry(obj) { | ||
return typeof obj === 'object' && 'style' in obj && 'args' in obj; | ||
} | ||
function MagicPen(options) { | ||
@@ -304,4 +299,16 @@ if (!(this instanceof MagicPen)) { | ||
function hasSameTextStyling(a, b) { | ||
if (!a || !b || a.style !== 'text' || b.style !== 'text') { | ||
return false; | ||
} | ||
return utils.arrayEquals(Array.prototype.slice.call(a.args, 1), | ||
Array.prototype.slice.call(b.args, 1)); | ||
} | ||
MagicPen.prototype.write = function () { | ||
if (arguments.length === 1 && isOutputEntry(arguments[0])) { | ||
if (typeof arguments[0] === 'string') { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return this.write({ style: arguments[0], args: args }); | ||
} else { | ||
var options = arguments[0]; | ||
@@ -313,7 +320,12 @@ if (this.styles[options.style]) { | ||
this.output[0] = this.output[0] || []; | ||
this.output[this.output.length - 1].push(options); | ||
var lastLine = this.output[this.output.length - 1]; | ||
var lastEntry = lastLine[lastLine.length - 1]; | ||
if (hasSameTextStyling(lastEntry, options)) { | ||
options.args[0] = lastEntry.args[0] + options.args[0]; | ||
lastLine[lastLine.length - 1] = options; | ||
} else { | ||
lastLine.push(options); | ||
} | ||
return this; | ||
} else { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return this.write({ style: arguments[0], args: args }); | ||
} | ||
@@ -354,3 +366,3 @@ }; | ||
if (format === 'auto') { | ||
format = MagicPen.defaultFormat(); | ||
format = MagicPen.defaultFormat; | ||
} | ||
@@ -612,2 +624,21 @@ var serializer = new MagicPen.serializers[format](); | ||
return size; | ||
}, | ||
arrayEquals: function (a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (!a || a.length !== b.length) { | ||
return false; | ||
} | ||
for (var i = 0; i < a.length; i += 1) { | ||
if (a[i] !== b[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -614,0 +645,0 @@ }; |
{ | ||
"name": "magicpen", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -5,0 +5,0 @@ "main": "./lib/MagicPen.js", |
@@ -143,6 +143,6 @@ # MagicPen | ||
.text(' ') | ||
.text('colorful', 'yellow, bold') | ||
.text('colorful', 'yellow', 'bold') | ||
.text(' ') | ||
.text('world', 'green', 'underline') | ||
.text('!', 'bgYellow, blue'); | ||
.text('!', 'bgYellow', 'blue'); | ||
console.log(pen.toString('ansi')); | ||
@@ -161,5 +161,5 @@ ``` | ||
expect(pen.toString('html'), 'to equal', | ||
'<code>\n' + | ||
'<div style="font-family: monospace; white-space: nowrap">\n' + | ||
' <div><strong>Hello world!</strong></div>\n' + | ||
'</code>'); | ||
'</div>'); | ||
``` | ||
@@ -166,0 +166,0 @@ |
@@ -200,3 +200,3 @@ /*global describe, it, beforeEach*/ | ||
it('styles an be called as methods', function () { | ||
pen.red('Hello').sp().green('world').text('!', 'red, bold'); | ||
pen.red('Hello').sp().green('world').text('!', 'red', 'bold'); | ||
expect(pen.toString(), 'to equal', 'Hello world!'); | ||
@@ -297,2 +297,8 @@ }); | ||
it('merges adjacent text entries with the same styling', function () { | ||
pen.red('Hello').red(' ').red('world'); | ||
expect(pen.toString('ansi'), 'to equal', | ||
'\x1B[31mHello world\x1B[39m'); | ||
}); | ||
it('handles custom styles', function () { | ||
@@ -321,3 +327,3 @@ pen.addStyle('error', function (text) { | ||
it('styles an be called as methods', function () { | ||
pen.red('Hello').sp().green('world').text('!', 'red, bold'); | ||
pen.red('Hello').sp().green('world').text('!', 'red', 'bold'); | ||
expect(pen.toString('ansi'), 'to equal', | ||
@@ -344,3 +350,3 @@ '\x1B[31mHello\x1B[39m' + | ||
expect(pen.toString('ansi'), 'to equal', | ||
'\x1b[40m\x1b[48;5;236mHello world\x1b[49m' ); | ||
'\x1b[40m\x1b[48;5;236mHello world\x1b[49m'); | ||
}); | ||
@@ -370,3 +376,3 @@ | ||
it('styles an be called as methods', function () { | ||
pen.red('Hello').sp().green('world').text('!', 'red, bold'); | ||
pen.red('Hello').sp().green('world').text('!', 'red', 'bold'); | ||
expect(pen.toString('html'), 'to equal', | ||
@@ -421,2 +427,10 @@ '<div style="font-family: monospace; white-space: nowrap">\n' + | ||
it('merges adjacent text entries with the same styling', function () { | ||
pen.red('Hello').red(' ').red('world'); | ||
expect(pen.toString('html'), 'to equal', | ||
'<div style="font-family: monospace; white-space: nowrap">\n' + | ||
' <div><span style="color: red">Hello world</span></div>\n' + | ||
'</div>'); | ||
}); | ||
it('handles custom styles', function () { | ||
@@ -693,6 +707,6 @@ pen.addStyle('error', function (text) { | ||
pen.addStyle('keyword', function (text) { | ||
this.text(text, 'blue, bold'); | ||
this.text(text, 'blue', 'bold'); | ||
}); | ||
pen.addStyle('functionName', function (text) { | ||
this.text(text, 'white, bold'); | ||
this.text(text, 'white', 'bold'); | ||
}); | ||
@@ -717,6 +731,6 @@ pen.addStyle('number', function (text) { | ||
pen.addStyle('keyword', function (text) { | ||
this.text(text, 'black, bold'); | ||
this.text(text, 'black', 'bold'); | ||
}); | ||
pen.addStyle('functionName', function (text) { | ||
this.text(text, 'red, bold'); | ||
this.text(text, 'red', 'bold'); | ||
}); | ||
@@ -723,0 +737,0 @@ pen.addStyle('number', function (text) { |
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
446052
2185