Comparing version 1.0.0-alpha.11 to 1.0.0-alpha.12
CHANGELOG | ||
v1.0.0-alpha.12 | ||
- Improve performance when probe is disabled | ||
v1.0.0-alpha.11 | ||
@@ -4,0 +7,0 @@ - `spy.returns` method added |
@@ -1,2 +0,2 @@ | ||
'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&'function'==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?'symbol':typeof obj},_createClass=function(){function defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,'value'in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}(),_globals=require('./utils/globals'),_localStorage=require('./utils/local-storage'),_localStorage2=_interopRequireDefault(_localStorage),_timestamp=require('./utils/timestamp'),_formatters=require('./utils/formatters'),_color=require('./utils/color'),_autobind=require('./utils/autobind'),_assert=require('../lib/utils/assert'),_assert2=_interopRequireDefault(_assert);Object.defineProperty(exports,'__esModule',{value:!0});function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}return Array.from(arr)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError('Cannot call a class as a function')}var originalConsole={debug:_globals.isBrowser?console.debug||console.log:console.log,log:console.log,info:console.info,warn:console.warn,error:console.error},DEFAULT_SETTINGS={enabled:!1,priority:0};function noop(){}var cache={};function getTableHeader(table){for(var key in table)for(var title in table[key])return title||'untitled';return'empty'}var Log=function(){function Log(){var _ref=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},id=_ref.id;_classCallCheck(this,Log),this.id=id,this.VERSION=_globals.VERSION,this._startTs=(0,_timestamp.getTimestamp)(),this._deltaTs=(0,_timestamp.getTimestamp)(),this.LOG_THROTTLE_TIMEOUT=0,this._storage=new _localStorage2.default('__probe-'+this.id+'__',DEFAULT_SETTINGS),this.userData={},this.timeStamp(this.id+' started'),(0,_autobind.autobind)(this),Object.seal(this)}var _NumberisFinite=Number.isFinite;return _createClass(Log,[{key:'isEnabled',value:function isEnabled(){return this._storage.config.enabled}},{key:'getPriority',value:function getPriority(){return this._storage.config.priority}},{key:'getLevel',value:function getLevel(){return this._storage.config.priority}},{key:'getTotal',value:function getTotal(){return+((0,_timestamp.getTimestamp)()-this._startTs).toPrecision(10)}},{key:'getDelta',value:function getDelta(){return+((0,_timestamp.getTimestamp)()-this._deltaTs).toPrecision(10)}},{key:'enable',value:function enable(){var enabled=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];return this._storage.updateConfiguration({enabled:enabled}),this}},{key:'setLevel',value:function setLevel(level){return this._storage.updateConfiguration({priority:level}),this}},{key:'warn',value:function warn(message){for(var _len=arguments.length,args=Array(1<_len?_len-1:0),_key=1;_key<_len;_key++)args[_key-1]=arguments[_key];return this._getLogFunction({message:message,args:args,method:originalConsole.warn,once:!0})}},{key:'error',value:function error(message){for(var _len2=arguments.length,args=Array(1<_len2?_len2-1:0),_key2=1;_key2<_len2;_key2++)args[_key2-1]=arguments[_key2];return this._getLogFunction({message:message,args:args,method:originalConsole.error})}},{key:'deprecated',value:function deprecated(oldUsage,newUsage){return this.warn('`'+oldUsage+'` is deprecated and will be removed in a later version. Use `'+newUsage+'` instead')}},{key:'removed',value:function removed(oldUsage,newUsage){return this.error('`'+oldUsage+'` has been removed. Use `'+newUsage+'` instead')}},{key:'probe',value:function probe(priority,message){for(var _len3=arguments.length,args=Array(2<_len3?_len3-2:0),_key3=2;_key3<_len3;_key3++)args[_key3-2]=arguments[_key3];return this._getLogFunction({priority:priority,message:message,args:args,method:originalConsole.log,time:!0})}},{key:'log',value:function log(priority,message){for(var _len4=arguments.length,args=Array(2<_len4?_len4-2:0),_key4=2;_key4<_len4;_key4++)args[_key4-2]=arguments[_key4];return this._getLogFunction({priority:priority,message:message,args:args,method:originalConsole.debug})}},{key:'info',value:function info(priority,message){for(var _len5=arguments.length,args=Array(2<_len5?_len5-2:0),_key5=2;_key5<_len5;_key5++)args[_key5-2]=arguments[_key5];return this._getLogFunction({priority:priority,message:message,args:args,method:console.info})}},{key:'once',value:function once(priority,message){for(var _len6=arguments.length,args=Array(2<_len6?_len6-2:0),_key6=2;_key6<_len6;_key6++)args[_key6-2]=arguments[_key6];return this._getLogFunction({priority:priority,message:message,args:args,method:originalConsole.debug||originalConsole.info,once:!0})}},{key:'table',value:function table(priority,_table,columns){if(_table){var tag=getTableHeader(_table);return this._getLogFunction({priority:priority,message:_table,args:columns&&[columns],tag:tag,method:console.table||noop})}return noop}},{key:'image',value:function image(_ref2){var priority=_ref2.priority,_image=_ref2.image,_ref2$message=_ref2.message,message=void 0===_ref2$message?'':_ref2$message,_ref2$scale=_ref2.scale,scale=void 0===_ref2$scale?1:_ref2$scale;if(priority>this.getPriority())return noop;if('undefined'==typeof window)return noop;if('string'==typeof _image){var img=new Image;img.onload=function(){return console.log(_formatters.formatImage.bind(null,img,message,scale))},img.src=_image}var element=_image.nodeName||'';if('img'===element.toLowerCase()&&console.log((0,_formatters.formatImage)(_image,message,scale)),'canvas'===element.toLowerCase()){var _img=new Image;_img.onload=function(){return console.log(_formatters.formatImage.bind(null,_img,message,scale))},_img.src=_image.toDataURL()}return noop}},{key:'time',value:function time(priority,message){return this._getLogFunction({priority:priority,message:message,method:console.time?console.time:console.info})}},{key:'timeEnd',value:function timeEnd(priority,message){return this._getLogFunction({priority:priority,message:message,method:console.timeEnd?console.timeEnd:console.info})}},{key:'timeStamp',value:function timeStamp(priority,message){return this._getLogFunction({priority:priority,message:message,method:console.timeStamp||noop})}},{key:'group',value:function group(priority,message){var opts=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{collapsed:!1};opts=this._parseArguments({priority:priority,message:message,opts:opts});var _opts=opts,collapsed=_opts.collapsed;return this._getLogFunction({priority:priority,message:message,opts:opts,method:(collapsed?console.groupCollapsed:console.group)||console.info})}},{key:'groupCollapsed',value:function groupCollapsed(priority,message){var opts=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return this.group(priority,message,Object.assign({},opts,{collapsed:!0}))}},{key:'groupEnd',value:function groupEnd(priority){return this._getLogFunction({priority:priority,message:'',method:console.groupEnd||noop})}},{key:'withGroup',value:function withGroup(priority,message,func){var opts=this._parseArguments({priority:priority,message:message});this.group(opts);try{func()}finally{this.groupEnd(opts.message)}}},{key:'trace',value:function trace(){console.trace&&console.trace()}},{key:'_shouldLog',value:function _shouldLog(priority){return(0,_assert2.default)(_NumberisFinite(priority),'log priority must be a number'),0===priority||this.isEnabled()&&this.getPriority()>=priority}},{key:'_getElapsedTime',value:function _getElapsedTime(){var total=this.getTotal(),delta=this.getDelta();return this._deltaTs=(0,_timestamp.getTimestamp)(),{total:total,delta:delta}}},{key:'_getLogFunction',value:function _getLogFunction(opts){var _opts2=opts,method=_opts2.method;if(opts=this._parseArguments(opts),(0,_assert2.default)(method),this._shouldLog(opts.priority)){var _opts3=opts,message=_opts3.message,tag=opts.tag||opts.message;if(opts.once)if(!cache[tag])cache[tag]=(0,_timestamp.getTimestamp)();else return noop;return message=this._decorateMessage(message,opts),method.bind.apply(method,[console,message].concat(_toConsumableArray(opts.args)))}return noop}},{key:'_parseArguments',value:function _parseArguments(options){var priority=options.priority,message=options.message,_options$args=options.args,args=void 0===_options$args?[]:_options$args,_options$opts=options.opts,opts=void 0===_options$opts?{}:_options$opts,normOpts=this._normalizeArguments({priority:priority,message:message,args:args}),_getElapsedTime2=this._getElapsedTime(),delta=_getElapsedTime2.delta,total=_getElapsedTime2.total;return Object.assign(options,normOpts,opts,{delta:delta,total:total})}},{key:'_normalizeArguments',value:function _normalizeArguments(_ref3){var priority=_ref3.priority,message=_ref3.message,_ref3$args=_ref3.args,args=void 0===_ref3$args?[]:_ref3$args,newOpts=null;switch('undefined'==typeof priority?'undefined':_typeof(priority)){case'number':(0,_assert2.default)(0<=priority),newOpts={priority:priority,message:message,args:args};break;case'string':case'function':void 0!==message&&args.unshift(message),newOpts={priority:0,message:priority,args:args};break;case'object':newOpts=Object.assign({priority:0,message:message,args:args},priority);break;default:newOpts={priority:0,message:message,args:args};}return(0,_assert2.default)(_NumberisFinite(newOpts.priority)),'function'==typeof newOpts.message&&(newOpts.message=this._shouldLog(newOpts.priority)?newOpts.message():''),(0,_assert2.default)('string'==typeof newOpts.message||'object'===_typeof(newOpts.message)),newOpts}},{key:'_decorateMessage',value:function _decorateMessage(message,opts){if('string'==typeof message){var time='';if(opts.time){var _getElapsedTime3=this._getElapsedTime(),total=_getElapsedTime3.total;time=(0,_formatters.leftPad)((0,_formatters.formatTime)(total))}message=opts.time?this.id+': '+time+' '+message:this.id+': '+message,message=(0,_color.addColor)(message,opts.color,opts.background)}return message}},{key:'priority',set:function set(newPriority){return this._storage.updateConfiguration({priority:newPriority}),this},get:function get(){return this._storage.config.priority}}]),Log}();exports.default=Log,Log.VERSION=_globals.VERSION; | ||
'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&'function'==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?'symbol':typeof obj},_createClass=function(){function defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,'value'in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}(),_globals=require('./utils/globals'),_localStorage=require('./utils/local-storage'),_localStorage2=_interopRequireDefault(_localStorage),_timestamp=require('./utils/timestamp'),_formatters=require('./utils/formatters'),_color=require('./utils/color'),_autobind=require('./utils/autobind'),_assert=require('../lib/utils/assert'),_assert2=_interopRequireDefault(_assert);Object.defineProperty(exports,'__esModule',{value:!0});function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}return Array.from(arr)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError('Cannot call a class as a function')}var originalConsole={debug:_globals.isBrowser?console.debug||console.log:console.log,log:console.log,info:console.info,warn:console.warn,error:console.error},DEFAULT_SETTINGS={enabled:!1,priority:0};function noop(){}var cache={};function getTableHeader(table){for(var key in table)for(var title in table[key])return title||'untitled';return'empty'}var Log=function(){function Log(){var _ref=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},id=_ref.id;_classCallCheck(this,Log),this.id=id,this.VERSION=_globals.VERSION,this._startTs=(0,_timestamp.getTimestamp)(),this._deltaTs=(0,_timestamp.getTimestamp)(),this.LOG_THROTTLE_TIMEOUT=0,this._storage=new _localStorage2.default('__probe-'+this.id+'__',DEFAULT_SETTINGS),this.userData={},this.timeStamp(this.id+' started'),(0,_autobind.autobind)(this),Object.seal(this)}return _createClass(Log,[{key:'isEnabled',value:function isEnabled(){return this._storage.config.enabled}},{key:'getPriority',value:function getPriority(){return this._storage.config.priority}},{key:'getLevel',value:function getLevel(){return this._storage.config.priority}},{key:'getTotal',value:function getTotal(){return+((0,_timestamp.getTimestamp)()-this._startTs).toPrecision(10)}},{key:'getDelta',value:function getDelta(){return+((0,_timestamp.getTimestamp)()-this._deltaTs).toPrecision(10)}},{key:'enable',value:function enable(){var enabled=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];return this._storage.updateConfiguration({enabled:enabled}),this}},{key:'setLevel',value:function setLevel(level){return this._storage.updateConfiguration({priority:level}),this}},{key:'warn',value:function warn(message){for(var _len=arguments.length,args=Array(1<_len?_len-1:0),_key=1;_key<_len;_key++)args[_key-1]=arguments[_key];return this._getLogFunction({message:message,args:args,method:originalConsole.warn,once:!0})}},{key:'error',value:function error(message){for(var _len2=arguments.length,args=Array(1<_len2?_len2-1:0),_key2=1;_key2<_len2;_key2++)args[_key2-1]=arguments[_key2];return this._getLogFunction({message:message,args:args,method:originalConsole.error})}},{key:'deprecated',value:function deprecated(oldUsage,newUsage){return this.warn('`'+oldUsage+'` is deprecated and will be removed in a later version. Use `'+newUsage+'` instead')}},{key:'removed',value:function removed(oldUsage,newUsage){return this.error('`'+oldUsage+'` has been removed. Use `'+newUsage+'` instead')}},{key:'probe',value:function probe(priority,message){for(var _len3=arguments.length,args=Array(2<_len3?_len3-2:0),_key3=2;_key3<_len3;_key3++)args[_key3-2]=arguments[_key3];return this._getLogFunction({priority:priority,message:message,args:args,method:originalConsole.log,time:!0})}},{key:'log',value:function log(priority,message){for(var _len4=arguments.length,args=Array(2<_len4?_len4-2:0),_key4=2;_key4<_len4;_key4++)args[_key4-2]=arguments[_key4];return this._getLogFunction({priority:priority,message:message,args:args,method:originalConsole.debug})}},{key:'info',value:function info(priority,message){for(var _len5=arguments.length,args=Array(2<_len5?_len5-2:0),_key5=2;_key5<_len5;_key5++)args[_key5-2]=arguments[_key5];return this._getLogFunction({priority:priority,message:message,args:args,method:console.info})}},{key:'once',value:function once(priority,message){for(var _len6=arguments.length,args=Array(2<_len6?_len6-2:0),_key6=2;_key6<_len6;_key6++)args[_key6-2]=arguments[_key6];return this._getLogFunction({priority:priority,message:message,args:args,method:originalConsole.debug||originalConsole.info,once:!0})}},{key:'table',value:function table(priority,_table,columns){if(_table){var tag=getTableHeader(_table);return this._getLogFunction({priority:priority,message:_table,args:columns&&[columns],tag:tag,method:console.table||noop})}return noop}},{key:'image',value:function image(_ref2){var priority=_ref2.priority,_image=_ref2.image,_ref2$message=_ref2.message,message=void 0===_ref2$message?'':_ref2$message,_ref2$scale=_ref2.scale,scale=void 0===_ref2$scale?1:_ref2$scale;if(priority>this.getPriority())return noop;if('undefined'==typeof window)return noop;if('string'==typeof _image){var img=new Image;img.onload=function(){return console.log(_formatters.formatImage.bind(null,img,message,scale))},img.src=_image}var element=_image.nodeName||'';if('img'===element.toLowerCase()&&console.log((0,_formatters.formatImage)(_image,message,scale)),'canvas'===element.toLowerCase()){var _img=new Image;_img.onload=function(){return console.log(_formatters.formatImage.bind(null,_img,message,scale))},_img.src=_image.toDataURL()}return noop}},{key:'time',value:function time(priority,message){return this._getLogFunction({priority:priority,message:message,method:console.time?console.time:console.info})}},{key:'timeEnd',value:function timeEnd(priority,message){return this._getLogFunction({priority:priority,message:message,method:console.timeEnd?console.timeEnd:console.info})}},{key:'timeStamp',value:function timeStamp(priority,message){return this._getLogFunction({priority:priority,message:message,method:console.timeStamp||noop})}},{key:'group',value:function group(priority,message){var opts=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{collapsed:!1};opts=this._normalizeArguments({priority:priority,message:message,opts:opts});var _opts=opts,collapsed=_opts.collapsed;return this._getLogFunction({priority:priority,message:message,opts:opts,method:(collapsed?console.groupCollapsed:console.group)||console.info})}},{key:'groupCollapsed',value:function groupCollapsed(priority,message){var opts=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return this.group(priority,message,Object.assign({},opts,{collapsed:!0}))}},{key:'groupEnd',value:function groupEnd(priority){return this._getLogFunction({priority:priority,message:'',method:console.groupEnd||noop})}},{key:'withGroup',value:function withGroup(priority,message,func){var opts=this._normalizeArguments({priority:priority,message:message});this.group(opts);try{func()}finally{this.groupEnd(opts.message)}}},{key:'trace',value:function trace(){console.trace&&console.trace()}},{key:'_shouldLog',value:function _shouldLog(priority){return priority=this._normalizePriority(priority),0===priority||this.isEnabled()&&this.getPriority()>=priority}},{key:'_getElapsedTime',value:function _getElapsedTime(){var total=this.getTotal(),delta=this.getDelta();return this._deltaTs=(0,_timestamp.getTimestamp)(),{total:total,delta:delta}}},{key:'_getLogFunction',value:function _getLogFunction(opts){if(this._shouldLog(opts.priority)){var _opts2=opts,method=_opts2.method;opts=this._parseArguments(opts),(0,_assert2.default)(method);var _opts3=opts,message=_opts3.message,tag=opts.tag||opts.message;if(opts.once)if(!cache[tag])cache[tag]=(0,_timestamp.getTimestamp)();else return noop;return message=this._decorateMessage(message,opts),method.bind.apply(method,[console,message].concat(_toConsumableArray(opts.args)))}return noop}},{key:'_parseArguments',value:function _parseArguments(options){var normOpts=this._normalizeArguments(options),_getElapsedTime2=this._getElapsedTime(),delta=_getElapsedTime2.delta,total=_getElapsedTime2.total;return Object.assign(options,normOpts,{delta:delta,total:total})}},{key:'_normalizePriority',value:function _normalizePriority(priority){var resolvedPriority;switch('undefined'==typeof priority?'undefined':_typeof(priority)){case'number':resolvedPriority=priority;break;case'object':resolvedPriority=priority.priority||0;break;default:resolvedPriority=0;}return(0,_assert2.default)(Number.isFinite(resolvedPriority)&&0<=resolvedPriority),resolvedPriority}},{key:'_normalizeArguments',value:function _normalizeArguments(_ref3){var priority=_ref3.priority,message=_ref3.message,_ref3$args=_ref3.args,args=void 0===_ref3$args?[]:_ref3$args,opts=_ref3.opts,newOpts={priority:this._normalizePriority(priority),message:message,args:args};switch('undefined'==typeof priority?'undefined':_typeof(priority)){case'string':case'function':void 0!==message&&args.unshift(message),Object.assign(newOpts,{message:priority});break;case'object':Object.assign(newOpts,priority);break;default:}return'function'==typeof newOpts.message&&(newOpts.message=this._shouldLog(newOpts.priority)?newOpts.message():''),(0,_assert2.default)('string'==typeof newOpts.message||'object'===_typeof(newOpts.message)),Object.assign(newOpts,opts)}},{key:'_decorateMessage',value:function _decorateMessage(message,opts){if('string'==typeof message){var time='';if(opts.time){var _getElapsedTime3=this._getElapsedTime(),total=_getElapsedTime3.total;time=(0,_formatters.leftPad)((0,_formatters.formatTime)(total))}message=opts.time?this.id+': '+time+' '+message:this.id+': '+message,message=(0,_color.addColor)(message,opts.color,opts.background)}return message}},{key:'priority',set:function set(newPriority){return this._storage.updateConfiguration({priority:newPriority}),this},get:function get(){return this._storage.config.priority}}]),Log}();exports.default=Log,Log.VERSION=_globals.VERSION; | ||
//# sourceMappingURL=log.js.map |
@@ -1,2 +0,2 @@ | ||
'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&'function'==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?'symbol':typeof obj},isBrowser='object'!==('undefined'==typeof process?'undefined':_typeof(process))||'[object process]'!==process+''||process.browser,window_='undefined'==typeof window?global:window,document_='undefined'==typeof document?{}:document,global_='undefined'==typeof global?window:global,process_='object'===('undefined'==typeof process?'undefined':_typeof(process))?process:{},VERSION='1.0.0-alpha.11';Object.defineProperty(exports,'__esModule',{value:!0});exports.window=window_,exports.document=document_,exports.global=global_,exports.process=process_,exports.console=console,exports.isBrowser=isBrowser,exports.VERSION=VERSION; | ||
'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&'function'==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?'symbol':typeof obj},isBrowser='object'!==('undefined'==typeof process?'undefined':_typeof(process))||'[object process]'!==process+''||process.browser,window_='undefined'==typeof window?global:window,document_='undefined'==typeof document?{}:document,global_='undefined'==typeof global?window:global,process_='object'===('undefined'==typeof process?'undefined':_typeof(process))?process:{},VERSION='1.0.0-alpha.12';Object.defineProperty(exports,'__esModule',{value:!0});exports.window=window_,exports.document=document_,exports.global=global_,exports.process=process_,exports.console=console,exports.isBrowser=isBrowser,exports.VERSION=VERSION; | ||
//# sourceMappingURL=globals.js.map |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.0-alpha.11", | ||
"version": "1.0.0-alpha.12", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "javascript", |
@@ -286,3 +286,3 @@ // Copyright (c) 2017 Uber Technologies, Inc. | ||
group(priority, message, opts = {collapsed: false}) { | ||
opts = this._parseArguments({priority, message, opts}); | ||
opts = this._normalizeArguments({priority, message, opts}); | ||
const {collapsed} = opts; | ||
@@ -312,3 +312,3 @@ return this._getLogFunction({ | ||
withGroup(priority, message, func) { | ||
const opts = this._parseArguments({ | ||
const opts = this._normalizeArguments({ | ||
priority, | ||
@@ -336,3 +336,3 @@ message | ||
_shouldLog(priority) { | ||
assert(Number.isFinite(priority), 'log priority must be a number'); | ||
priority = this._normalizePriority(priority); | ||
return priority === 0 || (this.isEnabled() && this.getPriority() >= priority); | ||
@@ -350,9 +350,9 @@ } | ||
_getLogFunction(opts) { | ||
const {method} = opts; | ||
if (this._shouldLog(opts.priority)) { | ||
const {method} = opts; | ||
opts = this._parseArguments(opts); | ||
opts = this._parseArguments(opts); | ||
assert(method); | ||
assert(method); | ||
if (this._shouldLog(opts.priority)) { | ||
let {message} = opts; | ||
@@ -383,15 +383,9 @@ const tag = opts.tag || opts.message; | ||
// "Normalizes" the various argument patterns into an object with known types | ||
// - log(priority, message, args) => {priority, message, args} | ||
// - log(message, args) => {priority: 0, message, args} | ||
// - log({priority, ...}, message, args) => {priority, message, args} | ||
// - log({priority, message, args}) => {priority, message, args} | ||
_parseArguments(options) { | ||
const {priority, message, args = [], opts = {}} = options; | ||
const normOpts = this._normalizeArguments({priority, message, args}); | ||
const normOpts = this._normalizeArguments(options); | ||
const {delta, total} = this._getElapsedTime(); | ||
// original opts + normalized opts + opts arg + fixed up message + timings | ||
return Object.assign(options, normOpts, opts, { | ||
// normalized opts + timings | ||
return Object.assign(options, normOpts, { | ||
delta, | ||
@@ -402,12 +396,41 @@ total | ||
// helper for _parseArguments | ||
_normalizeArguments({priority, message, args = []}) { | ||
let newOpts = null; | ||
// Get priority from first argument: | ||
// - log(priority, message, args) => priority | ||
// - log(message, args) => 0 | ||
// - log({priority, ...}, message, args) => priority | ||
// - log({priority, message, args}) => priority | ||
_normalizePriority(priority) { | ||
let resolvedPriority; | ||
switch (typeof priority) { | ||
case 'number': | ||
assert(priority >= 0); | ||
newOpts = {priority, message, args}; | ||
resolvedPriority = priority; | ||
break; | ||
case 'object': | ||
resolvedPriority = priority.priority || 0; | ||
break; | ||
default: | ||
resolvedPriority = 0; | ||
} | ||
// 'log priority must be a number' | ||
assert(Number.isFinite(resolvedPriority) && resolvedPriority >= 0); | ||
return resolvedPriority; | ||
} | ||
// "Normalizes" the various argument patterns into an object with known types | ||
// - log(priority, message, args) => {priority, message, args} | ||
// - log(message, args) => {priority: 0, message, args} | ||
// - log({priority, ...}, message, args) => {priority, message, args} | ||
// - log({priority, message, args}) => {priority, message, args} | ||
_normalizeArguments({priority, message, args = [], opts}) { | ||
const newOpts = { | ||
priority: this._normalizePriority(priority), | ||
message, | ||
args | ||
}; | ||
switch (typeof priority) { | ||
case 'string': | ||
@@ -418,17 +441,12 @@ case 'function': | ||
} | ||
newOpts = {priority: 0, message: priority, args}; | ||
Object.assign(newOpts, {message: priority}); | ||
break; | ||
case 'object': | ||
const opts = priority; | ||
newOpts = Object.assign({priority: 0, message, args}, opts); | ||
Object.assign(newOpts, priority); | ||
break; | ||
default: | ||
newOpts = {priority: 0, message, args}; | ||
break; | ||
} | ||
assert(Number.isFinite(newOpts.priority)); // 'log priority must be a number' | ||
// Resolve functions into strings by calling them | ||
@@ -438,7 +456,7 @@ if (typeof newOpts.message === 'function') { | ||
} | ||
// 'log message must be a string' or object | ||
assert(typeof newOpts.message === 'string' || typeof newOpts.message === 'object'); | ||
return newOpts; | ||
// original opts + normalized opts + opts arg + fixed up message | ||
return Object.assign(newOpts, opts); | ||
} | ||
@@ -445,0 +463,0 @@ |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
215750
1877
1