@probe.gl/log
Advanced tools
Comparing version 4.0.6 to 4.0.7
@@ -1,12 +0,12 @@ | ||
import { Log } from './log'; | ||
import { Log } from "./log.js"; | ||
declare const _default: Log; | ||
export default _default; | ||
export { Log } from './log'; | ||
export { COLOR } from './utils/color'; | ||
export { addColor } from './utils/color'; | ||
export { leftPad, rightPad } from './utils/formatters'; | ||
export { autobind } from './utils/autobind'; | ||
export { LocalStorage } from './utils/local-storage'; | ||
export { getHiResTimestamp } from './utils/hi-res-timestamp'; | ||
import './init'; | ||
export { Log } from "./log.js"; | ||
export { COLOR } from "./utils/color.js"; | ||
export { addColor } from "./utils/color.js"; | ||
export { leftPad, rightPad } from "./utils/formatters.js"; | ||
export { autobind } from "./utils/autobind.js"; | ||
export { LocalStorage } from "./utils/local-storage.js"; | ||
export { getHiResTimestamp } from "./utils/hi-res-timestamp.js"; | ||
import "./init.js"; | ||
//# sourceMappingURL=index.d.ts.map |
import { Log } from "./log.js"; | ||
export default new Log({ | ||
id: '@probe.gl/log' | ||
}); | ||
// DEFAULT EXPORT IS A LOG INSTANCE | ||
export default new Log({ id: '@probe.gl/log' }); | ||
// LOGGING | ||
export { Log } from "./log.js"; | ||
export { COLOR } from "./utils/color.js"; | ||
// UTILITIES | ||
export { addColor } from "./utils/color.js"; | ||
@@ -13,2 +14,1 @@ export { leftPad, rightPad } from "./utils/formatters.js"; | ||
import "./init.js"; | ||
//# sourceMappingURL=index.js.map |
@@ -0,2 +1,3 @@ | ||
// @ts-nocheck | ||
/* eslint-disable */ | ||
globalThis.probe = {}; | ||
//# sourceMappingURL=init.js.map |
@@ -1,2 +0,2 @@ | ||
import { LocalStorage } from './utils/local-storage'; | ||
import { LocalStorage } from "./utils/local-storage.js"; | ||
/** "Global" log configuration settings */ | ||
@@ -3,0 +3,0 @@ type LogConfiguration = { |
683
dist/log.js
@@ -0,1 +1,3 @@ | ||
// probe.gl, MIT license | ||
/* eslint-disable no-console */ | ||
import { VERSION, isBrowser } from '@probe.gl/env'; | ||
@@ -8,409 +10,330 @@ import { LocalStorage } from "./utils/local-storage.js"; | ||
import { getHiResTimestamp } from "./utils/hi-res-timestamp.js"; | ||
// Instrumentation in other packages may override console methods, so preserve them here | ||
const originalConsole = { | ||
debug: isBrowser() ? console.debug || console.log : console.log, | ||
log: console.log, | ||
info: console.info, | ||
warn: console.warn, | ||
error: console.error | ||
debug: isBrowser() ? console.debug || console.log : console.log, | ||
log: console.log, | ||
info: console.info, | ||
warn: console.warn, | ||
error: console.error | ||
}; | ||
const DEFAULT_LOG_CONFIGURATION = { | ||
enabled: true, | ||
level: 0 | ||
enabled: true, | ||
level: 0 | ||
}; | ||
function noop() {} | ||
function noop() { } // eslint-disable-line @typescript-eslint/no-empty-function | ||
const cache = {}; | ||
const ONCE = { | ||
once: true | ||
}; | ||
const ONCE = { once: true }; | ||
/** A console wrapper */ | ||
export class Log { | ||
constructor() { | ||
let { | ||
id | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
id: '' | ||
}; | ||
this.id = void 0; | ||
this.VERSION = VERSION; | ||
this._startTs = getHiResTimestamp(); | ||
this._deltaTs = getHiResTimestamp(); | ||
this._storage = void 0; | ||
this.userData = {}; | ||
this.LOG_THROTTLE_TIMEOUT = 0; | ||
this.id = id; | ||
this.userData = {}; | ||
this._storage = new LocalStorage(`__probe-${this.id}__`, DEFAULT_LOG_CONFIGURATION); | ||
this.timeStamp(`${this.id} started`); | ||
autobind(this); | ||
Object.seal(this); | ||
} | ||
set level(newLevel) { | ||
this.setLevel(newLevel); | ||
} | ||
get level() { | ||
return this.getLevel(); | ||
} | ||
isEnabled() { | ||
return this._storage.config.enabled; | ||
} | ||
getLevel() { | ||
return this._storage.config.level; | ||
} | ||
getTotal() { | ||
return Number((getHiResTimestamp() - this._startTs).toPrecision(10)); | ||
} | ||
getDelta() { | ||
return Number((getHiResTimestamp() - this._deltaTs).toPrecision(10)); | ||
} | ||
set priority(newPriority) { | ||
this.level = newPriority; | ||
} | ||
get priority() { | ||
return this.level; | ||
} | ||
getPriority() { | ||
return this.level; | ||
} | ||
enable() { | ||
let enabled = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; | ||
this._storage.setConfiguration({ | ||
enabled | ||
}); | ||
return this; | ||
} | ||
setLevel(level) { | ||
this._storage.setConfiguration({ | ||
level | ||
}); | ||
return this; | ||
} | ||
get(setting) { | ||
return this._storage.config[setting]; | ||
} | ||
set(setting, value) { | ||
this._storage.setConfiguration({ | ||
[setting]: value | ||
}); | ||
} | ||
settings() { | ||
if (console.table) { | ||
console.table(this._storage.config); | ||
} else { | ||
console.log(this._storage.config); | ||
constructor({ id } = { id: '' }) { | ||
this.VERSION = VERSION; | ||
this._startTs = getHiResTimestamp(); | ||
this._deltaTs = getHiResTimestamp(); | ||
this.userData = {}; | ||
// TODO - fix support from throttling groups | ||
this.LOG_THROTTLE_TIMEOUT = 0; // Time before throttled messages are logged again | ||
this.id = id; | ||
this.userData = {}; | ||
this._storage = new LocalStorage(`__probe-${this.id}__`, DEFAULT_LOG_CONFIGURATION); | ||
this.timeStamp(`${this.id} started`); | ||
autobind(this); | ||
Object.seal(this); | ||
} | ||
} | ||
assert(condition, message) { | ||
assert(condition, message); | ||
} | ||
warn(message) { | ||
return this._getLogFunction(0, message, originalConsole.warn, arguments, ONCE); | ||
} | ||
error(message) { | ||
return this._getLogFunction(0, message, originalConsole.error, arguments); | ||
} | ||
deprecated(oldUsage, newUsage) { | ||
return this.warn(`\`${oldUsage}\` is deprecated and will be removed \ | ||
set level(newLevel) { | ||
this.setLevel(newLevel); | ||
} | ||
get level() { | ||
return this.getLevel(); | ||
} | ||
isEnabled() { | ||
return this._storage.config.enabled; | ||
} | ||
getLevel() { | ||
return this._storage.config.level; | ||
} | ||
/** @return milliseconds, with fractions */ | ||
getTotal() { | ||
return Number((getHiResTimestamp() - this._startTs).toPrecision(10)); | ||
} | ||
/** @return milliseconds, with fractions */ | ||
getDelta() { | ||
return Number((getHiResTimestamp() - this._deltaTs).toPrecision(10)); | ||
} | ||
/** @deprecated use logLevel */ | ||
set priority(newPriority) { | ||
this.level = newPriority; | ||
} | ||
/** @deprecated use logLevel */ | ||
get priority() { | ||
return this.level; | ||
} | ||
/** @deprecated use logLevel */ | ||
getPriority() { | ||
return this.level; | ||
} | ||
// Configure | ||
enable(enabled = true) { | ||
this._storage.setConfiguration({ enabled }); | ||
return this; | ||
} | ||
setLevel(level) { | ||
this._storage.setConfiguration({ level }); | ||
return this; | ||
} | ||
/** return the current status of the setting */ | ||
get(setting) { | ||
return this._storage.config[setting]; | ||
} | ||
// update the status of the setting | ||
set(setting, value) { | ||
this._storage.setConfiguration({ [setting]: value }); | ||
} | ||
/** Logs the current settings as a table */ | ||
settings() { | ||
if (console.table) { | ||
console.table(this._storage.config); | ||
} | ||
else { | ||
console.log(this._storage.config); | ||
} | ||
} | ||
// Unconditional logging | ||
assert(condition, message) { | ||
assert(condition, message); | ||
} | ||
warn(message) { | ||
return this._getLogFunction(0, message, originalConsole.warn, arguments, ONCE); | ||
} | ||
error(message) { | ||
return this._getLogFunction(0, message, originalConsole.error, arguments); | ||
} | ||
/** Print a deprecation warning */ | ||
deprecated(oldUsage, newUsage) { | ||
return this.warn(`\`${oldUsage}\` is deprecated and will be removed \ | ||
in a later version. Use \`${newUsage}\` instead`); | ||
} | ||
removed(oldUsage, newUsage) { | ||
return this.error(`\`${oldUsage}\` has been removed. Use \`${newUsage}\` instead`); | ||
} | ||
probe(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, originalConsole.log, arguments, { | ||
time: true, | ||
once: true | ||
}); | ||
} | ||
log(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, originalConsole.debug, arguments); | ||
} | ||
info(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.info, arguments); | ||
} | ||
once(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, originalConsole.debug || originalConsole.info, arguments, ONCE); | ||
} | ||
table(logLevel, table, columns) { | ||
if (table) { | ||
return this._getLogFunction(logLevel, table, console.table || noop, columns && [columns], { | ||
tag: getTableHeader(table) | ||
}); | ||
} | ||
return noop; | ||
} | ||
image(_ref) { | ||
let { | ||
logLevel, | ||
priority, | ||
image, | ||
message = '', | ||
scale = 1 | ||
} = _ref; | ||
if (!this._shouldLog(logLevel || priority)) { | ||
return noop; | ||
/** Print a removal warning */ | ||
removed(oldUsage, newUsage) { | ||
return this.error(`\`${oldUsage}\` has been removed. Use \`${newUsage}\` instead`); | ||
} | ||
return isBrowser() ? logImageInBrowser({ | ||
image, | ||
message, | ||
scale | ||
}) : logImageInNode({ | ||
image, | ||
message, | ||
scale | ||
}); | ||
} | ||
time(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.time ? console.time : console.info); | ||
} | ||
timeEnd(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.timeEnd ? console.timeEnd : console.info); | ||
} | ||
timeStamp(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.timeStamp || noop); | ||
} | ||
group(logLevel, message) { | ||
let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { | ||
collapsed: false | ||
}; | ||
const options = normalizeArguments({ | ||
logLevel, | ||
message, | ||
opts | ||
}); | ||
const { | ||
collapsed | ||
} = opts; | ||
options.method = (collapsed ? console.groupCollapsed : console.group) || console.info; | ||
return this._getLogFunction(options); | ||
} | ||
groupCollapsed(logLevel, message) { | ||
let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.group(logLevel, message, Object.assign({}, opts, { | ||
collapsed: true | ||
})); | ||
} | ||
groupEnd(logLevel) { | ||
return this._getLogFunction(logLevel, '', console.groupEnd || noop); | ||
} | ||
withGroup(logLevel, message, func) { | ||
this.group(logLevel, message)(); | ||
try { | ||
func(); | ||
} finally { | ||
this.groupEnd(logLevel)(); | ||
probe(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, originalConsole.log, arguments, { | ||
time: true, | ||
once: true | ||
}); | ||
} | ||
} | ||
trace() { | ||
if (console.trace) { | ||
console.trace(); | ||
log(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, originalConsole.debug, arguments); | ||
} | ||
} | ||
_shouldLog(logLevel) { | ||
return this.isEnabled() && this.getLevel() >= normalizeLogLevel(logLevel); | ||
} | ||
_getLogFunction(logLevel, message, method, args, opts) { | ||
if (this._shouldLog(logLevel)) { | ||
opts = normalizeArguments({ | ||
logLevel, | ||
message, | ||
args, | ||
opts | ||
}); | ||
method = method || opts.method; | ||
assert(method); | ||
opts.total = this.getTotal(); | ||
opts.delta = this.getDelta(); | ||
this._deltaTs = getHiResTimestamp(); | ||
const tag = opts.tag || opts.message; | ||
if (opts.once && tag) { | ||
if (!cache[tag]) { | ||
cache[tag] = getHiResTimestamp(); | ||
} else { | ||
return noop; | ||
info(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.info, arguments); | ||
} | ||
once(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, originalConsole.debug || originalConsole.info, arguments, ONCE); | ||
} | ||
/** Logs an object as a table */ | ||
table(logLevel, table, columns) { | ||
if (table) { | ||
return this._getLogFunction(logLevel, table, console.table || noop, (columns && [columns]), { | ||
tag: getTableHeader(table) | ||
}); | ||
} | ||
} | ||
message = decorateMessage(this.id, opts.message, opts); | ||
return method.bind(console, message, ...opts.args); | ||
return noop; | ||
} | ||
return noop; | ||
} | ||
/** logs an image under Chrome */ | ||
image({ logLevel, priority, image, message = '', scale = 1 }) { | ||
if (!this._shouldLog(logLevel || priority)) { | ||
return noop; | ||
} | ||
return isBrowser() | ||
? logImageInBrowser({ image, message, scale }) | ||
: logImageInNode({ image, message, scale }); | ||
} | ||
time(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.time ? console.time : console.info); | ||
} | ||
timeEnd(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.timeEnd ? console.timeEnd : console.info); | ||
} | ||
timeStamp(logLevel, message) { | ||
return this._getLogFunction(logLevel, message, console.timeStamp || noop); | ||
} | ||
group(logLevel, message, opts = { collapsed: false }) { | ||
const options = normalizeArguments({ logLevel, message, opts }); | ||
const { collapsed } = opts; | ||
// @ts-expect-error | ||
options.method = (collapsed ? console.groupCollapsed : console.group) || console.info; | ||
return this._getLogFunction(options); | ||
} | ||
groupCollapsed(logLevel, message, opts = {}) { | ||
return this.group(logLevel, message, Object.assign({}, opts, { collapsed: true })); | ||
} | ||
groupEnd(logLevel) { | ||
return this._getLogFunction(logLevel, '', console.groupEnd || noop); | ||
} | ||
// EXPERIMENTAL | ||
withGroup(logLevel, message, func) { | ||
this.group(logLevel, message)(); | ||
try { | ||
func(); | ||
} | ||
finally { | ||
this.groupEnd(logLevel)(); | ||
} | ||
} | ||
trace() { | ||
if (console.trace) { | ||
console.trace(); | ||
} | ||
} | ||
// PRIVATE METHODS | ||
/** Deduces log level from a variety of arguments */ | ||
_shouldLog(logLevel) { | ||
return this.isEnabled() && this.getLevel() >= normalizeLogLevel(logLevel); | ||
} | ||
_getLogFunction(logLevel, message, method, args, opts) { | ||
if (this._shouldLog(logLevel)) { | ||
// normalized opts + timings | ||
opts = normalizeArguments({ logLevel, message, args, opts }); | ||
method = method || opts.method; | ||
assert(method); | ||
opts.total = this.getTotal(); | ||
opts.delta = this.getDelta(); | ||
// reset delta timer | ||
this._deltaTs = getHiResTimestamp(); | ||
const tag = opts.tag || opts.message; | ||
if (opts.once && tag) { | ||
if (!cache[tag]) { | ||
cache[tag] = getHiResTimestamp(); | ||
} | ||
else { | ||
return noop; | ||
} | ||
} | ||
// TODO - Make throttling work with groups | ||
// if (opts.nothrottle || !throttle(tag, this.LOG_THROTTLE_TIMEOUT)) { | ||
// return noop; | ||
// } | ||
message = decorateMessage(this.id, opts.message, opts); | ||
// Bind console function so that it can be called after being returned | ||
return method.bind(console, message, ...opts.args); | ||
} | ||
return noop; | ||
} | ||
} | ||
Log.VERSION = VERSION; | ||
/** | ||
* Get logLevel from first argument: | ||
* - log(logLevel, message, args) => logLevel | ||
* - log(message, args) => 0 | ||
* - log({logLevel, ...}, message, args) => logLevel | ||
* - log({logLevel, message, args}) => logLevel | ||
*/ | ||
function normalizeLogLevel(logLevel) { | ||
if (!logLevel) { | ||
return 0; | ||
} | ||
let resolvedLevel; | ||
switch (typeof logLevel) { | ||
case 'number': | ||
resolvedLevel = logLevel; | ||
break; | ||
case 'object': | ||
resolvedLevel = logLevel.logLevel || logLevel.priority || 0; | ||
break; | ||
default: | ||
return 0; | ||
} | ||
assert(Number.isFinite(resolvedLevel) && resolvedLevel >= 0); | ||
return resolvedLevel; | ||
if (!logLevel) { | ||
return 0; | ||
} | ||
let resolvedLevel; | ||
switch (typeof logLevel) { | ||
case 'number': | ||
resolvedLevel = logLevel; | ||
break; | ||
case 'object': | ||
// Backward compatibility | ||
// TODO - deprecate `priority` | ||
// @ts-expect-error | ||
resolvedLevel = logLevel.logLevel || logLevel.priority || 0; | ||
break; | ||
default: | ||
return 0; | ||
} | ||
// 'log level must be a number' | ||
assert(Number.isFinite(resolvedLevel) && resolvedLevel >= 0); | ||
return resolvedLevel; | ||
} | ||
/** | ||
* "Normalizes" the various argument patterns into an object with known types | ||
* - log(logLevel, message, args) => {logLevel, message, args} | ||
* - log(message, args) => {logLevel: 0, message, args} | ||
* - log({logLevel, ...}, message, args) => {logLevel, message, args} | ||
* - log({logLevel, message, args}) => {logLevel, message, args} | ||
*/ | ||
export function normalizeArguments(opts) { | ||
const { | ||
logLevel, | ||
message | ||
} = opts; | ||
opts.logLevel = normalizeLogLevel(logLevel); | ||
const args = opts.args ? Array.from(opts.args) : []; | ||
while (args.length && args.shift() !== message) {} | ||
switch (typeof logLevel) { | ||
case 'string': | ||
case 'function': | ||
if (message !== undefined) { | ||
args.unshift(message); | ||
} | ||
opts.message = logLevel; | ||
break; | ||
case 'object': | ||
Object.assign(opts, logLevel); | ||
break; | ||
default: | ||
} | ||
if (typeof opts.message === 'function') { | ||
opts.message = opts.message(); | ||
} | ||
const messageType = typeof opts.message; | ||
assert(messageType === 'string' || messageType === 'object'); | ||
return Object.assign(opts, { | ||
args | ||
}, opts.opts); | ||
const { logLevel, message } = opts; | ||
opts.logLevel = normalizeLogLevel(logLevel); | ||
// We use `arguments` instead of rest parameters (...args) because IE | ||
// does not support the syntax. Rest parameters is transpiled to code with | ||
// perf impact. Doing it here instead avoids constructing args when logging is | ||
// disabled. | ||
// TODO - remove when/if IE support is dropped | ||
const args = opts.args ? Array.from(opts.args) : []; | ||
// args should only contain arguments that appear after `message` | ||
// eslint-disable-next-line no-empty | ||
while (args.length && args.shift() !== message) { } | ||
switch (typeof logLevel) { | ||
case 'string': | ||
case 'function': | ||
if (message !== undefined) { | ||
args.unshift(message); | ||
} | ||
opts.message = logLevel; | ||
break; | ||
case 'object': | ||
Object.assign(opts, logLevel); | ||
break; | ||
default: | ||
} | ||
// Resolve functions into strings by calling them | ||
if (typeof opts.message === 'function') { | ||
opts.message = opts.message(); | ||
} | ||
const messageType = typeof opts.message; | ||
// 'log message must be a string' or object | ||
assert(messageType === 'string' || messageType === 'object'); | ||
// original opts + normalized opts + opts arg + fixed up message | ||
return Object.assign(opts, { args }, opts.opts); | ||
} | ||
function decorateMessage(id, message, opts) { | ||
if (typeof message === 'string') { | ||
const time = opts.time ? leftPad(formatTime(opts.total)) : ''; | ||
message = opts.time ? `${id}: ${time} ${message}` : `${id}: ${message}`; | ||
message = addColor(message, opts.color, opts.background); | ||
} | ||
return message; | ||
if (typeof message === 'string') { | ||
const time = opts.time ? leftPad(formatTime(opts.total)) : ''; | ||
message = opts.time ? `${id}: ${time} ${message}` : `${id}: ${message}`; | ||
message = addColor(message, opts.color, opts.background); | ||
} | ||
return message; | ||
} | ||
function logImageInNode(_ref2) { | ||
let { | ||
image, | ||
message = '', | ||
scale = 1 | ||
} = _ref2; | ||
console.warn('removed'); | ||
return noop; | ||
/** @deprecated Function removed */ | ||
function logImageInNode({ image, message = '', scale = 1 }) { | ||
console.warn('removed'); | ||
return noop; | ||
} | ||
function logImageInBrowser(_ref3) { | ||
let { | ||
image, | ||
message = '', | ||
scale = 1 | ||
} = _ref3; | ||
if (typeof image === 'string') { | ||
const img = new Image(); | ||
img.onload = () => { | ||
const args = formatImage(img, message, scale); | ||
console.log(...args); | ||
}; | ||
img.src = image; | ||
function logImageInBrowser({ image, message = '', scale = 1 }) { | ||
if (typeof image === 'string') { | ||
const img = new Image(); | ||
img.onload = () => { | ||
const args = formatImage(img, message, scale); | ||
console.log(...args); | ||
}; | ||
img.src = image; | ||
return noop; | ||
} | ||
const element = image.nodeName || ''; | ||
if (element.toLowerCase() === 'img') { | ||
console.log(...formatImage(image, message, scale)); | ||
return noop; | ||
} | ||
if (element.toLowerCase() === 'canvas') { | ||
const img = new Image(); | ||
img.onload = () => console.log(...formatImage(img, message, scale)); | ||
img.src = image.toDataURL(); | ||
return noop; | ||
} | ||
return noop; | ||
} | ||
const element = image.nodeName || ''; | ||
if (element.toLowerCase() === 'img') { | ||
console.log(...formatImage(image, message, scale)); | ||
return noop; | ||
} | ||
if (element.toLowerCase() === 'canvas') { | ||
const img = new Image(); | ||
img.onload = () => console.log(...formatImage(img, message, scale)); | ||
img.src = image.toDataURL(); | ||
return noop; | ||
} | ||
return noop; | ||
} | ||
function getTableHeader(table) { | ||
for (const key in table) { | ||
for (const title in table[key]) { | ||
return title || 'untitled'; | ||
for (const key in table) { | ||
for (const title in table[key]) { | ||
return title || 'untitled'; | ||
} | ||
} | ||
} | ||
return 'empty'; | ||
return 'empty'; | ||
} | ||
//# sourceMappingURL=log.js.map |
export default function assert(condition, message) { | ||
if (!condition) { | ||
throw new Error(message || 'Assertion failed'); | ||
} | ||
if (!condition) { | ||
throw new Error(message || 'Assertion failed'); | ||
} | ||
} | ||
//# sourceMappingURL=assert.js.map |
@@ -1,17 +0,36 @@ | ||
export function autobind(obj) { | ||
let predefined = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['constructor']; | ||
const proto = Object.getPrototypeOf(obj); | ||
const propNames = Object.getOwnPropertyNames(proto); | ||
const object = obj; | ||
for (const key of propNames) { | ||
const value = object[key]; | ||
if (typeof value === 'function') { | ||
if (!predefined.find(name => key === name)) { | ||
object[key] = value.bind(obj); | ||
} | ||
// Copyright (c) 2015 - 2017 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
/** | ||
* Binds the "this" argument of all functions on a class instance to the instance | ||
* @param obj - class instance (typically a react component) | ||
*/ | ||
export function autobind(obj, predefined = ['constructor']) { | ||
const proto = Object.getPrototypeOf(obj); | ||
const propNames = Object.getOwnPropertyNames(proto); | ||
const object = obj; | ||
for (const key of propNames) { | ||
const value = object[key]; | ||
if (typeof value === 'function') { | ||
if (!predefined.find(name => key === name)) { | ||
object[key] = value.bind(obj); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=autobind.js.map |
import { isBrowser } from '@probe.gl/env'; | ||
export let COLOR; | ||
export var COLOR; | ||
(function (COLOR) { | ||
COLOR[COLOR["BLACK"] = 30] = "BLACK"; | ||
COLOR[COLOR["RED"] = 31] = "RED"; | ||
COLOR[COLOR["GREEN"] = 32] = "GREEN"; | ||
COLOR[COLOR["YELLOW"] = 33] = "YELLOW"; | ||
COLOR[COLOR["BLUE"] = 34] = "BLUE"; | ||
COLOR[COLOR["MAGENTA"] = 35] = "MAGENTA"; | ||
COLOR[COLOR["CYAN"] = 36] = "CYAN"; | ||
COLOR[COLOR["WHITE"] = 37] = "WHITE"; | ||
COLOR[COLOR["BRIGHT_BLACK"] = 90] = "BRIGHT_BLACK"; | ||
COLOR[COLOR["BRIGHT_RED"] = 91] = "BRIGHT_RED"; | ||
COLOR[COLOR["BRIGHT_GREEN"] = 92] = "BRIGHT_GREEN"; | ||
COLOR[COLOR["BRIGHT_YELLOW"] = 93] = "BRIGHT_YELLOW"; | ||
COLOR[COLOR["BRIGHT_BLUE"] = 94] = "BRIGHT_BLUE"; | ||
COLOR[COLOR["BRIGHT_MAGENTA"] = 95] = "BRIGHT_MAGENTA"; | ||
COLOR[COLOR["BRIGHT_CYAN"] = 96] = "BRIGHT_CYAN"; | ||
COLOR[COLOR["BRIGHT_WHITE"] = 97] = "BRIGHT_WHITE"; | ||
COLOR[COLOR["BLACK"] = 30] = "BLACK"; | ||
COLOR[COLOR["RED"] = 31] = "RED"; | ||
COLOR[COLOR["GREEN"] = 32] = "GREEN"; | ||
COLOR[COLOR["YELLOW"] = 33] = "YELLOW"; | ||
COLOR[COLOR["BLUE"] = 34] = "BLUE"; | ||
COLOR[COLOR["MAGENTA"] = 35] = "MAGENTA"; | ||
COLOR[COLOR["CYAN"] = 36] = "CYAN"; | ||
COLOR[COLOR["WHITE"] = 37] = "WHITE"; | ||
COLOR[COLOR["BRIGHT_BLACK"] = 90] = "BRIGHT_BLACK"; | ||
COLOR[COLOR["BRIGHT_RED"] = 91] = "BRIGHT_RED"; | ||
COLOR[COLOR["BRIGHT_GREEN"] = 92] = "BRIGHT_GREEN"; | ||
COLOR[COLOR["BRIGHT_YELLOW"] = 93] = "BRIGHT_YELLOW"; | ||
COLOR[COLOR["BRIGHT_BLUE"] = 94] = "BRIGHT_BLUE"; | ||
COLOR[COLOR["BRIGHT_MAGENTA"] = 95] = "BRIGHT_MAGENTA"; | ||
COLOR[COLOR["BRIGHT_CYAN"] = 96] = "BRIGHT_CYAN"; | ||
COLOR[COLOR["BRIGHT_WHITE"] = 97] = "BRIGHT_WHITE"; | ||
})(COLOR || (COLOR = {})); | ||
const BACKGROUND_INCREMENT = 10; | ||
function getColor(color) { | ||
if (typeof color !== 'string') { | ||
return color; | ||
} | ||
color = color.toUpperCase(); | ||
return COLOR[color] || COLOR.WHITE; | ||
if (typeof color !== 'string') { | ||
return color; | ||
} | ||
color = color.toUpperCase(); | ||
return COLOR[color] || COLOR.WHITE; | ||
} | ||
export function addColor(string, color, background) { | ||
if (!isBrowser && typeof string === 'string') { | ||
if (color) { | ||
const colorCode = getColor(color); | ||
string = `\u001b[${colorCode}m${string}\u001b[39m`; | ||
if (!isBrowser && typeof string === 'string') { | ||
if (color) { | ||
const colorCode = getColor(color); | ||
string = `\u001b[${colorCode}m${string}\u001b[39m`; | ||
} | ||
if (background) { | ||
// background colors values are +10 | ||
const colorCode = getColor(background); | ||
string = `\u001b[${colorCode + BACKGROUND_INCREMENT}m${string}\u001b[49m`; | ||
} | ||
} | ||
if (background) { | ||
const colorCode = getColor(background); | ||
string = `\u001b[${colorCode + BACKGROUND_INCREMENT}m${string}\u001b[49m`; | ||
} | ||
} | ||
return string; | ||
return string; | ||
} | ||
//# sourceMappingURL=color.js.map |
@@ -0,90 +1,89 @@ | ||
// probe.gl, MIT license | ||
/** | ||
* Format time | ||
*/ | ||
export function formatTime(ms) { | ||
let formatted; | ||
if (ms < 10) { | ||
formatted = `${ms.toFixed(2)}ms`; | ||
} else if (ms < 100) { | ||
formatted = `${ms.toFixed(1)}ms`; | ||
} else if (ms < 1000) { | ||
formatted = `${ms.toFixed(0)}ms`; | ||
} else { | ||
formatted = `${(ms / 1000).toFixed(2)}s`; | ||
} | ||
return formatted; | ||
let formatted; | ||
if (ms < 10) { | ||
formatted = `${ms.toFixed(2)}ms`; | ||
} | ||
else if (ms < 100) { | ||
formatted = `${ms.toFixed(1)}ms`; | ||
} | ||
else if (ms < 1000) { | ||
formatted = `${ms.toFixed(0)}ms`; | ||
} | ||
else { | ||
formatted = `${(ms / 1000).toFixed(2)}s`; | ||
} | ||
return formatted; | ||
} | ||
export function leftPad(string) { | ||
let length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 8; | ||
const padLength = Math.max(length - string.length, 0); | ||
return `${' '.repeat(padLength)}${string}`; | ||
export function leftPad(string, length = 8) { | ||
const padLength = Math.max(length - string.length, 0); | ||
return `${' '.repeat(padLength)}${string}`; | ||
} | ||
export function rightPad(string) { | ||
let length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 8; | ||
const padLength = Math.max(length - string.length, 0); | ||
return `${string}${' '.repeat(padLength)}`; | ||
export function rightPad(string, length = 8) { | ||
const padLength = Math.max(length - string.length, 0); | ||
return `${string}${' '.repeat(padLength)}`; | ||
} | ||
export function formatValue(v) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const EPSILON = 1e-16; | ||
const { | ||
isInteger = false | ||
} = options; | ||
if (Array.isArray(v) || ArrayBuffer.isView(v)) { | ||
return formatArrayValue(v, options); | ||
} | ||
if (!Number.isFinite(v)) { | ||
return String(v); | ||
} | ||
if (Math.abs(v) < EPSILON) { | ||
return isInteger ? '0' : '0.'; | ||
} | ||
if (isInteger) { | ||
return v.toFixed(0); | ||
} | ||
if (Math.abs(v) > 100 && Math.abs(v) < 10000) { | ||
return v.toFixed(0); | ||
} | ||
const string = v.toPrecision(2); | ||
const decimal = string.indexOf('.0'); | ||
return decimal === string.length - 2 ? string.slice(0, -1) : string; | ||
export function formatValue(v, options = {}) { | ||
const EPSILON = 1e-16; | ||
const { isInteger = false } = options; | ||
if (Array.isArray(v) || ArrayBuffer.isView(v)) { | ||
return formatArrayValue(v, options); | ||
} | ||
if (!Number.isFinite(v)) { | ||
return String(v); | ||
} | ||
// @ts-expect-error | ||
if (Math.abs(v) < EPSILON) { | ||
return isInteger ? '0' : '0.'; | ||
} | ||
if (isInteger) { | ||
// @ts-expect-error | ||
return v.toFixed(0); | ||
} | ||
// @ts-expect-error | ||
if (Math.abs(v) > 100 && Math.abs(v) < 10000) { | ||
// @ts-expect-error | ||
return v.toFixed(0); | ||
} | ||
// @ts-expect-error | ||
const string = v.toPrecision(2); | ||
const decimal = string.indexOf('.0'); | ||
return decimal === string.length - 2 ? string.slice(0, -1) : string; | ||
} | ||
/** Helper to formatValue */ | ||
function formatArrayValue(v, options) { | ||
const { | ||
maxElts = 16, | ||
size = 1 | ||
} = options; | ||
let string = '['; | ||
for (let i = 0; i < v.length && i < maxElts; ++i) { | ||
if (i > 0) { | ||
string += `,${i % size === 0 ? ' ' : ''}`; | ||
const { maxElts = 16, size = 1 } = options; | ||
let string = '['; | ||
for (let i = 0; i < v.length && i < maxElts; ++i) { | ||
if (i > 0) { | ||
string += `,${i % size === 0 ? ' ' : ''}`; | ||
} | ||
string += formatValue(v[i], options); | ||
} | ||
string += formatValue(v[i], options); | ||
} | ||
const terminator = v.length > maxElts ? '...' : ']'; | ||
return `${string}${terminator}`; | ||
const terminator = v.length > maxElts ? '...' : ']'; | ||
return `${string}${terminator}`; | ||
} | ||
export function formatImage(image, message, scale) { | ||
let maxWidth = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 600; | ||
const imageUrl = image.src.replace(/\(/g, '%28').replace(/\)/g, '%29'); | ||
if (image.width > maxWidth) { | ||
scale = Math.min(scale, maxWidth / image.width); | ||
} | ||
const width = image.width * scale; | ||
const height = image.height * scale; | ||
const style = ['font-size:1px;', `padding:${Math.floor(height / 2)}px ${Math.floor(width / 2)}px;`, `line-height:${height}px;`, `background:url(${imageUrl});`, `background-size:${width}px ${height}px;`, 'color:transparent;'].join(''); | ||
return [`${message} %c+`, style]; | ||
/** | ||
* Log an image to the console (uses browser specific console formatting styles) | ||
* Inspired by https://github.com/hughsk/console-image (MIT license) | ||
*/ | ||
export function formatImage(image, message, scale, maxWidth = 600) { | ||
const imageUrl = image.src.replace(/\(/g, '%28').replace(/\)/g, '%29'); | ||
if (image.width > maxWidth) { | ||
scale = Math.min(scale, maxWidth / image.width); | ||
} | ||
const width = image.width * scale; | ||
const height = image.height * scale; | ||
const style = [ | ||
'font-size:1px;', | ||
`padding:${Math.floor(height / 2)}px ${Math.floor(width / 2)}px;`, | ||
`line-height:${height}px;`, | ||
`background:url(${imageUrl});`, | ||
`background-size:${width}px ${height}px;`, | ||
'color:transparent;' | ||
].join(''); | ||
return [`${message} %c+`, style]; | ||
} | ||
//# sourceMappingURL=formatters.js.map |
@@ -0,20 +1,18 @@ | ||
// probe.gl, MIT license | ||
import { window, process, isBrowser } from '@probe.gl/env'; | ||
/** Get best timer available. */ | ||
export function getHiResTimestamp() { | ||
let timestamp; | ||
if (isBrowser() && window.performance) { | ||
var _window$performance, _window$performance$n; | ||
timestamp = window === null || window === void 0 ? void 0 : (_window$performance = window.performance) === null || _window$performance === void 0 ? void 0 : (_window$performance$n = _window$performance.now) === null || _window$performance$n === void 0 ? void 0 : _window$performance$n.call(_window$performance); | ||
} else if ('hrtime' in process) { | ||
var _process$hrtime; | ||
const timeParts = process === null || process === void 0 ? void 0 : (_process$hrtime = process.hrtime) === null || _process$hrtime === void 0 ? void 0 : _process$hrtime.call(process); | ||
timestamp = timeParts[0] * 1000 + timeParts[1] / 1e6; | ||
} else { | ||
timestamp = Date.now(); | ||
} | ||
return timestamp; | ||
let timestamp; | ||
if (isBrowser() && window.performance) { | ||
timestamp = window?.performance?.now?.(); | ||
} | ||
else if ('hrtime' in process) { | ||
// @ts-ignore | ||
const timeParts = process?.hrtime?.(); | ||
timestamp = timeParts[0] * 1000 + timeParts[1] / 1e6; | ||
} | ||
else { | ||
timestamp = Date.now(); | ||
} | ||
return timestamp; | ||
} | ||
//# sourceMappingURL=hi-res-timestamp.js.map |
@@ -0,52 +1,42 @@ | ||
// probe.gl, MIT license | ||
function getStorage(type) { | ||
try { | ||
const storage = window[type]; | ||
const x = '__storage_test__'; | ||
storage.setItem(x, x); | ||
storage.removeItem(x); | ||
return storage; | ||
} catch (e) { | ||
return null; | ||
} | ||
try { | ||
const storage = window[type]; | ||
const x = '__storage_test__'; | ||
storage.setItem(x, x); | ||
storage.removeItem(x); | ||
return storage; | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
} | ||
// Store keys in local storage via simple interface | ||
export class LocalStorage { | ||
constructor(id, defaultConfig) { | ||
let type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'sessionStorage'; | ||
this.storage = void 0; | ||
this.id = void 0; | ||
this.config = void 0; | ||
this.storage = getStorage(type); | ||
this.id = id; | ||
this.config = defaultConfig; | ||
this._loadConfiguration(); | ||
} | ||
getConfiguration() { | ||
return this.config; | ||
} | ||
setConfiguration(configuration) { | ||
Object.assign(this.config, configuration); | ||
if (this.storage) { | ||
const serialized = JSON.stringify(this.config); | ||
this.storage.setItem(this.id, serialized); | ||
constructor(id, defaultConfig, type = 'sessionStorage') { | ||
this.storage = getStorage(type); | ||
this.id = id; | ||
this.config = defaultConfig; | ||
this._loadConfiguration(); | ||
} | ||
} | ||
_loadConfiguration() { | ||
let configuration = {}; | ||
if (this.storage) { | ||
const serializedConfiguration = this.storage.getItem(this.id); | ||
configuration = serializedConfiguration ? JSON.parse(serializedConfiguration) : {}; | ||
getConfiguration() { | ||
return this.config; | ||
} | ||
Object.assign(this.config, configuration); | ||
return this; | ||
} | ||
setConfiguration(configuration) { | ||
Object.assign(this.config, configuration); | ||
if (this.storage) { | ||
const serialized = JSON.stringify(this.config); | ||
this.storage.setItem(this.id, serialized); | ||
} | ||
} | ||
// Get config from persistent store, if available | ||
_loadConfiguration() { | ||
let configuration = {}; | ||
if (this.storage) { | ||
const serializedConfiguration = this.storage.getItem(this.id); | ||
configuration = serializedConfiguration ? JSON.parse(serializedConfiguration) : {}; | ||
} | ||
Object.assign(this.config, configuration); | ||
return this; | ||
} | ||
} | ||
//# sourceMappingURL=local-storage.js.map |
@@ -6,3 +6,3 @@ { | ||
"type": "module", | ||
"version": "4.0.6", | ||
"version": "4.0.7", | ||
"keywords": [ | ||
@@ -37,6 +37,5 @@ "javascript", | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0", | ||
"@probe.gl/env": "4.0.6" | ||
"@probe.gl/env": "4.0.7" | ||
}, | ||
"gitHead": "ed10fc803b4b0feb08bdcde39ef9407db1f79e64" | ||
"gitHead": "e1192bb8b78fb8f713519ab693638408da08de16" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1
144462
1902
+ Added@probe.gl/env@4.0.7(transitive)
- Removed@babel/runtime@^7.0.0
- Removed@babel/runtime@7.26.7(transitive)
- Removed@probe.gl/env@4.0.6(transitive)
- Removedregenerator-runtime@0.14.1(transitive)
Updated@probe.gl/env@4.0.7