Skip to content

Instantly share code, notes, and snippets.

View arabold's full-sized avatar
🦀
Making things. Breaking things.

Andre Rabold arabold

🦀
Making things. Breaking things.
View GitHub Profile
/** Log to Browser Console; Use colorful logs for Chome, Safari, etc. */
export function createBrowserLogger(): LogHandlerFunc {
return function log({ level, message, payload, meta }: LogEntry) {
const logFunc = ConsoleLogFuncs[level];
if (payload && typeof console.groupCollapsed === "function") {
console.groupCollapsed(`%c[${meta?.tag}] %c${message}`, "color:magenta;", "color:black;font-weight:normal");
logFunc(payload);
console.groupEnd();
} else if (payload) {
logFunc(`%c[${meta?.tag}] %c${message}`, "color:magenta;", "color:black;", payload);
/**
* Fields that will automatically be scrubbed from logs. Field names will automatically transformed to lower case
* and special characters stripped before matching the string, so e.g. "access_token", "access-token" and
* "accessToken" will all match "accesstoken".
*
* To disable scrubbing, set this to an empty array
*/
static scrubValues = [
"password",
"newpassword",
/** A list of class names, primarily from network libraries, that will be collapsed in the logs in order to keep it shorter and more readable */
static collapseClasses = [
"ClientRequest",
"IncomingMessage",
"Buffer",
"TLSSocket",
"Socket",
"WebSocket",
"WebSocketTransport",
"ReadableState",
/**
* Destroys circular references for use with JSON serialization
*
* @param from - Source object or array
* @param seen - Array with object already serialized. Set to `[]` (empty array)
* when using this function!
*/
private static _destroyCircular(from: any, seen: any[]) {
let to: any;
if (Array.isArray(from)) {
/** Returns `true` if running in a CI environment, e.g. during an automated build */
static get isCI(): boolean {
if (!Logger.isNode) {
return false;
}
const { CI, CONTINUOUS_INTEGRATION, BUILD_NUMBER, RUN_ID } = process.env;
// Shamelessly stolen from https://github.com/watson/ci-info
return !!(
CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
CONTINUOUS_INTEGRATION || // Travis CI, Cirrus CI
/**
* Returns `true` if code is running with JSDOM
*/
static get isJSDOM(): boolean {
return navigator?.userAgent?.includes("Node.js") || navigator?.userAgent?.includes("jsdom");
}
/**
* Returns `true` if code is running in a web browser environment
*/
/**
* Wrap async handler functions for use with `onPress` and similar React callbacks, which are typically
* not safe to use with promises. `useAsyncFunc` gracefully handles any promise errors.
*
* Note that this hook, unlike `useCallback`, does not memoize the callback function itself. To create
* a memoized function wrap it with `useCallback` instead.
* @example
* ```tsx
import Intl from "intl";
import React, { useMemo } from "react";
import useLocalization from "./useLocalization";
export interface NumberFormatProps {
/** The value to format */
value: number;
/** Override system locale (not recommended) */
locale?: string;
import Intl from "intl";
import React, { useMemo } from "react";
import useLocalization from "./useLocalization";
export interface DateTimeFormatProps {
/** The value to format */
value: Date | string | number;
/** Override system locale (not recommended) */
locale?: string;
import { useCallback, useRef, useState } from "react";
export type Dispatch<TState> = (action: ThunkAction<TState>) => Promise<void>
/** A thunk action that can be dispatched by the {@link useThunkReducer}. */
export type ThunkAction<TState> = (
dispatch: Dispatch<TState>,
getState: () => Readonly<TState>,
) => Promise<TState | undefined | void> | TState | undefined | void;