Skip to content

Commit 008c8f3

Browse files
committed
- Enhancement: Allow user to pass in reporter (and provide default HTML reporter)
- Testing: Add browser test - Travis: Add Node 10 (console.assert at least has some issues fixed)
1 parent 7e049b8 commit 008c8f3

File tree

14 files changed

+591
-23
lines changed

14 files changed

+591
-23
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.DS_Store
22
node_modules
33
.travis.yml
4+
test
5+
core.js
46
package-lock.json

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ node_js:
44
- 0.12
55
- 4
66
- 6
7+
- 10
78
git:
89
depth: 1
910
branches:
1011
only:
11-
- master
12+
- master

cjs/index.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
35
/*! (c) 2013-2018 Andrea Giammarchi (ISC) */
46
/**
57
* Fully inspired by the work of John Gruber
@@ -253,9 +255,41 @@ for (var
253255
return original.apply(console, parse.apply(null, arguments));
254256
} :
255257
function () {
256-
return arguments.length === 1 && typeof arguments[0] === 'string' ?
257-
original.apply(console, parse(arguments[0])) :
258-
original.apply(console, arguments);
258+
var singleStringArg = arguments.length === 1 && typeof arguments[0] === 'string';
259+
var args = singleStringArg ? parse(arguments[0]) : arguments;
260+
261+
// Todo: We might expose more to the reporter (e.g., returning
262+
// `what` and `match` from the `parse`->`match` function) so
263+
// the user could, e.g., build spans with classes rather than
264+
// inline styles
265+
if (_reporter) {
266+
var
267+
lastIndex, resultInfo,
268+
msg = args[0],
269+
formattingArgs = args.slice(1),
270+
formatRegex = /%c(.*?)(?=%c|$)/g,
271+
tmpIndex = 0;
272+
_reporter.init();
273+
while ((resultInfo = formatRegex.exec(msg)) !== null) {
274+
var lastIndex = formatRegex.lastIndex;
275+
var result = resultInfo[0];
276+
if (result.length > 2) { // Ignore any empty %c's
277+
var beginningResultIdx = lastIndex - result.length;
278+
if (beginningResultIdx > tmpIndex) {
279+
var text = msg.slice(tmpIndex, beginningResultIdx);
280+
_reporter.report(text);
281+
}
282+
_reporter.report(result.slice(2), formattingArgs.splice(0, 1)[0]);
283+
}
284+
tmpIndex = lastIndex;
285+
}
286+
if (tmpIndex < msg.length) {
287+
var text = msg.slice(tmpIndex);
288+
_reporter.report(text);
289+
}
290+
_reporter.done(args);
291+
}
292+
return original.apply(console, args);
259293
}).raw = function () {
260294
return original.apply(console, arguments);
261295
};
@@ -289,4 +323,12 @@ try {
289323
}
290324
}
291325

292-
module.exports = consolemd;
326+
var _reporter = null;
327+
var addReporter = function (reporter) {
328+
_reporter = reporter;
329+
};
330+
331+
exports.addReporter = addReporter;
332+
exports.default = consolemd;
333+
334+
module.exports = exports.default;

core.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,41 @@ for (var
251251
return original.apply(console, parse.apply(null, arguments));
252252
} :
253253
function () {
254-
return arguments.length === 1 && typeof arguments[0] === 'string' ?
255-
original.apply(console, parse(arguments[0])) :
256-
original.apply(console, arguments);
254+
var singleStringArg = arguments.length === 1 && typeof arguments[0] === 'string';
255+
var args = singleStringArg ? parse(arguments[0]) : arguments;
256+
257+
// Todo: We might expose more to the reporter (e.g., returning
258+
// `what` and `match` from the `parse`->`match` function) so
259+
// the user could, e.g., build spans with classes rather than
260+
// inline styles
261+
if (_reporter) {
262+
var
263+
lastIndex, resultInfo,
264+
msg = args[0],
265+
formattingArgs = args.slice(1),
266+
formatRegex = /%c(.*?)(?=%c|$)/g,
267+
tmpIndex = 0;
268+
_reporter.init();
269+
while ((resultInfo = formatRegex.exec(msg)) !== null) {
270+
var lastIndex = formatRegex.lastIndex;
271+
var result = resultInfo[0];
272+
if (result.length > 2) { // Ignore any empty %c's
273+
var beginningResultIdx = lastIndex - result.length;
274+
if (beginningResultIdx > tmpIndex) {
275+
var text = msg.slice(tmpIndex, beginningResultIdx);
276+
_reporter.report(text);
277+
}
278+
_reporter.report(result.slice(2), formattingArgs.splice(0, 1)[0]);
279+
}
280+
tmpIndex = lastIndex;
281+
}
282+
if (tmpIndex < msg.length) {
283+
var text = msg.slice(tmpIndex);
284+
_reporter.report(text);
285+
}
286+
_reporter.done(args);
287+
}
288+
return original.apply(console, args);
257289
}).raw = function () {
258290
return original.apply(console, arguments);
259291
};
@@ -287,4 +319,10 @@ try {
287319
}
288320
}
289321

322+
var _reporter = null;
323+
var addReporter = function (reporter) {
324+
_reporter = reporter;
325+
};
326+
327+
export { addReporter };
290328
export default consolemd;

esm/index.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,41 @@ for (var
251251
return original.apply(console, parse.apply(null, arguments));
252252
} :
253253
function () {
254-
return arguments.length === 1 && typeof arguments[0] === 'string' ?
255-
original.apply(console, parse(arguments[0])) :
256-
original.apply(console, arguments);
254+
var singleStringArg = arguments.length === 1 && typeof arguments[0] === 'string';
255+
var args = singleStringArg ? parse(arguments[0]) : arguments;
256+
257+
// Todo: We might expose more to the reporter (e.g., returning
258+
// `what` and `match` from the `parse`->`match` function) so
259+
// the user could, e.g., build spans with classes rather than
260+
// inline styles
261+
if (_reporter) {
262+
var
263+
lastIndex, resultInfo,
264+
msg = args[0],
265+
formattingArgs = args.slice(1),
266+
formatRegex = /%c(.*?)(?=%c|$)/g,
267+
tmpIndex = 0;
268+
_reporter.init();
269+
while ((resultInfo = formatRegex.exec(msg)) !== null) {
270+
var lastIndex = formatRegex.lastIndex;
271+
var result = resultInfo[0];
272+
if (result.length > 2) { // Ignore any empty %c's
273+
var beginningResultIdx = lastIndex - result.length;
274+
if (beginningResultIdx > tmpIndex) {
275+
var text = msg.slice(tmpIndex, beginningResultIdx);
276+
_reporter.report(text);
277+
}
278+
_reporter.report(result.slice(2), formattingArgs.splice(0, 1)[0]);
279+
}
280+
tmpIndex = lastIndex;
281+
}
282+
if (tmpIndex < msg.length) {
283+
var text = msg.slice(tmpIndex);
284+
_reporter.report(text);
285+
}
286+
_reporter.done(args);
287+
}
288+
return original.apply(console, args);
257289
}).raw = function () {
258290
return original.apply(console, arguments);
259291
};
@@ -287,4 +319,10 @@ try {
287319
}
288320
}
289321

322+
var _reporter = null;
323+
var addReporter = function (reporter) {
324+
_reporter = reporter;
325+
};
326+
290327
export default consolemd;
328+
export { addReporter };

esm/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var consolemd = (function () {
1+
var consolemd = (function (exports) {
22
'use strict';
33

44
/*! (c) 2013-2018 Andrea Giammarchi (ISC) */
@@ -254,9 +254,41 @@ var consolemd = (function () {
254254
return original.apply(console, parse.apply(null, arguments));
255255
} :
256256
function () {
257-
return arguments.length === 1 && typeof arguments[0] === 'string' ?
258-
original.apply(console, parse(arguments[0])) :
259-
original.apply(console, arguments);
257+
var singleStringArg = arguments.length === 1 && typeof arguments[0] === 'string';
258+
var args = singleStringArg ? parse(arguments[0]) : arguments;
259+
260+
// Todo: We might expose more to the reporter (e.g., returning
261+
// `what` and `match` from the `parse`->`match` function) so
262+
// the user could, e.g., build spans with classes rather than
263+
// inline styles
264+
if (_reporter) {
265+
var
266+
lastIndex, resultInfo,
267+
msg = args[0],
268+
formattingArgs = args.slice(1),
269+
formatRegex = /%c(.*?)(?=%c|$)/g,
270+
tmpIndex = 0;
271+
_reporter.init();
272+
while ((resultInfo = formatRegex.exec(msg)) !== null) {
273+
var lastIndex = formatRegex.lastIndex;
274+
var result = resultInfo[0];
275+
if (result.length > 2) { // Ignore any empty %c's
276+
var beginningResultIdx = lastIndex - result.length;
277+
if (beginningResultIdx > tmpIndex) {
278+
var text = msg.slice(tmpIndex, beginningResultIdx);
279+
_reporter.report(text);
280+
}
281+
_reporter.report(result.slice(2), formattingArgs.splice(0, 1)[0]);
282+
}
283+
tmpIndex = lastIndex;
284+
}
285+
if (tmpIndex < msg.length) {
286+
var text = msg.slice(tmpIndex);
287+
_reporter.report(text);
288+
}
289+
_reporter.done(args);
290+
}
291+
return original.apply(console, args);
260292
}).raw = function () {
261293
return original.apply(console, arguments);
262294
};
@@ -290,6 +322,14 @@ var consolemd = (function () {
290322
}
291323
}
292324

293-
return consolemd;
325+
var _reporter = null;
326+
var addReporter = function (reporter) {
327+
_reporter = reporter;
328+
};
329+
330+
exports.addReporter = addReporter;
331+
exports.default = consolemd;
332+
333+
return exports;
294334

295-
}());
335+
}({}));

0 commit comments

Comments
 (0)