How to use globalthis - 3 common examples

To help you get started, we’ve selected a few globalthis examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gajus / roarr / src / factories / createLogger.js View on Github external
import createGlobalThis from 'globalthis';
import stringify from 'json-stringify-safe';
import {
  sprintf,
} from 'sprintf-js';
import type {
  LoggerType,
  MessageContextType,
  MessageEventHandlerType,
  TranslateMessageFunctionType,
} from '../types';
import {
  logLevels,
} from '../constants';

const globalThis = createGlobalThis();

const createLogger = (onMessage: MessageEventHandlerType, parentContext?: MessageContextType): LoggerType => {
  // eslint-disable-next-line id-length, unicorn/prevent-abbreviations
  const log = (a, b, c, d, e, f, g, h, i, k) => {
    const time = Date.now();
    const sequence = globalThis.ROARR.sequence++;

    let context;
    let message;

    if (typeof a === 'string') {
      context = {
        ...parentContext || {},
      };
      message = sprintf(a, b, c, d, e, f, g, h, i, k);
    } else {
github gajus / roarr / src / log.js View on Github external
// @flow

import {
  boolean,
} from 'boolean';
import createGlobalThis from 'globalthis';
import environmentIsNode from 'detect-node';
import {
  createLogger,
  createMockLogger,
  createRoarrInititialGlobalState,
} from './factories';

const globalThis = createGlobalThis();

globalThis.ROARR = createRoarrInititialGlobalState(globalThis.ROARR || {});

let logFactory = createLogger;

if (environmentIsNode) {
  // eslint-disable-next-line no-process-env
  const enabled = boolean(process.env.ROARR_LOG || '');

  if (!enabled) {
    logFactory = createMockLogger;
  }
}

export type {
  LoggerType,
github gajus / roarr / test / roarr / roarr.js View on Github external
beforeEach(() => {
  const globalThis = createGlobalThis();

  globalThis.ROARR = createRoarrInititialGlobalState({});
});

globalthis

ECMAScript spec-compliant polyfill/shim for `globalThis`

MIT
Latest version published 2 years ago

Package Health Score

70 / 100
Full package analysis

Popular globalthis functions