Comparing version 2.1.1 to 3.0.0
@@ -18,3 +18,3 @@ /*global window*/ | ||
this.indentationLevel = 0; | ||
this.output = []; | ||
this.output = [[]]; | ||
this.styles = {}; | ||
@@ -40,4 +40,2 @@ } | ||
this.output[0] = this.output[0] || []; | ||
for (var i = 0; i < count; i += 1) { | ||
@@ -72,9 +70,14 @@ this.output.push([]); | ||
var lastEntry = result[result.length - 1]; | ||
if (hasSameTextStyling(lastEntry, line[i])) { | ||
var entry = line[i]; | ||
if (entry.style === 'text' && entry.args[0] === '') { | ||
continue; | ||
} | ||
if (hasSameTextStyling(lastEntry, entry)) { | ||
result[result.length - 1] = { | ||
style: line[i].style, | ||
args: [lastEntry.args[0] + line[i].args[0]].concat(line[i].args.slice(1)) | ||
style: entry.style, | ||
args: [lastEntry.args[0] + entry.args[0]].concat(entry.args.slice(1)) | ||
}; | ||
} else { | ||
result.push(line[i]); | ||
result.push(entry); | ||
} | ||
@@ -96,3 +99,2 @@ } | ||
} | ||
this.output[0] = this.output[0] || []; | ||
var lastLine = this.output[this.output.length - 1]; | ||
@@ -150,2 +152,6 @@ var lastEntry = lastLine[lastLine.length - 1]; | ||
MagicPen.prototype.text = function (content) { | ||
if (content === '') { | ||
return this; | ||
} | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
@@ -213,7 +219,6 @@ content = String(content); | ||
if (pen.output.length === 0) { | ||
if (pen.isEmpty()) { | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
var lastLine = this.output[this.output.length - 1]; | ||
@@ -229,9 +234,5 @@ Array.prototype.push.apply(lastLine, pen.output[0]); | ||
MagicPen.prototype.prependLinesWith = function () { | ||
if (this.output.length === 0) { | ||
return this; | ||
} | ||
var pen = this.getContentFromArguments(arguments); | ||
if (pen.output.length === 0) { | ||
if (pen.isEmpty()) { | ||
return this; | ||
@@ -284,3 +285,3 @@ } | ||
clonedPen.indentationLevel = 0; | ||
clonedPen.output = []; | ||
clonedPen.output = [[]]; | ||
return clonedPen; | ||
@@ -304,3 +305,84 @@ }; | ||
function replaceText(output, outputArray, regexp, cb) { | ||
var replacedOutput = output; | ||
outputArray.forEach(function (line, i) { | ||
if (0 < i) { | ||
replacedOutput.nl(); | ||
} | ||
line.forEach(function (outputEntry, j) { | ||
if (outputEntry.style === 'block') { | ||
return replacedOutput.output[replacedOutput.output.length - 1].push({ | ||
style: 'block', | ||
args: [replaceText(output.clone(), outputEntry.args[0], regexp, cb)] | ||
}); | ||
} else if (outputEntry.style !== 'text') { | ||
return replacedOutput.output[replacedOutput.output.length - 1].push(outputEntry); | ||
} | ||
if (regexp.global) { | ||
regexp.lastIndex = 0; | ||
} | ||
var styles = outputEntry.args.slice(1); | ||
var m; | ||
var first = true; | ||
var lastIndex = 0; | ||
var text = outputEntry.args[0]; | ||
while ((m = regexp.exec(text)) !== null && (regexp.global || first)) { | ||
if (lastIndex < m.index) { | ||
replacedOutput.text.apply(replacedOutput, [text.substring(lastIndex, m.index)].concat(styles)); | ||
} | ||
cb.apply(replacedOutput, [styles].concat(m)); | ||
first = false; | ||
lastIndex = m.index + m[0].length; | ||
} | ||
if (lastIndex === 0) { | ||
var lastLine; | ||
if (replacedOutput.output.length === 0) { | ||
lastLine = replacedOutput.output[0] = []; | ||
} else { | ||
lastLine = replacedOutput.output[replacedOutput.output.length - 1]; | ||
} | ||
lastLine.push(outputEntry); | ||
} else if (lastIndex < text.length) { | ||
replacedOutput.text.apply(replacedOutput, [text.substring(lastIndex, text.length)].concat(styles)); | ||
} | ||
}, this); | ||
}, this); | ||
return replacedOutput.output.map(normalizeLine); | ||
} | ||
MagicPen.prototype.isEmpty = function () { | ||
return this.output.length === 1 && this.output[0].length === 0; | ||
}; | ||
MagicPen.prototype.replaceText = function (regexp, cb) { | ||
if (this.isEmpty()) { | ||
return this; | ||
} | ||
if (typeof cb === 'string') { | ||
var text = cb; | ||
cb = function (styles, _) { | ||
var args = [text].concat(styles); | ||
this.text.apply(this, args); | ||
}; | ||
} | ||
if (arguments.length === 1) { | ||
cb = regexp; | ||
regexp = /.*/; | ||
} | ||
this.output = replaceText(this.clone(), this.output, regexp, cb); | ||
return this; | ||
}; | ||
module.exports = MagicPen; |
125
magicpen.js
@@ -261,3 +261,3 @@ /*! | ||
this.indentationLevel = 0; | ||
this.output = []; | ||
this.output = [[]]; | ||
this.styles = {}; | ||
@@ -283,4 +283,2 @@ } | ||
this.output[0] = this.output[0] || []; | ||
for (var i = 0; i < count; i += 1) { | ||
@@ -315,9 +313,14 @@ this.output.push([]); | ||
var lastEntry = result[result.length - 1]; | ||
if (hasSameTextStyling(lastEntry, line[i])) { | ||
var entry = line[i]; | ||
if (entry.style === 'text' && entry.args[0] === '') { | ||
continue; | ||
} | ||
if (hasSameTextStyling(lastEntry, entry)) { | ||
result[result.length - 1] = { | ||
style: line[i].style, | ||
args: [lastEntry.args[0] + line[i].args[0]].concat(line[i].args.slice(1)) | ||
style: entry.style, | ||
args: [lastEntry.args[0] + entry.args[0]].concat(entry.args.slice(1)) | ||
}; | ||
} else { | ||
result.push(line[i]); | ||
result.push(entry); | ||
} | ||
@@ -339,3 +342,2 @@ } | ||
} | ||
this.output[0] = this.output[0] || []; | ||
var lastLine = this.output[this.output.length - 1]; | ||
@@ -393,2 +395,6 @@ var lastEntry = lastLine[lastLine.length - 1]; | ||
MagicPen.prototype.text = function (content) { | ||
if (content === '') { | ||
return this; | ||
} | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
@@ -456,7 +462,6 @@ content = String(content); | ||
if (pen.output.length === 0) { | ||
if (pen.isEmpty()) { | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
var lastLine = this.output[this.output.length - 1]; | ||
@@ -472,9 +477,5 @@ Array.prototype.push.apply(lastLine, pen.output[0]); | ||
MagicPen.prototype.prependLinesWith = function () { | ||
if (this.output.length === 0) { | ||
return this; | ||
} | ||
var pen = this.getContentFromArguments(arguments); | ||
if (pen.output.length === 0) { | ||
if (pen.isEmpty()) { | ||
return this; | ||
@@ -527,3 +528,3 @@ } | ||
clonedPen.indentationLevel = 0; | ||
clonedPen.output = []; | ||
clonedPen.output = [[]]; | ||
return clonedPen; | ||
@@ -547,3 +548,84 @@ }; | ||
function replaceText(output, outputArray, regexp, cb) { | ||
var replacedOutput = output; | ||
outputArray.forEach(function (line, i) { | ||
if (0 < i) { | ||
replacedOutput.nl(); | ||
} | ||
line.forEach(function (outputEntry, j) { | ||
if (outputEntry.style === 'block') { | ||
return replacedOutput.output[replacedOutput.output.length - 1].push({ | ||
style: 'block', | ||
args: [replaceText(output.clone(), outputEntry.args[0], regexp, cb)] | ||
}); | ||
} else if (outputEntry.style !== 'text') { | ||
return replacedOutput.output[replacedOutput.output.length - 1].push(outputEntry); | ||
} | ||
if (regexp.global) { | ||
regexp.lastIndex = 0; | ||
} | ||
var styles = outputEntry.args.slice(1); | ||
var m; | ||
var first = true; | ||
var lastIndex = 0; | ||
var text = outputEntry.args[0]; | ||
while ((m = regexp.exec(text)) !== null && (regexp.global || first)) { | ||
if (lastIndex < m.index) { | ||
replacedOutput.text.apply(replacedOutput, [text.substring(lastIndex, m.index)].concat(styles)); | ||
} | ||
cb.apply(replacedOutput, [styles].concat(m)); | ||
first = false; | ||
lastIndex = m.index + m[0].length; | ||
} | ||
if (lastIndex === 0) { | ||
var lastLine; | ||
if (replacedOutput.output.length === 0) { | ||
lastLine = replacedOutput.output[0] = []; | ||
} else { | ||
lastLine = replacedOutput.output[replacedOutput.output.length - 1]; | ||
} | ||
lastLine.push(outputEntry); | ||
} else if (lastIndex < text.length) { | ||
replacedOutput.text.apply(replacedOutput, [text.substring(lastIndex, text.length)].concat(styles)); | ||
} | ||
}, this); | ||
}, this); | ||
return replacedOutput.output.map(normalizeLine); | ||
} | ||
MagicPen.prototype.isEmpty = function () { | ||
return this.output.length === 1 && this.output[0].length === 0; | ||
}; | ||
MagicPen.prototype.replaceText = function (regexp, cb) { | ||
if (this.isEmpty()) { | ||
return this; | ||
} | ||
if (typeof cb === 'string') { | ||
var text = cb; | ||
cb = function (styles, _) { | ||
var args = [text].concat(styles); | ||
this.text.apply(this, args); | ||
}; | ||
} | ||
if (arguments.length === 1) { | ||
cb = regexp; | ||
regexp = /.*/; | ||
} | ||
this.output = replaceText(this.clone(), this.output, regexp, cb); | ||
return this; | ||
}; | ||
module.exports = MagicPen; | ||
@@ -1210,15 +1292,8 @@ | ||
'use strict'; | ||
var argv = process.argv; | ||
module.exports = (function () { | ||
if (argv.indexOf('--no-color') !== -1 || | ||
argv.indexOf('--no-colors') !== -1 || | ||
argv.indexOf('--color=false') !== -1) { | ||
if (process.argv.indexOf('--no-color') !== -1) { | ||
return false; | ||
} | ||
if (argv.indexOf('--color') !== -1 || | ||
argv.indexOf('--colors') !== -1 || | ||
argv.indexOf('--color=true') !== -1 || | ||
argv.indexOf('--color=always') !== -1) { | ||
if (process.argv.indexOf('--color') !== -1) { | ||
return true; | ||
@@ -1225,0 +1300,0 @@ } |
{ | ||
"name": "magicpen", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -30,3 +30,3 @@ "main": "./lib/MagicPen.js", | ||
"sinon": "=1.9.1", | ||
"unexpected": "^3.2.2", | ||
"unexpected": "^5.0.0-beta5", | ||
"unexpected-sinon": "^3.0.2" | ||
@@ -33,0 +33,0 @@ }, |
@@ -6,2 +6,16 @@ /*global describe, it, beforeEach*/ | ||
expect.installPlugin(require('unexpected-sinon')); | ||
expect.addType({ | ||
name: 'magicpen', | ||
identify: function (obj) { | ||
return obj.isMagicPen; | ||
}, | ||
inspect: function (pen, depth, output) { | ||
return output.append(pen); | ||
}, | ||
equal: function (a, b) { | ||
return a.toString() === b.toString() && | ||
a.toString('ansi') === b.toString('ansi') && | ||
a.toString('html') === b.toString('html'); | ||
} | ||
}); | ||
@@ -143,2 +157,93 @@ describe('magicpen', function () { | ||
describe('replaceText', function () { | ||
describe('with only a callback', function () { | ||
it('replaces all region with same styling', function () { | ||
pen.red('Hello').sp().green('world!').replaceText(function (styles, text) { | ||
var args = [text.toUpperCase()].concat(styles); | ||
this.text.apply(this, args); | ||
}); | ||
expect(pen, 'to equal', | ||
magicpen().red('HELLO').sp().green('WORLD!')); | ||
}); | ||
}); | ||
describe('with a given pattern and a callback', function () { | ||
it('replaces text content maching pattern', function () { | ||
pen.red('Hello').sp().green('world!').replaceText(/[a-z]{3}/, function (styles, text) { | ||
var args = [text.toUpperCase()].concat(styles); | ||
this.text.apply(this, args); | ||
}); | ||
expect(pen, 'to equal', | ||
magicpen().red('HELLo').sp().green('WORld!')); | ||
}); | ||
}); | ||
describe('with a given pattern and a string', function () { | ||
it('replaces text content maching pattern', function () { | ||
pen.text('Hello ').green('<placeholder>!').replaceText(/<placeholder>/, 'Jane Doe'); | ||
expect(pen, 'to equal', | ||
magicpen().text('Hello ').green('Jane Doe!')); | ||
}); | ||
}); | ||
it('collapses entries with similar styles', function () { | ||
pen.green('Hello').sp().green('world!').replaceText(/ /, function (styles, text) { | ||
this.text(text, 'green'); | ||
}); | ||
expect(pen, 'to equal', | ||
magicpen().green('Hello world!')); | ||
}); | ||
it('is capable of introducing newlines from callback', function () { | ||
pen.text('Hello <placeholder>!').replaceText(/<placeholder>/, function (styles, text) { | ||
this.green('Jane Doe').nl().red('the incredible'); | ||
}); | ||
expect(pen, 'to equal', | ||
magicpen().text('Hello').sp().green('Jane Doe').nl() | ||
.red('the incredible').text('!')); | ||
}); | ||
it('group matches are given as arguments to the replace callback', function () { | ||
pen.text('foo@example.com').replaceText(/(.+)@(.+)/, function (styles, text, user, domain) { | ||
this.text(user + ' at ' + domain); | ||
}); | ||
expect(pen, 'to equal', | ||
magicpen().text('foo at example.com')); | ||
}); | ||
it('replace in complex output', function () { | ||
pen.addStyle('lightGray', function (content) { | ||
this.text(content, '#AAA'); | ||
}); | ||
writeComplicatedExampleWithPen(pen).replaceText(/\//g, function (styles, text) { | ||
this.lightGray(text); | ||
}); | ||
expect(pen, 'to equal', | ||
pen.clone().blue('This').sp().red('will').nl() | ||
.green('output').sp().yellow('a').sp().cyan('dragon:') | ||
.sp().block(function () { | ||
this.gray(" ").lightGray("/").gray(" ").lightGray("/").nl() | ||
.gray(" ").lightGray("/").gray("' .,,,, .").lightGray("/").nl() | ||
.gray(" ").lightGray("/").gray("';' ,").lightGray("/").nl() | ||
.gray(" ").lightGray("/").gray(" ").lightGray("/").gray(" ,,").text("//", "#AAA").gray(",`'`").nl() | ||
.gray(" ( ,, '_, ,,,' ``").nl() | ||
.gray(" | ").lightGray("/").red("@").gray(" ,,, ;\" `").nl() | ||
.gray(" ").lightGray("/").gray(" . ,''").lightGray("/").gray("' `,``").nl() | ||
.gray(" ").lightGray("/").gray(" . .").lightGray("/").gray(", `,, ` ;").nl() | ||
.gray(" ,.").lightGray("/").gray(" . ,-,',` ,,").lightGray("/").gray("''\\,'").nl() | ||
.gray("| ").lightGray("/").gray("; .").lightGray("/").gray(",,'`,,'' | |").nl() | ||
.gray("| ").lightGray("/").gray(" ',' ").lightGray("/").gray(" |").nl() | ||
.gray(" \\___").lightGray("/").gray("' ' | |").nl() | ||
.gray(" `,,' | ").lightGray("/").gray(" `\\").nl() | ||
.gray(" ").lightGray("/").gray(" | ~\\").nl() | ||
.gray(" ' (").nl() | ||
.gray(" :").nl() | ||
.gray(" ; . \\--").nl() | ||
.gray(" : \\ ; ").blue('Ooyamaneko'); | ||
}).append(function () { | ||
this.nl(2).text('stolen from the interwebs.'); | ||
}).prependLinesWith('text', ' ')); | ||
}); | ||
}); | ||
describe('prependLinesWith', function () { | ||
@@ -145,0 +250,0 @@ it('prepends all lines with the the content of the given pen', function () { |
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
461738
2528