react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
55 lines (54 loc) • 1.39 kB
JavaScript
const noop = () => undefined;
const consoleMethods = [
"debug",
"error",
"info",
"log",
"trace",
"warn"
];
function getRuntimeRoots() {
const roots = [];
const addRoot = (value) => {
if (value && typeof value === "object") {
const root = value;
if (!roots.includes(root)) {
roots.push(root);
}
}
};
addRoot(globalThis);
addRoot(globalThis.global);
try {
if (typeof global !== "undefined") {
addRoot(global);
}
}
catch {
// Some embedded runtimes expose only globalThis.
}
return roots;
}
export function ensureRuntimeConsole() {
const roots = getRuntimeRoots();
const runtimeConsole = roots
.map((root) => root.console)
.find((value) => value && typeof value === "object") ?? {};
for (const root of roots) {
root.console = runtimeConsole;
}
for (const method of consoleMethods) {
if (typeof runtimeConsole[method] !== "function") {
runtimeConsole[method] = noop;
}
}
if (typeof runtimeConsole.assert !== "function") {
runtimeConsole.assert = (condition, ...messages) => {
if (!condition) {
runtimeConsole.error(...messages);
}
};
}
return runtimeConsole;
}
ensureRuntimeConsole();