UNPKG

@financial-times/o-ads

Version:

This package contains the core functionality used by the FT in providing ads across all of its sites. This includes ft.com, howtospendit.com, ftadviser.com and other specialist titles.

158 lines (123 loc) 3.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = log; exports.attributeTable = exports.table = exports.end = exports.start = exports.info = exports.error = exports.warn = exports.isOn = void 0; var _index = require("./index.js"); /* eslint no-console: 0 */ /** * Utility methods for logging. * @author Origami Advertising, origami.advertising@ft.com * @module utils/log * @see utils */ /* jshint devel: true */ /** * Safe logger for the browser * @exports utils/log * @param {string} type Sets the type of log message log, warn, error or info, if not set to one of these values log will be used * @param {any} args the arguments to be passed to console[type] */ function log() { var type; var argsIndex; if ('log warn error info'.indexOf(arguments[0]) === -1) { type = 'log'; argsIndex = 0; } else { type = arguments[0]; argsIndex = 1; } var args = [].slice.call(arguments, argsIndex); if (log.isOn(type)) { window.console[type].apply(window.console, args); } } /** * Determine if debug logging is on and if the type if supported * @param {string} type */ var isOn = function isOn(type) { /* istanbul ignore else */ var debug = localStorage && localStorage.getItem('oAds') || location.search.indexOf('DEBUG=OADS') !== -1; return debug && window.console && window.console[type]; }; /** * Log a warning message */ exports.isOn = isOn; var warn = function warn() { var args = ['warn'].concat([].slice.call(arguments, 0)); log.apply(null, args); }; /** * Log an error message */ exports.warn = warn; var error = function error() { var args = ['error'].concat([].slice.call(arguments, 0)); log.apply(null, args); }; /** * Log an info message */ exports.error = error; var info = function info() { var args = ['info'].concat([].slice.call(arguments, 0)); log.apply(null, args); }; /** * Start a collapsed group * @param {string} group the name of the group, defaults to o-ads */ exports.info = info; var start = function start(group) { if (!log.isOn('groupCollapsed')) { return; } window.console.groupCollapsed(group || 'o-ads'); }; /** * End a collapsed group */ exports.start = start; var end = function end() { if (!log.isOn('groupEnd')) { return; } window.console.groupEnd(); }; exports.end = end; var table = function table(data, columns) { if (log.isOn('log') && window.console) { if (console.table) { console.table(data, columns); } else { console.log(data); } } }; exports.table = table; var attributeTable = function attributeTable(object, columns) { if (log.isOn('log') && window.console) { if (object && console.table) { var data = Object.keys(object).map(item => { var val; if ((0, _index.isArray)(object[item]) || (0, _index.isObject)(object[item])) { val = JSON.stringify(object[item]); } else { val = object[item]; } return { key: item, value: val }; }); console.table(data, columns); } else { console.log(object); } } }; exports.attributeTable = attributeTable;