From 82810f3dcd649c6b4454d3a083dc67103e6842a1 Mon Sep 17 00:00:00 2001 From: Benny Neugebauer Date: Fri, 20 Jan 2017 14:55:25 +0100 Subject: [PATCH] 2.0.3 --- dist/logdown.js | 548 +++++++++++++++++++++++++++++++++++++++- dist/logdown.min.js | 10 + dist/logdown.min.js.map | 1 + example/lib/logdown.js | 2 +- package.json | 2 +- 5 files changed, 559 insertions(+), 4 deletions(-) create mode 100644 dist/logdown.min.js create mode 100644 dist/logdown.min.js.map diff --git a/dist/logdown.js b/dist/logdown.js index 9043dca..2a534b1 100644 --- a/dist/logdown.js +++ b/dist/logdown.js @@ -1,9 +1,553 @@ /** * logdown - Debug utility with markdown support that runs on browser and server * - * @version v2.0.2 + * @version v2.0.3 * @link https://github.com/caiogondim/logdown * @author Caio Gondim (http://caiogondim.com) * @license MIT */ -!function(){"use strict";function e(o){if(!(this instanceof e))return new e(o);o=o||{};var n=void 0===o.prefix?"":o.prefix;return n=p(n),n&&s(n,g)?u(n,g):(this.alignOutput=Boolean(o.alignOutput),this.markdown=void 0===o.markdown||o.markdown,this.prefix=n,g.push(this),r(g),d()?(this.prefixColor=x[h%x.length],h+=1):f()&&(this.prefixColor=v()),this)}function r(e){var r=e.sort(function(e,r){return r.prefix.length-e.prefix.length})[0];e.forEach(function(e){if(e.alignOutput){var o=new Array(Math.max(r.prefix.length-e.prefix.length+1,0)).join(" ");e.prefix=e.prefix+o}})}function o(e){for(var r=[],o=n(e);o;)e=e.replace(o.rule.regexp,o.rule.replacer),d()&&(r.push(o.rule.style),r.push("")),o=n(e);return{text:e,styles:r}}function n(e){var r=[],o=[];return d()?o=[{regexp:/\*([^\*]+)\*/,replacer:function(e,r){return"%c"+r+"%c"},style:"font-weight:bold;"},{regexp:/_([^_]+)_/,replacer:function(e,r){return"%c"+r+"%c"},style:"font-style:italic;"},{regexp:/`([^`]+)`/,replacer:function(e,r){return"%c"+r+"%c"},style:"background:#FDF6E3; color:#586E75; padding:1px 5px; border-radius:4px;"}]:f()&&(o=[{regexp:/\*([^\*]+)\*/,replacer:function(e,r){return"["+m.modifiers.bold[0]+"m"+r+"["+m.modifiers.bold[1]+"m"}},{regexp:/_([^_]+)_/,replacer:function(e,r){return"["+m.modifiers.italic[0]+"m"+r+"["+m.modifiers.italic[1]+"m"}},{regexp:/`([^`]+)`/,replacer:function(e,r){return"["+m.bgColors.bgYellow[0]+"m["+m.colors.black[0]+"m "+r+" ["+m.colors.black[1]+"m["+m.bgColors.bgYellow[1]+"m"}}]),o.forEach(function(o){var n=e.match(o.regexp);n&&r.push({rule:o,match:n})}),0===r.length?null:(r.sort(function(e,r){return e.match.index-r.match.index}),r[0])}function t(e,r){var n,t=[];return r.prefix?a()?(t.push("%c"+r.prefix+"%c "),t.push("color:"+r.prefixColor+"; font-weight:bold;","")):t.push("["+r.prefix+"] "):t.push(""),"string"==typeof e[0]?r.markdown&&a()?(n=o(e[0]),t[0]=t[0]+n.text,t=t.concat(n.styles)):t[0]=t[0]+e[0]:t[0]=e[0],e.length>1&&(t=t.concat(e.splice(1))),t}function i(e,r,n){var t=[];return n.prefix&&(a()?t[0]="["+n.prefixColor[0]+"m["+m.modifiers.bold[0]+"m"+n.prefix+"["+m.modifiers.bold[1]+"m["+n.prefixColor[1]+"m":t[0]="["+n.prefix+"]"),"warn"===r?t[0]="["+m.colors.yellow[0]+"m⚠["+m.colors.yellow[1]+"m "+(t[0]||""):"error"===r?t[0]="["+m.colors.red[0]+"m✖["+m.colors.red[1]+"m "+(t[0]||""):"info"===r?t[0]="["+m.colors.blue[0]+"mℹ["+m.colors.blue[1]+"m "+(t[0]||""):"debug"===r&&(t[0]="["+m.colors.gray[0]+"m🐛["+m.colors.gray[1]+"m "+(t[0]||"")),e.forEach(function(e){"string"==typeof e&&n.markdown?t.push(o(e).text):t.push(e)}),t}function c(r){var o=null;"undefined"!=typeof process&&void 0!==process.env&&0===b.length&&(void 0!==process.env.NODE_DEBUG&&""!==process.env.NODE_DEBUG?o="NODE_DEBUG":void 0!==process.env.DEBUG&&""!==process.env.DEBUG&&(o="DEBUG"),o&&(e.disable("*"),process.env[o].split(",").forEach(function(r){e.enable(r)})));var n=!1;return b.forEach(function(e){"enable"===e.type&&e.regExp.test(r.prefix)?n=!1:"disable"===e.type&&e.regExp.test(r.prefix)&&(n=!0)}),n}function l(e){return new RegExp("^"+e.replace(/\*/g,".*?")+"$")}function s(e,r){var o=!1;return r.forEach(function(r){if(r.prefix===e)return void(o=!0)}),o}function u(e,r){var o;return r.forEach(function(r){if(r.prefix===e)return void(o=r)}),o}function p(e){return"string"==typeof e?e.replace(/%c/g,""):e}function a(){if(d()){var e="WebkitAppearance"in document.documentElement.style,r=window.console&&(console.firebug||console.exception&&console.table),o=navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31;return e||r||o}if(f())return!(process.stdout&&!process.stdout.isTTY)&&("win32"===process.platform||("COLORTERM"in process.env||"dumb"!==process.env.TERM&&!!/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)))}function f(){return"undefined"!=typeof module&&"undefined"!=typeof module.exports}function d(){return"undefined"!=typeof window}var g=[],h=0,x=["#B58900","#CB4B16","#DC322F","#D33682","#6C71C4","#268BD2","#2AA198","#859900"],m={modifiers:{reset:[0,0],bold:[1,22],dim:[2,22],italic:[3,23],underline:[4,24],inverse:[7,27],hidden:[8,28],strikethrough:[9,29]},colors:{black:[30,39],red:[31,39],green:[32,39],yellow:[33,39],blue:[34,39],magenta:[35,39],cyan:[36,39],white:[37,39],gray:[90,39]},bgColors:{bgBlack:[40,49],bgRed:[41,49],bgGreen:[42,49],bgYellow:[43,49],bgBlue:[44,49],bgMagenta:[45,49],bgCyan:[46,49],bgWhite:[47,49]}},b=[];e.enable=function(){Array.prototype.forEach.call(arguments,function(r){"-"===r[0]&&e.disable(r.substr(1));var o=l(r);"*"===r?b=[]:b.push({type:"enable",regExp:o})})},e.disable=function(){Array.prototype.forEach.call(arguments,function(r){"-"===r[0]&&e.enable(r.substr(1));var o=l(r);"*"===r?b=[{type:"disable",regExp:o}]:b.push({type:"disable",regExp:o})})};var y=["debug","log","info","warn","error"];y.forEach(function(r){e.prototype[r]=function(){if(!c(this)){var e,o=Array.prototype.slice.call(arguments,0);d()?(e=t(o,this),Function.prototype.apply.call(console[r]||console.log,console,e)):f()&&(e=i(o,r,this),(console[r]||console.log).apply(console,e))}}});var v=function(){var e=0,r=[[31,39],[32,39],[33,39],[34,39],[35,39],[36,39]];return function(){return r[(e+=1)%r.length]}}();f()?module.exports=e:d()&&(window.Logdown=e)}(); \ No newline at end of file +/* global console, module, window, document, navigator, process */ + +;(function () { + 'use strict' + + var instances = [] + var lastUsedColorIndex = 0 + // Solarized accent colors http://ethanschoonover.com/solarized + var colors = [ + '#B58900', + '#CB4B16', + '#DC322F', + '#D33682', + '#6C71C4', + '#268BD2', + '#2AA198', + '#859900' + ] + // Taken from ansi-styles npm module + // https://github.com/sindresorhus/ansi-styles/blob/master/index.js + var ansiColors = { + modifiers: { + reset: [0, 0], + bold: [1, 22], // 21 isn't widely supported and 22 does the same thing + dim: [2, 22], + italic: [3, 23], + underline: [4, 24], + inverse: [7, 27], + hidden: [8, 28], + strikethrough: [9, 29] + }, + colors: { + black: [30, 39], + red: [31, 39], + green: [32, 39], + yellow: [33, 39], + blue: [34, 39], + magenta: [35, 39], + cyan: [36, 39], + white: [37, 39], + gray: [90, 39] + }, + bgColors: { + bgBlack: [40, 49], + bgRed: [41, 49], + bgGreen: [42, 49], + bgYellow: [43, 49], + bgBlue: [44, 49], + bgMagenta: [45, 49], + bgCyan: [46, 49], + bgWhite: [47, 49] + } + } + var filterRegExps = [] + + function Logdown (opts) { + // Enforces new. + if (!(this instanceof Logdown)) { + return new Logdown(opts) + } + + opts = opts || {} + + var prefix = opts.prefix === undefined ? '' : opts.prefix + prefix = sanitizeStringToBrowser(prefix) + if (prefix && isPrefixAlreadyInUse(prefix, instances)) { + return getInstanceByPrefix(prefix, instances) + } + + // + this.alignOutput = Boolean(opts.alignOutput) + this.markdown = opts.markdown === undefined ? true : opts.markdown + this.prefix = prefix + + // + instances.push(this) + alignPrefixes(instances) + + if (isBrowser()) { + this.prefixColor = colors[lastUsedColorIndex % colors.length] + lastUsedColorIndex += 1 + } else if (isNode()) { + this.prefixColor = getNextPrefixColor() + } + + return this + } + + // Static + // ------ + + Logdown.enable = function () { + Array.prototype.forEach.call(arguments, function (str) { + if (str[0] === '-') { + Logdown.disable(str.substr(1)) + } + + var regExp = prepareRegExpForPrefixSearch(str) + + if (str === '*') { + filterRegExps = [] + } else { + filterRegExps.push({ + type: 'enable', + regExp: regExp + }) + } + }) + } + + Logdown.disable = function () { + Array.prototype.forEach.call(arguments, function (str) { + if (str[0] === '-') { + Logdown.enable(str.substr(1)) + } + + var regExp = prepareRegExpForPrefixSearch(str) + + if (str === '*') { + filterRegExps = [{ + type: 'disable', + regExp: regExp + }] + } else { + filterRegExps.push({ + type: 'disable', + regExp: regExp + }) + } + }) + } + + // Public + // ------ + + var methods = ['debug', 'log', 'info', 'warn', 'error'] + methods.forEach(function (method) { + Logdown.prototype[method] = function () { + if (isDisabled(this)) { + return + } + + var preparedOutput + var args = Array.prototype.slice.call(arguments, 0) + + if (isBrowser()) { + preparedOutput = prepareOutputToBrowser(args, this) + + // IE9 workaround + // http://stackoverflow.com/questions/5538972/ + // console-log-apply-not-working-in-ie9 + Function.prototype.apply.call( + console[method] || console.log, + console, + preparedOutput + ) + } else if (isNode()) { + preparedOutput = prepareOutputToNode(args, method, this) + + ;(console[method] || console.log).apply( + console, + preparedOutput + ) + } + } + }) + + // Private + // ------- + + function alignPrefixes (instances) { + var longest = instances.sort(function (a, b) { + return b.prefix.length - a.prefix.length + })[0] + + instances.forEach(function (instance) { + if (instance.alignOutput) { + var padding = new Array(Math.max(longest.prefix.length - instance.prefix.length + 1, 0)).join(' ') + instance.prefix = instance.prefix + padding + } + }) + } + + function parseMarkdown (text) { + var styles = [] + var match = getNextMatch(text) + + while (match) { + text = text.replace(match.rule.regexp, match.rule.replacer) + + if (isBrowser()) { + styles.push(match.rule.style) + styles.push('') // Empty string resets style. + } + + match = getNextMatch(text) + } + + return {text: text, styles: styles} + } + + function getNextMatch (text) { + var matches = [] + var rules = [] + if (isBrowser()) { + rules = [ + { + regexp: /\*([^\*]+)\*/, + replacer: function (match, submatch1) { + return '%c' + submatch1 + '%c' + }, + style: 'font-weight:bold;' + }, + { + regexp: /_([^_]+)_/, + replacer: function (match, submatch1) { + return '%c' + submatch1 + '%c' + }, + style: 'font-style:italic;' + }, + { + regexp: /`([^`]+)`/, + replacer: function (match, submatch1) { + return '%c' + submatch1 + '%c' + }, + style: + 'background:#FDF6E3; ' + + 'color:#586E75; ' + + 'padding:1px 5px; ' + + 'border-radius:4px;' + } + ] + } else if (isNode()) { + rules = [ + { + regexp: /\*([^\*]+)\*/, + replacer: function (match, submatch1) { + return '\u001b[' + ansiColors.modifiers.bold[0] + 'm' + + submatch1 + + '\u001b[' + ansiColors.modifiers.bold[1] + 'm' + } + }, + { + regexp: /_([^_]+)_/, + replacer: function (match, submatch1) { + return '\u001b[' + ansiColors.modifiers.italic[0] + 'm' + + submatch1 + + '\u001b[' + ansiColors.modifiers.italic[1] + 'm' + } + }, + { + regexp: /`([^`]+)`/, + replacer: function (match, submatch1) { + return '\u001b[' + ansiColors.bgColors.bgYellow[0] + 'm' + + '\u001b[' + ansiColors.colors.black[0] + 'm' + + ' ' + submatch1 + ' ' + + '\u001b[' + ansiColors.colors.black[1] + 'm' + + '\u001b[' + ansiColors.bgColors.bgYellow[1] + 'm' + } + } + ] + } + + // + rules.forEach(function (rule) { + var match = text.match(rule.regexp) + if (match) { + matches.push({ + rule: rule, + match: match + }) + } + }) + if (matches.length === 0) { + return null + } + + // + matches.sort(function (a, b) { + return a.match.index - b.match.index + }) + + return matches[0] + } + + function prepareOutputToBrowser (args, instance) { + var preparedOutput = [] + var parsedMarkdown + + if (instance.prefix) { + if (isColorSupported()) { + preparedOutput.push('%c' + instance.prefix + '%c ') + preparedOutput.push( + 'color:' + instance.prefixColor + '; font-weight:bold;', + '' // Empty string resets style. + ) + } else { + preparedOutput.push('[' + instance.prefix + '] ') + } + } else { + preparedOutput.push('') + } + + // Only first argument on `console` can have style. + if (typeof args[0] === 'string') { + if (instance.markdown && isColorSupported()) { + parsedMarkdown = parseMarkdown(args[0]) + preparedOutput[0] = preparedOutput[0] + parsedMarkdown.text + preparedOutput = preparedOutput.concat(parsedMarkdown.styles) + } else { + preparedOutput[0] = preparedOutput[0] + args[0] + } + } else { + preparedOutput[0] = args[0] + } + + if (args.length > 1) { + preparedOutput = preparedOutput.concat(args.splice(1)) + } + + return preparedOutput + } + + function prepareOutputToNode (args, method, instance) { + var preparedOutput = [] + + if (instance.prefix) { + if (isColorSupported()) { + preparedOutput[0] = + '\u001b[' + instance.prefixColor[0] + 'm' + + '\u001b[' + ansiColors.modifiers.bold[0] + 'm' + + instance.prefix + + '\u001b[' + ansiColors.modifiers.bold[1] + 'm' + + '\u001b[' + instance.prefixColor[1] + 'm' + } else { + preparedOutput[0] = '[' + instance.prefix + ']' + } + } + + if (method === 'warn') { + preparedOutput[0] = + '\u001b[' + ansiColors.colors.yellow[0] + 'm' + + '⚠' + + '\u001b[' + ansiColors.colors.yellow[1] + 'm ' + + (preparedOutput[0] || '') + } else if (method === 'error') { + preparedOutput[0] = + '\u001b[' + ansiColors.colors.red[0] + 'm' + + '✖' + + '\u001b[' + ansiColors.colors.red[1] + 'm ' + + (preparedOutput[0] || '') + } else if (method === 'info') { + preparedOutput[0] = + '\u001b[' + ansiColors.colors.blue[0] + 'm' + + 'ℹ' + + '\u001b[' + ansiColors.colors.blue[1] + 'm ' + + (preparedOutput[0] || '') + } else if (method === 'debug') { + preparedOutput[0] = + '\u001b[' + ansiColors.colors.gray[0] + 'm' + + '🐛' + + '\u001b[' + ansiColors.colors.gray[1] + 'm ' + + (preparedOutput[0] || '') + } + + args.forEach(function (arg) { + if (typeof arg === 'string') { + if (instance.markdown) { + preparedOutput.push(parseMarkdown(arg).text) + } else { + preparedOutput.push(arg) + } + } else { + preparedOutput.push(arg) + } + }) + + return preparedOutput + } + + function isDisabled (instance) { + // Parsing `NODE_DEBUG` and `DEBUG` env var. + // We verify `NODE_DEBUG` and `DEBUG` env vars on runtime so it is + // easier to test. + var envVar = null + if (typeof process !== 'undefined' && + process.env !== undefined && + filterRegExps.length === 0) { + // `NODE_DEBUG` has precedence over `DEBUG` + if (process.env.NODE_DEBUG !== undefined && + process.env.NODE_DEBUG !== '') { + envVar = 'NODE_DEBUG' + } else if (process.env.DEBUG !== undefined && + process.env.DEBUG !== '') { + envVar = 'DEBUG' + } + + if (envVar) { + Logdown.disable('*') + process.env[envVar] + .split(',') + .forEach(function (regExp) { + Logdown.enable(regExp) + }) + } + } + + // Now checks if instance is disabled + var isDisabled_ = false + filterRegExps.forEach(function (filter) { + if (filter.type === 'enable' && filter.regExp.test(instance.prefix)) { + isDisabled_ = false + } else if (filter.type === 'disable' && + filter.regExp.test(instance.prefix)) { + isDisabled_ = true + } + }) + + return isDisabled_ + } + + function prepareRegExpForPrefixSearch (str) { + return new RegExp('^' + str.replace(/\*/g, '.*?') + '$') + } + + function isPrefixAlreadyInUse (prefix, instances) { + var isPrefixAlreadyInUse_ = false + + instances.forEach(function (instance) { + if (instance.prefix === prefix) { + isPrefixAlreadyInUse_ = true + return + } + }) + + return isPrefixAlreadyInUse_ + } + + function getInstanceByPrefix (prefix, instances) { + var instance + + instances.forEach(function (instanceCur) { + if (instanceCur.prefix === prefix) { + instance = instanceCur + return + } + }) + + return instance + } + + function sanitizeStringToBrowser (str) { + if (typeof str === 'string') { + return str.replace(/%c/g, '') + } else { + return str + } + } + + /** + * Currently only WebKit-based Web Inspectors, Firefox >= v31, + * and the Firebug extension (any Firefox version) are known + * to support "%c" CSS customizations. + * + * Code took from https://github.com/visionmedia/debug/blob/master/browser.js + */ + function isColorSupported () { + if (isBrowser()) { + // Is webkit? http://stackoverflow.com/a/16459606/376773 + var isWebkit = ('WebkitAppearance' in document.documentElement.style) + // Is firebug? http://stackoverflow.com/a/398120/376773 + var isFirebug = ( + window.console && + (console.firebug || (console.exception && console.table)) + ) + // Is firefox >= v31? + // https://developer.mozilla.org/en-US/docs/Tools/ + // Web_Console#Styling_messages + var isFirefox31Plus = ( + navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && + parseInt(RegExp.$1, 10) >= 31 + ) + + return (isWebkit || isFirebug || isFirefox31Plus) + } else if (isNode()) { + if (process.stdout && !process.stdout.isTTY) { + return false + } + + if (process.platform === 'win32') { + return true + } + + if ('COLORTERM' in process.env) { + return true + } + + if (process.env.TERM === 'dumb') { + return false + } + + if ( + /^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM) + ) { + return true + } + + return false + } + } + + function isNode () { + return ( + typeof module !== 'undefined' && + typeof module.exports !== 'undefined' + ) + } + + function isBrowser () { + return (typeof window !== 'undefined') + } + + var getNextPrefixColor = (function () { + var lastUsed = 0 + var nodePrefixColors = [ + [31, 39], // red + [32, 39], // green + [33, 39], // yellow + [34, 39], // blue + [35, 39], // magenta + [36, 39] // cyan + ] + + return function () { + return nodePrefixColors[(lastUsed += 1) % nodePrefixColors.length] + } + })() + + // Export module + if (isNode()) { + module.exports = Logdown + } else if (isBrowser()) { + window.Logdown = Logdown + } +}()) diff --git a/dist/logdown.min.js b/dist/logdown.min.js new file mode 100644 index 0000000..97c7b96 --- /dev/null +++ b/dist/logdown.min.js @@ -0,0 +1,10 @@ +/** + * logdown - Debug utility with markdown support that runs on browser and server + * + * @version v2.0.3 + * @link https://github.com/caiogondim/logdown + * @author Caio Gondim (http://caiogondim.com) + * @license MIT + */ +!function(){"use strict";function e(o){if(!(this instanceof e))return new e(o);o=o||{};var n=void 0===o.prefix?"":o.prefix;return n=p(n),n&&s(n,g)?u(n,g):(this.alignOutput=Boolean(o.alignOutput),this.markdown=void 0===o.markdown||o.markdown,this.prefix=n,g.push(this),r(g),d()?(this.prefixColor=x[h%x.length],h+=1):f()&&(this.prefixColor=v()),this)}function r(e){var r=e.sort(function(e,r){return r.prefix.length-e.prefix.length})[0];e.forEach(function(e){if(e.alignOutput){var o=new Array(Math.max(r.prefix.length-e.prefix.length+1,0)).join(" ");e.prefix=e.prefix+o}})}function o(e){for(var r=[],o=n(e);o;)e=e.replace(o.rule.regexp,o.rule.replacer),d()&&(r.push(o.rule.style),r.push("")),o=n(e);return{text:e,styles:r}}function n(e){var r=[],o=[];return d()?o=[{regexp:/\*([^\*]+)\*/,replacer:function(e,r){return"%c"+r+"%c"},style:"font-weight:bold;"},{regexp:/_([^_]+)_/,replacer:function(e,r){return"%c"+r+"%c"},style:"font-style:italic;"},{regexp:/`([^`]+)`/,replacer:function(e,r){return"%c"+r+"%c"},style:"background:#FDF6E3; color:#586E75; padding:1px 5px; border-radius:4px;"}]:f()&&(o=[{regexp:/\*([^\*]+)\*/,replacer:function(e,r){return"["+m.modifiers.bold[0]+"m"+r+"["+m.modifiers.bold[1]+"m"}},{regexp:/_([^_]+)_/,replacer:function(e,r){return"["+m.modifiers.italic[0]+"m"+r+"["+m.modifiers.italic[1]+"m"}},{regexp:/`([^`]+)`/,replacer:function(e,r){return"["+m.bgColors.bgYellow[0]+"m["+m.colors.black[0]+"m "+r+" ["+m.colors.black[1]+"m["+m.bgColors.bgYellow[1]+"m"}}]),o.forEach(function(o){var n=e.match(o.regexp);n&&r.push({rule:o,match:n})}),0===r.length?null:(r.sort(function(e,r){return e.match.index-r.match.index}),r[0])}function t(e,r){var n,t=[];return r.prefix?a()?(t.push("%c"+r.prefix+"%c "),t.push("color:"+r.prefixColor+"; font-weight:bold;","")):t.push("["+r.prefix+"] "):t.push(""),"string"==typeof e[0]?r.markdown&&a()?(n=o(e[0]),t[0]=t[0]+n.text,t=t.concat(n.styles)):t[0]=t[0]+e[0]:t[0]=e[0],e.length>1&&(t=t.concat(e.splice(1))),t}function i(e,r,n){var t=[];return n.prefix&&(a()?t[0]="["+n.prefixColor[0]+"m["+m.modifiers.bold[0]+"m"+n.prefix+"["+m.modifiers.bold[1]+"m["+n.prefixColor[1]+"m":t[0]="["+n.prefix+"]"),"warn"===r?t[0]="["+m.colors.yellow[0]+"m⚠["+m.colors.yellow[1]+"m "+(t[0]||""):"error"===r?t[0]="["+m.colors.red[0]+"m✖["+m.colors.red[1]+"m "+(t[0]||""):"info"===r?t[0]="["+m.colors.blue[0]+"mℹ["+m.colors.blue[1]+"m "+(t[0]||""):"debug"===r&&(t[0]="["+m.colors.gray[0]+"m🐛["+m.colors.gray[1]+"m "+(t[0]||"")),e.forEach(function(e){"string"==typeof e&&n.markdown?t.push(o(e).text):t.push(e)}),t}function c(r){var o=null;"undefined"!=typeof process&&void 0!==process.env&&0===b.length&&(void 0!==process.env.NODE_DEBUG&&""!==process.env.NODE_DEBUG?o="NODE_DEBUG":void 0!==process.env.DEBUG&&""!==process.env.DEBUG&&(o="DEBUG"),o&&(e.disable("*"),process.env[o].split(",").forEach(function(r){e.enable(r)})));var n=!1;return b.forEach(function(e){"enable"===e.type&&e.regExp.test(r.prefix)?n=!1:"disable"===e.type&&e.regExp.test(r.prefix)&&(n=!0)}),n}function l(e){return new RegExp("^"+e.replace(/\*/g,".*?")+"$")}function s(e,r){var o=!1;return r.forEach(function(r){if(r.prefix===e)return void(o=!0)}),o}function u(e,r){var o;return r.forEach(function(r){if(r.prefix===e)return void(o=r)}),o}function p(e){return"string"==typeof e?e.replace(/%c/g,""):e}function a(){if(d()){var e="WebkitAppearance"in document.documentElement.style,r=window.console&&(console.firebug||console.exception&&console.table),o=navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31;return e||r||o}if(f())return!(process.stdout&&!process.stdout.isTTY)&&("win32"===process.platform||("COLORTERM"in process.env||"dumb"!==process.env.TERM&&!!/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)))}function f(){return"undefined"!=typeof module&&"undefined"!=typeof module.exports}function d(){return"undefined"!=typeof window}var g=[],h=0,x=["#B58900","#CB4B16","#DC322F","#D33682","#6C71C4","#268BD2","#2AA198","#859900"],m={modifiers:{reset:[0,0],bold:[1,22],dim:[2,22],italic:[3,23],underline:[4,24],inverse:[7,27],hidden:[8,28],strikethrough:[9,29]},colors:{black:[30,39],red:[31,39],green:[32,39],yellow:[33,39],blue:[34,39],magenta:[35,39],cyan:[36,39],white:[37,39],gray:[90,39]},bgColors:{bgBlack:[40,49],bgRed:[41,49],bgGreen:[42,49],bgYellow:[43,49],bgBlue:[44,49],bgMagenta:[45,49],bgCyan:[46,49],bgWhite:[47,49]}},b=[];e.enable=function(){Array.prototype.forEach.call(arguments,function(r){"-"===r[0]&&e.disable(r.substr(1));var o=l(r);"*"===r?b=[]:b.push({type:"enable",regExp:o})})},e.disable=function(){Array.prototype.forEach.call(arguments,function(r){"-"===r[0]&&e.enable(r.substr(1));var o=l(r);"*"===r?b=[{type:"disable",regExp:o}]:b.push({type:"disable",regExp:o})})};var y=["debug","log","info","warn","error"];y.forEach(function(r){e.prototype[r]=function(){if(!c(this)){var e,o=Array.prototype.slice.call(arguments,0);d()?(e=t(o,this),Function.prototype.apply.call(console[r]||console.log,console,e)):f()&&(e=i(o,r,this),(console[r]||console.log).apply(console,e))}}});var v=function(){var e=0,r=[[31,39],[32,39],[33,39],[34,39],[35,39],[36,39]];return function(){return r[(e+=1)%r.length]}}();f()?module.exports=e:d()&&(window.Logdown=e)}(); +//# sourceMappingURL=logdown.min.js.map diff --git a/dist/logdown.min.js.map b/dist/logdown.min.js.map new file mode 100644 index 0000000..9bc9eb6 --- /dev/null +++ b/dist/logdown.min.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["logdown.js"],"names":["Logdown","opts","this","prefix","undefined","sanitizeStringToBrowser","isPrefixAlreadyInUse","instances","getInstanceByPrefix","alignOutput","Boolean","markdown","push","alignPrefixes","isBrowser","prefixColor","colors","lastUsedColorIndex","length","isNode","getNextPrefixColor","longest","sort","a","b","forEach","instance","padding","Array","Math","max","join","parseMarkdown","text","styles","match","getNextMatch","replace","rule","regexp","replacer","style","matches","rules","submatch1","ansiColors","modifiers","bold","italic","bgColors","bgYellow","black","index","prepareOutputToBrowser","args","parsedMarkdown","preparedOutput","isColorSupported","concat","splice","prepareOutputToNode","method","yellow","red","blue","gray","arg","isDisabled","envVar","process","env","filterRegExps","NODE_DEBUG","DEBUG","disable","split","regExp","enable","isDisabled_","filter","type","test","prepareRegExpForPrefixSearch","str","RegExp","isPrefixAlreadyInUse_","instanceCur","isWebkit","document","documentElement","isFirebug","window","console","firebug","exception","table","isFirefox31Plus","navigator","userAgent","toLowerCase","parseInt","$1","stdout","isTTY","platform","TERM","module","exports","reset","dim","underline","inverse","hidden","strikethrough","green","magenta","cyan","white","bgBlack","bgRed","bgGreen","bgBlue","bgMagenta","bgCyan","bgWhite","prototype","call","arguments","substr","methods","slice","Function","apply","log","lastUsed","nodePrefixColors"],"mappings":";;;;;;;;CAEA,WACA,YAoDA,SAAAA,GAAAC,GAEA,KAAAC,eAAAF,IACA,MAAA,IAAAA,GAAAC,EAGAA,GAAAA,KAEA,IAAAE,GAAAC,SAAAH,EAAAE,OAAA,GAAAF,EAAAE,MAEA,OADAA,GAAAE,EAAAF,GACAA,GAAAG,EAAAH,EAAAI,GACAC,EAAAL,EAAAI,IAIAL,KAAAO,YAAAC,QAAAT,EAAAQ,aACAP,KAAAS,SAAAP,SAAAH,EAAAU,UAAAV,EAAAU,SACAT,KAAAC,OAAAA,EAGAI,EAAAK,KAAAV,MACAW,EAAAN,GAEAO,KACAZ,KAAAa,YAAAC,EAAAC,EAAAD,EAAAE,QACAD,GAAA,GACAE,MACAjB,KAAAa,YAAAK,KAGAlB,MAqFA,QAAAW,GAAAN,GACA,GAAAc,GAAAd,EAAAe,KAAA,SAAAC,EAAAC,GACA,MAAAA,GAAArB,OAAAe,OAAAK,EAAApB,OAAAe,SACA,EAEAX,GAAAkB,QAAA,SAAAC,GAC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,IAsCAvE,GAAA6E,OAAA,WACAjD,MAAAiG,UAAApG,QAAAqG,KAAAC,UAAA,SAAA5C,GACA,MAAAA,EAAA,IACAnF,EAAA0E,QAAAS,EAAA6C,OAAA,GAGA,IAAApD,GAAAM,EAAAC,EAEA,OAAAA,EACAZ,KAEAA,EAAA3D,MACAoE,KAAA,SACAJ,OAAAA,OAMA5E,EAAA0E,QAAA,WACA9C,MAAAiG,UAAApG,QAAAqG,KAAAC,UAAA,SAAA5C,GACA,MAAAA,EAAA,IACAnF,EAAA6E,OAAAM,EAAA6C,OAAA,GAGA,IAAApD,GAAAM,EAAAC,EAEA,OAAAA,EACAZ,IACAS,KAAA,UACAJ,OAAAA,IAGAL,EAAA3D,MACAoE,KAAA,UACAJ,OAAAA,MASA,IAAAqD,IAAA,QAAA,MAAA,OAAA,OAAA,QACAA,GAAAxG,QAAA,SAAAoC,GACA7D,EAAA6H,UAAAhE,GAAA,WACA,IAAAM,EAAAjE,MAAA,CAIA,GAAAsD,GACAF,EAAA1B,MAAAiG,UAAAK,MAAAJ,KAAAC,UAAA,EAEAjH,MACA0C,EAAAH,EAAAC,EAAApD,MAKAiI,SAAAN,UAAAO,MAAAN,KACAlC,QAAA/B,IAAA+B,QAAAyC,IACAzC,QACApC,IAEArC,MACAqC,EAAAI,EAAAN,EAAAO,EAAA3D,OAEA0F,QAAA/B,IAAA+B,QAAAyC,KAAAD,MACAxC,QACApC,OAyWA,IAAApC,GAAA,WACA,GAAAkH,GAAA,EACAC,IACA,GAAA,KACA,GAAA,KACA,GAAA,KACA,GAAA,KACA,GAAA,KACA,GAAA,IAGA,OAAA,YACA,MAAAA,IAAAD,GAAA,GAAAC,EAAArH,WAKAC,KACAuF,OAAAC,QAAA3G,EACAc,MACA6E,OAAA3F,QAAAA","file":"logdown.min.js","sourcesContent":["/**\n * logdown - Debug utility with markdown support that runs on browser and server\n *\n * @version v2.0.3\n * @link https://github.com/caiogondim/logdown\n * @author Caio Gondim (http://caiogondim.com)\n * @license MIT\n */\n/* global console, module, window, document, navigator, process */\r\n\r\n;(function () {\r\n 'use strict'\r\n\r\n var instances = []\r\n var lastUsedColorIndex = 0\r\n // Solarized accent colors http://ethanschoonover.com/solarized\r\n var colors = [\r\n '#B58900',\r\n '#CB4B16',\r\n '#DC322F',\r\n '#D33682',\r\n '#6C71C4',\r\n '#268BD2',\r\n '#2AA198',\r\n '#859900'\r\n ]\r\n // Taken from ansi-styles npm module\r\n // https://github.com/sindresorhus/ansi-styles/blob/master/index.js\r\n var ansiColors = {\r\n modifiers: {\r\n reset: [0, 0],\r\n bold: [1, 22], // 21 isn't widely supported and 22 does the same thing\r\n dim: [2, 22],\r\n italic: [3, 23],\r\n underline: [4, 24],\r\n inverse: [7, 27],\r\n hidden: [8, 28],\r\n strikethrough: [9, 29]\r\n },\r\n colors: {\r\n black: [30, 39],\r\n red: [31, 39],\r\n green: [32, 39],\r\n yellow: [33, 39],\r\n blue: [34, 39],\r\n magenta: [35, 39],\r\n cyan: [36, 39],\r\n white: [37, 39],\r\n gray: [90, 39]\r\n },\r\n bgColors: {\r\n bgBlack: [40, 49],\r\n bgRed: [41, 49],\r\n bgGreen: [42, 49],\r\n bgYellow: [43, 49],\r\n bgBlue: [44, 49],\r\n bgMagenta: [45, 49],\r\n bgCyan: [46, 49],\r\n bgWhite: [47, 49]\r\n }\r\n }\r\n var filterRegExps = []\r\n\r\n function Logdown (opts) {\r\n // Enforces new.\r\n if (!(this instanceof Logdown)) {\r\n return new Logdown(opts)\r\n }\r\n\r\n opts = opts || {}\r\n\r\n var prefix = opts.prefix === undefined ? '' : opts.prefix\r\n prefix = sanitizeStringToBrowser(prefix)\r\n if (prefix && isPrefixAlreadyInUse(prefix, instances)) {\r\n return getInstanceByPrefix(prefix, instances)\r\n }\r\n\r\n //\r\n this.alignOutput = Boolean(opts.alignOutput)\r\n this.markdown = opts.markdown === undefined ? true : opts.markdown\r\n this.prefix = prefix\r\n\r\n //\r\n instances.push(this)\r\n alignPrefixes(instances)\r\n\r\n if (isBrowser()) {\r\n this.prefixColor = colors[lastUsedColorIndex % colors.length]\r\n lastUsedColorIndex += 1\r\n } else if (isNode()) {\r\n this.prefixColor = getNextPrefixColor()\r\n }\r\n\r\n return this\r\n }\r\n\r\n // Static\r\n // ------\r\n\r\n Logdown.enable = function () {\r\n Array.prototype.forEach.call(arguments, function (str) {\r\n if (str[0] === '-') {\r\n Logdown.disable(str.substr(1))\r\n }\r\n\r\n var regExp = prepareRegExpForPrefixSearch(str)\r\n\r\n if (str === '*') {\r\n filterRegExps = []\r\n } else {\r\n filterRegExps.push({\r\n type: 'enable',\r\n regExp: regExp\r\n })\r\n }\r\n })\r\n }\r\n\r\n Logdown.disable = function () {\r\n Array.prototype.forEach.call(arguments, function (str) {\r\n if (str[0] === '-') {\r\n Logdown.enable(str.substr(1))\r\n }\r\n\r\n var regExp = prepareRegExpForPrefixSearch(str)\r\n\r\n if (str === '*') {\r\n filterRegExps = [{\r\n type: 'disable',\r\n regExp: regExp\r\n }]\r\n } else {\r\n filterRegExps.push({\r\n type: 'disable',\r\n regExp: regExp\r\n })\r\n }\r\n })\r\n }\r\n\r\n // Public\r\n // ------\r\n\r\n var methods = ['debug', 'log', 'info', 'warn', 'error']\r\n methods.forEach(function (method) {\r\n Logdown.prototype[method] = function () {\r\n if (isDisabled(this)) {\r\n return\r\n }\r\n\r\n var preparedOutput\r\n var args = Array.prototype.slice.call(arguments, 0)\r\n\r\n if (isBrowser()) {\r\n preparedOutput = prepareOutputToBrowser(args, this)\r\n\r\n // IE9 workaround\r\n // http://stackoverflow.com/questions/5538972/\r\n // console-log-apply-not-working-in-ie9\r\n Function.prototype.apply.call(\r\n console[method] || console.log,\r\n console,\r\n preparedOutput\r\n )\r\n } else if (isNode()) {\r\n preparedOutput = prepareOutputToNode(args, method, this)\r\n\r\n ;(console[method] || console.log).apply(\r\n console,\r\n preparedOutput\r\n )\r\n }\r\n }\r\n })\r\n\r\n // Private\r\n // -------\r\n\r\n function alignPrefixes (instances) {\r\n var longest = instances.sort(function (a, b) {\r\n return b.prefix.length - a.prefix.length\r\n })[0]\r\n\r\n instances.forEach(function (instance) {\r\n if (instance.alignOutput) {\r\n var padding = new Array(Math.max(longest.prefix.length - instance.prefix.length + 1, 0)).join(' ')\r\n instance.prefix = instance.prefix + padding\r\n }\r\n })\r\n }\r\n\r\n function parseMarkdown (text) {\r\n var styles = []\r\n var match = getNextMatch(text)\r\n\r\n while (match) {\r\n text = text.replace(match.rule.regexp, match.rule.replacer)\r\n\r\n if (isBrowser()) {\r\n styles.push(match.rule.style)\r\n styles.push('') // Empty string resets style.\r\n }\r\n\r\n match = getNextMatch(text)\r\n }\r\n\r\n return {text: text, styles: styles}\r\n }\r\n\r\n function getNextMatch (text) {\r\n var matches = []\r\n var rules = []\r\n if (isBrowser()) {\r\n rules = [\r\n {\r\n regexp: /\\*([^\\*]+)\\*/,\r\n replacer: function (match, submatch1) {\r\n return '%c' + submatch1 + '%c'\r\n },\r\n style: 'font-weight:bold;'\r\n },\r\n {\r\n regexp: /_([^_]+)_/,\r\n replacer: function (match, submatch1) {\r\n return '%c' + submatch1 + '%c'\r\n },\r\n style: 'font-style:italic;'\r\n },\r\n {\r\n regexp: /`([^`]+)`/,\r\n replacer: function (match, submatch1) {\r\n return '%c' + submatch1 + '%c'\r\n },\r\n style:\r\n 'background:#FDF6E3; ' +\r\n 'color:#586E75; ' +\r\n 'padding:1px 5px; ' +\r\n 'border-radius:4px;'\r\n }\r\n ]\r\n } else if (isNode()) {\r\n rules = [\r\n {\r\n regexp: /\\*([^\\*]+)\\*/,\r\n replacer: function (match, submatch1) {\r\n return '\\u001b[' + ansiColors.modifiers.bold[0] + 'm' +\r\n submatch1 +\r\n '\\u001b[' + ansiColors.modifiers.bold[1] + 'm'\r\n }\r\n },\r\n {\r\n regexp: /_([^_]+)_/,\r\n replacer: function (match, submatch1) {\r\n return '\\u001b[' + ansiColors.modifiers.italic[0] + 'm' +\r\n submatch1 +\r\n '\\u001b[' + ansiColors.modifiers.italic[1] + 'm'\r\n }\r\n },\r\n {\r\n regexp: /`([^`]+)`/,\r\n replacer: function (match, submatch1) {\r\n return '\\u001b[' + ansiColors.bgColors.bgYellow[0] + 'm' +\r\n '\\u001b[' + ansiColors.colors.black[0] + 'm' +\r\n ' ' + submatch1 + ' ' +\r\n '\\u001b[' + ansiColors.colors.black[1] + 'm' +\r\n '\\u001b[' + ansiColors.bgColors.bgYellow[1] + 'm'\r\n }\r\n }\r\n ]\r\n }\r\n\r\n //\r\n rules.forEach(function (rule) {\r\n var match = text.match(rule.regexp)\r\n if (match) {\r\n matches.push({\r\n rule: rule,\r\n match: match\r\n })\r\n }\r\n })\r\n if (matches.length === 0) {\r\n return null\r\n }\r\n\r\n //\r\n matches.sort(function (a, b) {\r\n return a.match.index - b.match.index\r\n })\r\n\r\n return matches[0]\r\n }\r\n\r\n function prepareOutputToBrowser (args, instance) {\r\n var preparedOutput = []\r\n var parsedMarkdown\r\n\r\n if (instance.prefix) {\r\n if (isColorSupported()) {\r\n preparedOutput.push('%c' + instance.prefix + '%c ')\r\n preparedOutput.push(\r\n 'color:' + instance.prefixColor + '; font-weight:bold;',\r\n '' // Empty string resets style.\r\n )\r\n } else {\r\n preparedOutput.push('[' + instance.prefix + '] ')\r\n }\r\n } else {\r\n preparedOutput.push('')\r\n }\r\n\r\n // Only first argument on `console` can have style.\r\n if (typeof args[0] === 'string') {\r\n if (instance.markdown && isColorSupported()) {\r\n parsedMarkdown = parseMarkdown(args[0])\r\n preparedOutput[0] = preparedOutput[0] + parsedMarkdown.text\r\n preparedOutput = preparedOutput.concat(parsedMarkdown.styles)\r\n } else {\r\n preparedOutput[0] = preparedOutput[0] + args[0]\r\n }\r\n } else {\r\n preparedOutput[0] = args[0]\r\n }\r\n\r\n if (args.length > 1) {\r\n preparedOutput = preparedOutput.concat(args.splice(1))\r\n }\r\n\r\n return preparedOutput\r\n }\r\n\r\n function prepareOutputToNode (args, method, instance) {\r\n var preparedOutput = []\r\n\r\n if (instance.prefix) {\r\n if (isColorSupported()) {\r\n preparedOutput[0] =\r\n '\\u001b[' + instance.prefixColor[0] + 'm' +\r\n '\\u001b[' + ansiColors.modifiers.bold[0] + 'm' +\r\n instance.prefix +\r\n '\\u001b[' + ansiColors.modifiers.bold[1] + 'm' +\r\n '\\u001b[' + instance.prefixColor[1] + 'm'\r\n } else {\r\n preparedOutput[0] = '[' + instance.prefix + ']'\r\n }\r\n }\r\n\r\n if (method === 'warn') {\r\n preparedOutput[0] =\r\n '\\u001b[' + ansiColors.colors.yellow[0] + 'm' +\r\n '⚠' +\r\n '\\u001b[' + ansiColors.colors.yellow[1] + 'm ' +\r\n (preparedOutput[0] || '')\r\n } else if (method === 'error') {\r\n preparedOutput[0] =\r\n '\\u001b[' + ansiColors.colors.red[0] + 'm' +\r\n '✖' +\r\n '\\u001b[' + ansiColors.colors.red[1] + 'm ' +\r\n (preparedOutput[0] || '')\r\n } else if (method === 'info') {\r\n preparedOutput[0] =\r\n '\\u001b[' + ansiColors.colors.blue[0] + 'm' +\r\n 'ℹ' +\r\n '\\u001b[' + ansiColors.colors.blue[1] + 'm ' +\r\n (preparedOutput[0] || '')\r\n } else if (method === 'debug') {\r\n preparedOutput[0] =\r\n '\\u001b[' + ansiColors.colors.gray[0] + 'm' +\r\n '🐛' +\r\n '\\u001b[' + ansiColors.colors.gray[1] + 'm ' +\r\n (preparedOutput[0] || '')\r\n }\r\n\r\n args.forEach(function (arg) {\r\n if (typeof arg === 'string') {\r\n if (instance.markdown) {\r\n preparedOutput.push(parseMarkdown(arg).text)\r\n } else {\r\n preparedOutput.push(arg)\r\n }\r\n } else {\r\n preparedOutput.push(arg)\r\n }\r\n })\r\n\r\n return preparedOutput\r\n }\r\n\r\n function isDisabled (instance) {\r\n // Parsing `NODE_DEBUG` and `DEBUG` env var.\r\n // We verify `NODE_DEBUG` and `DEBUG` env vars on runtime so it is\r\n // easier to test.\r\n var envVar = null\r\n if (typeof process !== 'undefined' &&\r\n process.env !== undefined &&\r\n filterRegExps.length === 0) {\r\n // `NODE_DEBUG` has precedence over `DEBUG`\r\n if (process.env.NODE_DEBUG !== undefined &&\r\n process.env.NODE_DEBUG !== '') {\r\n envVar = 'NODE_DEBUG'\r\n } else if (process.env.DEBUG !== undefined &&\r\n process.env.DEBUG !== '') {\r\n envVar = 'DEBUG'\r\n }\r\n\r\n if (envVar) {\r\n Logdown.disable('*')\r\n process.env[envVar]\r\n .split(',')\r\n .forEach(function (regExp) {\r\n Logdown.enable(regExp)\r\n })\r\n }\r\n }\r\n\r\n // Now checks if instance is disabled\r\n var isDisabled_ = false\r\n filterRegExps.forEach(function (filter) {\r\n if (filter.type === 'enable' && filter.regExp.test(instance.prefix)) {\r\n isDisabled_ = false\r\n } else if (filter.type === 'disable' &&\r\n filter.regExp.test(instance.prefix)) {\r\n isDisabled_ = true\r\n }\r\n })\r\n\r\n return isDisabled_\r\n }\r\n\r\n function prepareRegExpForPrefixSearch (str) {\r\n return new RegExp('^' + str.replace(/\\*/g, '.*?') + '$')\r\n }\r\n\r\n function isPrefixAlreadyInUse (prefix, instances) {\r\n var isPrefixAlreadyInUse_ = false\r\n\r\n instances.forEach(function (instance) {\r\n if (instance.prefix === prefix) {\r\n isPrefixAlreadyInUse_ = true\r\n return\r\n }\r\n })\r\n\r\n return isPrefixAlreadyInUse_\r\n }\r\n\r\n function getInstanceByPrefix (prefix, instances) {\r\n var instance\r\n\r\n instances.forEach(function (instanceCur) {\r\n if (instanceCur.prefix === prefix) {\r\n instance = instanceCur\r\n return\r\n }\r\n })\r\n\r\n return instance\r\n }\r\n\r\n function sanitizeStringToBrowser (str) {\r\n if (typeof str === 'string') {\r\n return str.replace(/%c/g, '')\r\n } else {\r\n return str\r\n }\r\n }\r\n\r\n /**\r\n * Currently only WebKit-based Web Inspectors, Firefox >= v31,\r\n * and the Firebug extension (any Firefox version) are known\r\n * to support \"%c\" CSS customizations.\r\n *\r\n * Code took from https://github.com/visionmedia/debug/blob/master/browser.js\r\n */\r\n function isColorSupported () {\r\n if (isBrowser()) {\r\n // Is webkit? http://stackoverflow.com/a/16459606/376773\r\n var isWebkit = ('WebkitAppearance' in document.documentElement.style)\r\n // Is firebug? http://stackoverflow.com/a/398120/376773\r\n var isFirebug = (\r\n window.console &&\r\n (console.firebug || (console.exception && console.table))\r\n )\r\n // Is firefox >= v31?\r\n // https://developer.mozilla.org/en-US/docs/Tools/\r\n // Web_Console#Styling_messages\r\n var isFirefox31Plus = (\r\n navigator.userAgent.toLowerCase().match(/firefox\\/(\\d+)/) &&\r\n parseInt(RegExp.$1, 10) >= 31\r\n )\r\n\r\n return (isWebkit || isFirebug || isFirefox31Plus)\r\n } else if (isNode()) {\r\n if (process.stdout && !process.stdout.isTTY) {\r\n return false\r\n }\r\n\r\n if (process.platform === 'win32') {\r\n return true\r\n }\r\n\r\n if ('COLORTERM' in process.env) {\r\n return true\r\n }\r\n\r\n if (process.env.TERM === 'dumb') {\r\n return false\r\n }\r\n\r\n if (\r\n /^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)\r\n ) {\r\n return true\r\n }\r\n\r\n return false\r\n }\r\n }\r\n\r\n function isNode () {\r\n return (\r\n typeof module !== 'undefined' &&\r\n typeof module.exports !== 'undefined'\r\n )\r\n }\r\n\r\n function isBrowser () {\r\n return (typeof window !== 'undefined')\r\n }\r\n\r\n var getNextPrefixColor = (function () {\r\n var lastUsed = 0\r\n var nodePrefixColors = [\r\n [31, 39], // red\r\n [32, 39], // green\r\n [33, 39], // yellow\r\n [34, 39], // blue\r\n [35, 39], // magenta\r\n [36, 39] // cyan\r\n ]\r\n\r\n return function () {\r\n return nodePrefixColors[(lastUsed += 1) % nodePrefixColors.length]\r\n }\r\n })()\r\n\r\n // Export module\r\n if (isNode()) {\r\n module.exports = Logdown\r\n } else if (isBrowser()) {\r\n window.Logdown = Logdown\r\n }\r\n}())\r\n"]} \ No newline at end of file diff --git a/example/lib/logdown.js b/example/lib/logdown.js index 9043dca..13021b1 100644 --- a/example/lib/logdown.js +++ b/example/lib/logdown.js @@ -1,7 +1,7 @@ /** * logdown - Debug utility with markdown support that runs on browser and server * - * @version v2.0.2 + * @version v2.0.3 * @link https://github.com/caiogondim/logdown * @author Caio Gondim (http://caiogondim.com) * @license MIT diff --git a/package.json b/package.json index 3d7804e..83b1e20 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logdown", - "version": "2.0.2", + "version": "2.0.3", "description": "Debug utility with markdown support that runs on browser and server", "main": "dist/logdown.js", "types": "dist/logdown.d.ts",