Skip to content

Commit e7f0eff

Browse files
committed
- Enhancement: Allow user to pass in reporter
- Testing: Add browser test
1 parent c9ab204 commit e7f0eff

File tree

7 files changed

+179
-17
lines changed

7 files changed

+179
-17
lines changed

index-es.js

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

321+
let _reporter = null;
322+
const report = (reporter) => {
323+
_reporter = reporter;
324+
};
325+
289326
export default consolemd;
327+
export { report };

index-es.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-umd.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (global, factory) {
2-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3-
typeof define === 'function' && define.amd ? define(factory) :
4-
(global.consolemd = factory());
5-
}(this, (function () { 'use strict';
2+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
4+
(factory((global.consolemd = {})));
5+
}(this, (function (exports) { 'use strict';
66

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

295-
return consolemd;
327+
let _reporter = null;
328+
const report = (reporter) => {
329+
_reporter = reporter;
330+
};
331+
332+
exports.report = report;
333+
exports.default = consolemd;
334+
335+
Object.defineProperty(exports, '__esModule', { value: true });
296336

297337
})));

0 commit comments

Comments
 (0)