Comparing version 3.0.10 to 3.1.0
@@ -1,34 +0,3 @@ | ||
var htmlStyles = { | ||
bold: 'font-weight: bold', | ||
dim: 'opacity: 0.7', | ||
italic: 'font-style: italic', | ||
underline: 'text-decoration: underline', | ||
inverse: '-webkit-filter: invert(%100); filter: invert(100%)', | ||
hidden: 'visibility: hidden', | ||
strikeThrough: 'text-decoration: line-through', | ||
var cssStyles = require('./cssStyles'); | ||
black: 'color: black', | ||
red: 'color: red', | ||
green: 'color: green', | ||
yellow: 'color: yellow', | ||
blue: 'color: blue', | ||
magenta: 'color: magenta', | ||
cyan: 'color: cyan', | ||
white: 'color: white', | ||
gray: 'color: gray', | ||
bgBlack: 'background-color: black', | ||
bgRed: 'background-color: red', | ||
bgGreen: 'background-color: green', | ||
bgYellow: 'background-color: yellow', | ||
bgBlue: 'background-color: blue', | ||
bgMagenta: 'background-color: magenta', | ||
bgCyan: 'background-color: cyan', | ||
bgWhite: 'background-color: white' | ||
}; | ||
Object.keys(htmlStyles).forEach(function (styleName) { | ||
htmlStyles[styleName.toLowerCase()] = htmlStyles[styleName]; | ||
}); | ||
function HtmlSerializer() {} | ||
@@ -79,4 +48,4 @@ | ||
} | ||
} else if (htmlStyles[styleName]) { | ||
styleProperties.push(htmlStyles[styleName]); | ||
} else if (cssStyles[styleName]) { | ||
styleProperties.push(cssStyles[styleName]); | ||
} | ||
@@ -83,0 +52,0 @@ |
@@ -52,3 +52,4 @@ /*global window*/ | ||
html: require('./HtmlSerializer'), | ||
ansi: require('./AnsiSerializer') | ||
ansi: require('./AnsiSerializer'), | ||
coloredConsole: require('./ColoredConsoleSerializer') | ||
}; | ||
@@ -55,0 +56,0 @@ |
@@ -1,75 +0,4 @@ | ||
var duplicateText = require('./duplicateText'); | ||
var flattenBlocksInLines = require('./flattenBlocksInLines'); | ||
var utils = require('./utils'); | ||
function createPadding(length) { | ||
return { style: 'text', args: [duplicateText(' ', length)] }; | ||
} | ||
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 []; | ||
} | ||
} | ||
function flattenBlocksInLine(line) { | ||
if (line.length === 0) { | ||
return [[]]; | ||
} | ||
if (!utils.lineContainsBlocks(line)) { | ||
return [line]; | ||
} | ||
var result = []; | ||
var startIndex = 0; | ||
line.forEach(function (outputEntry, blockIndex) { | ||
var blockLines = flattenBlocksInOutputEntry(outputEntry); | ||
var blockLinesLengths = blockLines.map(function (line) { | ||
return utils.calculateLineSize(line).width; | ||
}); | ||
var longestLineLength = Math.max.apply(null, blockLinesLengths); | ||
var padLines = blockIndex < line.length - 1; | ||
blockLines.forEach(function (blockLine, index) { | ||
if (!result[index]) { | ||
result[index] = [createPadding(startIndex)]; | ||
} | ||
Array.prototype.push.apply(result[index], blockLine); | ||
var paddingLength = longestLineLength - blockLinesLengths[index]; | ||
if (padLines && paddingLength > 0) { | ||
result[index].push(createPadding(paddingLength)); | ||
} | ||
}); | ||
if (padLines) { | ||
for (var i = blockLines.length; i < result.length; i += 1) { | ||
result[i].push(createPadding(longestLineLength)); | ||
} | ||
} | ||
startIndex += longestLineLength; | ||
}, this); | ||
return result; | ||
} | ||
function flattenBlocksInLines(lines) { | ||
var result = []; | ||
lines.forEach(function (line) { | ||
flattenBlocksInLine(line).forEach(function (line) { | ||
result.push(line); | ||
}); | ||
}); | ||
return result; | ||
} | ||
function TextSerializer() {} | ||
@@ -76,0 +5,0 @@ |
@@ -42,9 +42,2 @@ var utils = { | ||
lineContainsBlocks: function (line) { | ||
return line.some(function (outputEntry) { | ||
return outputEntry.style === 'block' || | ||
(outputEntry.style === 'text' && String(outputEntry.args[0]).indexOf('\n') !== -1); | ||
}); | ||
}, | ||
arrayEquals: function (a, b) { | ||
@@ -51,0 +44,0 @@ if (a === b) { |
365
magicpen.js
@@ -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(6); | ||
var TextSerializer = require(4); | ||
var colorDiff = require(11); | ||
var utils = require(9); | ||
var TextSerializer = require(5); | ||
var colorDiff = require(14); | ||
var ansiStyles = utils.extend({}, require(7)); | ||
var ansiStyles = utils.extend({}, require(10)); | ||
Object.keys(ansiStyles).forEach(function (styleName) { | ||
@@ -156,35 +156,78 @@ ansiStyles[styleName.toLowerCase()] = ansiStyles[styleName]; | ||
},{}],2:[function(require,module,exports){ | ||
var htmlStyles = { | ||
bold: 'font-weight: bold', | ||
dim: 'opacity: 0.7', | ||
italic: 'font-style: italic', | ||
underline: 'text-decoration: underline', | ||
inverse: '-webkit-filter: invert(%100); filter: invert(100%)', | ||
hidden: 'visibility: hidden', | ||
strikeThrough: 'text-decoration: line-through', | ||
var cssStyles = require(6); | ||
var flattenBlocksInLines = require(8); | ||
black: 'color: black', | ||
red: 'color: red', | ||
green: 'color: green', | ||
yellow: 'color: yellow', | ||
blue: 'color: blue', | ||
magenta: 'color: magenta', | ||
cyan: 'color: cyan', | ||
white: 'color: white', | ||
gray: 'color: gray', | ||
function ColoredConsoleSerializer() {} | ||
bgBlack: 'background-color: black', | ||
bgRed: 'background-color: red', | ||
bgGreen: 'background-color: green', | ||
bgYellow: 'background-color: yellow', | ||
bgBlue: 'background-color: blue', | ||
bgMagenta: 'background-color: magenta', | ||
bgCyan: 'background-color: cyan', | ||
bgWhite: 'background-color: white' | ||
ColoredConsoleSerializer.prototype.serialize = function (lines) { | ||
var formatString = ''; | ||
var styleStrings = []; | ||
this.serializeLines(flattenBlocksInLines(lines)).forEach(function (entry) { | ||
if (entry) { | ||
formatString += entry[0]; | ||
if (entry.length > 1) { | ||
styleStrings.push(entry[1]); | ||
} | ||
} | ||
}); | ||
return [formatString].concat(styleStrings); | ||
}; | ||
Object.keys(htmlStyles).forEach(function (styleName) { | ||
htmlStyles[styleName.toLowerCase()] = htmlStyles[styleName]; | ||
}); | ||
ColoredConsoleSerializer.prototype.serializeLines = function (lines) { | ||
var result = []; | ||
lines.forEach(function (line, i) { | ||
if (i > 0) { | ||
result.push(['\n ']); | ||
} | ||
Array.prototype.push.apply(result, this.serializeLine(line)); | ||
}, this); | ||
return result; | ||
}; | ||
ColoredConsoleSerializer.prototype.serializeLine = function (line) { | ||
var result = []; | ||
line.forEach(function (outputEntry) { | ||
if (this[outputEntry.style]) { | ||
result.push(this[outputEntry.style].apply(this, outputEntry.args)); | ||
} | ||
}, this); | ||
return result; | ||
}; | ||
ColoredConsoleSerializer.prototype.block = function (content) { | ||
return this.serializeLines(content); | ||
}; | ||
var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i; | ||
ColoredConsoleSerializer.prototype.text = function () { | ||
var content = String(arguments[0]); | ||
if (content === '') { | ||
return null; | ||
} | ||
var result = ['%c' + content]; | ||
var styleProperties = []; | ||
if (arguments.length > 1) { | ||
for (var i = 1; i < arguments.length; i += 1) { | ||
var styleName = arguments[i]; | ||
if (rgbRegexp.test(styleName)) { | ||
if (styleName.substring(0, 2) === 'bg') { | ||
styleProperties.push('background-color: ' + styleName.substring(2)); | ||
} else { | ||
styleProperties.push('color: ' + styleName); | ||
} | ||
} else if (cssStyles[styleName]) { | ||
styleProperties.push(cssStyles[styleName]); | ||
} | ||
} | ||
} | ||
result.push(styleProperties.join('; ')); | ||
return result; | ||
}; | ||
module.exports = ColoredConsoleSerializer; | ||
},{}],3:[function(require,module,exports){ | ||
var cssStyles = require(6); | ||
function HtmlSerializer() {} | ||
@@ -235,4 +278,4 @@ | ||
} | ||
} else if (htmlStyles[styleName]) { | ||
styleProperties.push(htmlStyles[styleName]); | ||
} else if (cssStyles[styleName]) { | ||
styleProperties.push(cssStyles[styleName]); | ||
} | ||
@@ -248,7 +291,7 @@ | ||
},{}],3:[function(require,module,exports){ | ||
},{}],4:[function(require,module,exports){ | ||
/*global window*/ | ||
var utils = require(6); | ||
var utils = require(9); | ||
var extend = utils.extend; | ||
var duplicateText = require(5); | ||
var duplicateText = require(7); | ||
@@ -277,3 +320,3 @@ function MagicPen(options) { | ||
} | ||
} else if (require(13)) { | ||
} else if (require(16)) { | ||
MagicPen.defaultFormat = 'ansi'; // colored console | ||
@@ -300,5 +343,6 @@ } else { | ||
MagicPen.serializers = { | ||
text: require(4), | ||
html: require(2), | ||
ansi: require(1) | ||
text: require(5), | ||
html: require(3), | ||
ansi: require(1), | ||
coloredConsole: require(2) | ||
}; | ||
@@ -645,6 +689,107 @@ | ||
},{}],4:[function(require,module,exports){ | ||
var duplicateText = require(5); | ||
var utils = require(6); | ||
},{}],5:[function(require,module,exports){ | ||
var flattenBlocksInLines = require(8); | ||
var utils = require(9); | ||
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] ? | ||
String(this[outputEntry.style].apply(this, outputEntry.args)) : | ||
''; | ||
}, this).join(''); | ||
}; | ||
TextSerializer.prototype.text = function (content) { | ||
return content; | ||
}; | ||
TextSerializer.prototype.block = function (content) { | ||
return this.serialize(content); | ||
}; | ||
module.exports = TextSerializer; | ||
},{}],6:[function(require,module,exports){ | ||
var cssStyles = { | ||
bold: 'font-weight: bold', | ||
dim: 'opacity: 0.7', | ||
italic: 'font-style: italic', | ||
underline: 'text-decoration: underline', | ||
inverse: '-webkit-filter: invert(%100); filter: invert(100%)', | ||
hidden: 'visibility: hidden', | ||
strikeThrough: 'text-decoration: line-through', | ||
black: 'color: black', | ||
red: 'color: red', | ||
green: 'color: green', | ||
yellow: 'color: yellow', | ||
blue: 'color: blue', | ||
magenta: 'color: magenta', | ||
cyan: 'color: cyan', | ||
white: 'color: white', | ||
gray: 'color: gray', | ||
bgBlack: 'background-color: black', | ||
bgRed: 'background-color: red', | ||
bgGreen: 'background-color: green', | ||
bgYellow: 'background-color: yellow', | ||
bgBlue: 'background-color: blue', | ||
bgMagenta: 'background-color: magenta', | ||
bgCyan: 'background-color: cyan', | ||
bgWhite: 'background-color: white' | ||
}; | ||
Object.keys(cssStyles).forEach(function (styleName) { | ||
cssStyles[styleName.toLowerCase()] = cssStyles[styleName]; | ||
}); | ||
module.exports = cssStyles; | ||
},{}],7:[function(require,module,exports){ | ||
var whitespaceCacheLength = 256; | ||
var whitespaceCache = ['']; | ||
for (var i = 1; i <= whitespaceCacheLength; i += 1) { | ||
whitespaceCache[i] = whitespaceCache[i - 1] + ' '; | ||
} | ||
function duplicateText(content, times) { | ||
if (times < 0) { | ||
return ''; | ||
} | ||
var result = ''; | ||
if (content === ' ') { | ||
if (times <= whitespaceCacheLength) { | ||
return whitespaceCache[times]; | ||
} | ||
var segment = whitespaceCache[whitespaceCacheLength]; | ||
var numberOfSegments = Math.floor(times / whitespaceCacheLength); | ||
for (var i = 0; i < numberOfSegments; i += 1) { | ||
result += segment; | ||
} | ||
result += whitespaceCache[times % whitespaceCacheLength]; | ||
} else { | ||
for (var j = 0; j < times; j += 1) { | ||
result += content; | ||
} | ||
} | ||
return result; | ||
} | ||
module.exports = duplicateText; | ||
},{}],8:[function(require,module,exports){ | ||
var utils = require(9); | ||
var duplicateText = require(7); | ||
function createPadding(length) { | ||
@@ -654,2 +799,9 @@ return { style: 'text', args: [duplicateText(' ', length)] }; | ||
function lineContainsBlocks(line) { | ||
return line.some(function (outputEntry) { | ||
return outputEntry.style === 'block' || | ||
(outputEntry.style === 'text' && String(outputEntry.args[0]).indexOf('\n') !== -1); | ||
}); | ||
} | ||
function flattenBlocksInOutputEntry(outputEntry) { | ||
@@ -671,3 +823,3 @@ switch (outputEntry.style) { | ||
if (!utils.lineContainsBlocks(line)) { | ||
if (!lineContainsBlocks(line)) { | ||
return [line]; | ||
@@ -723,64 +875,5 @@ } | ||
function TextSerializer() {} | ||
module.exports = flattenBlocksInLines; | ||
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] ? | ||
String(this[outputEntry.style].apply(this, outputEntry.args)) : | ||
''; | ||
}, this).join(''); | ||
}; | ||
TextSerializer.prototype.text = function (content) { | ||
return content; | ||
}; | ||
TextSerializer.prototype.block = function (content) { | ||
return this.serialize(content); | ||
}; | ||
module.exports = TextSerializer; | ||
},{}],5:[function(require,module,exports){ | ||
var whitespaceCacheLength = 256; | ||
var whitespaceCache = ['']; | ||
for (var i = 1; i <= whitespaceCacheLength; i += 1) { | ||
whitespaceCache[i] = whitespaceCache[i - 1] + ' '; | ||
} | ||
function duplicateText(content, times) { | ||
if (times < 0) { | ||
return ''; | ||
} | ||
var result = ''; | ||
if (content === ' ') { | ||
if (times <= whitespaceCacheLength) { | ||
return whitespaceCache[times]; | ||
} | ||
var segment = whitespaceCache[whitespaceCacheLength]; | ||
var numberOfSegments = Math.floor(times / whitespaceCacheLength); | ||
for (var i = 0; i < numberOfSegments; i += 1) { | ||
result += segment; | ||
} | ||
result += whitespaceCache[times % whitespaceCacheLength]; | ||
} else { | ||
for (var j = 0; j < times; j += 1) { | ||
result += content; | ||
} | ||
} | ||
return result; | ||
} | ||
module.exports = duplicateText; | ||
},{}],6:[function(require,module,exports){ | ||
},{}],9:[function(require,module,exports){ | ||
var utils = { | ||
@@ -827,9 +920,2 @@ extend: function (target) { | ||
lineContainsBlocks: function (line) { | ||
return line.some(function (outputEntry) { | ||
return outputEntry.style === 'block' || | ||
(outputEntry.style === 'text' && String(outputEntry.args[0]).indexOf('\n') !== -1); | ||
}); | ||
}, | ||
arrayEquals: function (a, b) { | ||
@@ -861,3 +947,3 @@ if (a === b) { | ||
},{}],7:[function(require,module,exports){ | ||
},{}],10:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -920,3 +1006,3 @@ | ||
},{}],8:[function(require,module,exports){ | ||
},{}],11:[function(require,module,exports){ | ||
// shim for using process in browser | ||
@@ -986,3 +1072,3 @@ | ||
},{}],9:[function(require,module,exports){ | ||
},{}],12:[function(require,module,exports){ | ||
/** | ||
@@ -1102,3 +1188,3 @@ * @author Markus Näsman | ||
},{}],10:[function(require,module,exports){ | ||
},{}],13:[function(require,module,exports){ | ||
/** | ||
@@ -1269,8 +1355,8 @@ * @author Markus Näsman | ||
},{}],11:[function(require,module,exports){ | ||
},{}],14:[function(require,module,exports){ | ||
'use strict'; | ||
var diff = require(10); | ||
var convert = require(9); | ||
var palette = require(12); | ||
var diff = require(13); | ||
var convert = require(12); | ||
var palette = require(15); | ||
@@ -1287,16 +1373,7 @@ var color = module.exports = {}; | ||
var result = color.map_palette([target], relative, 'closest'); | ||
var result = color.map_palette([target], relative); | ||
return result[key]; | ||
}; | ||
color.furthest = function(target, relative) { | ||
var key = color.palette_map_key(target); | ||
var result = color.map_palette([target], relative, 'furthest'); | ||
return result[key]; | ||
}; | ||
},{}],12:[function(require,module,exports){ | ||
},{}],15:[function(require,module,exports){ | ||
/** | ||
@@ -1339,4 +1416,4 @@ * @author Markus Näsman | ||
*/ | ||
var color_diff = require(10); | ||
var color_convert = require(9); | ||
var color_diff = require(13); | ||
var color_convert = require(12); | ||
@@ -1361,9 +1438,7 @@ /** | ||
* @param [{rgbcolor}] b each element should have fields R,G,B | ||
* @param 'type' should have field closest or furthest | ||
* @return {palettemap} | ||
*/ | ||
function map_palette(a, b, type) | ||
function map_palette(a, b) | ||
{ | ||
var c = {}; | ||
type = type || 'closest'; | ||
for (var idx1 in a){ | ||
@@ -1377,15 +1452,7 @@ var color1 = a[idx1]; | ||
var current_color_diff = diff(color1,color2); | ||
if((best_color == undefined) || ((type === 'closest') && (current_color_diff < best_color_diff))) | ||
if((best_color == undefined) || (current_color_diff < best_color_diff)) | ||
{ | ||
best_color = color2; | ||
best_color_diff = current_color_diff; | ||
continue; | ||
} | ||
if((type === 'furthest') && (current_color_diff > best_color_diff)) | ||
{ | ||
best_color = color2; | ||
best_color_diff = current_color_diff; | ||
continue; | ||
} | ||
} | ||
@@ -1413,3 +1480,3 @@ c[palette_map_key(color1)] = best_color; | ||
},{}],13:[function(require,module,exports){ | ||
},{}],16:[function(require,module,exports){ | ||
(function (process){ | ||
@@ -1456,4 +1523,4 @@ 'use strict'; | ||
}).call(this,require(8)) | ||
},{}]},{},[3])(3) | ||
}).call(this,require(11)) | ||
},{}]},{},[4])(4) | ||
}); |
{ | ||
"name": "magicpen", | ||
"version": "3.0.10", | ||
"version": "3.1.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -5,0 +5,0 @@ "main": "./lib/MagicPen.js", |
@@ -9,3 +9,3 @@ /*global describe, it, beforeEach*/ | ||
identify: function (obj) { | ||
return obj.isMagicPen; | ||
return obj && obj.isMagicPen; | ||
}, | ||
@@ -949,2 +949,26 @@ inspect: function (pen, depth, output) { | ||
}); | ||
describe('ColoredConsoleSerializer', function () { | ||
it('should output an array', function () { | ||
var pen = magicpen(); | ||
pen.red('Hello, world!'); | ||
expect(pen.toString('coloredConsole'), 'to equal', [ | ||
'%cHello, world!', | ||
'color: red' | ||
]); | ||
}); | ||
it('should support blocks', function () { | ||
var pen = magicpen(); | ||
pen.red('Hello').block(function () { | ||
this.gray(' // ').text('This is a').nl() | ||
.indentLines() | ||
.gray(' // ').indent().text('multiline comment'); | ||
}); | ||
expect(pen.toString('coloredConsole'), 'to equal', [ | ||
'%cHello%c // %cThis is a\n %c %c // %c multiline comment', | ||
'color: red', 'color: gray', '', '', 'color: gray', '' | ||
]); | ||
}); | ||
}); | ||
}); |
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
474346
29
2901