All files / utils log.ts

90.9% Statements 20/22
83.33% Branches 5/6
75% Functions 3/4
89.47% Lines 17/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60        94x 94x       94x   94x               94x                       94x 14x 14x                   94x 8x       5x       94x 94x 21x 21x 21x 21x    
/**
 * @module Utils
 */
 
import * as MC from "@lisn/globals/minification-constants";
import * as MH from "@lisn/globals/minification-helpers";
 
import { LogFunction } from "@lisn/globals/types";
 
import { joinAsString } from "@lisn/utils/text";
 
import { Callback } from "@lisn/modules/callback";
 
/**
 * Like `console.info` except if the string representation of the given
 * arguments has already been logged, it does nothing.
 *
 * @category Logging
 */
export const logInfo: LogFunction = (...args) => {
  Iif (!isMessageSeen(args)) {
    MH.consoleInfo(MC.LOG_PREFIX, ...args);
  }
};
 
/**
 * Like `console.warn` except if the string representation of the given
 * arguments has already been logged, it does nothing.
 *
 * @category Logging
 */
export const logWarn: LogFunction = (...args) => {
  if (!isMessageSeen(args)) {
    MH.consoleWarn(MC.LOG_PREFIX, ...args);
  }
};
 
/**
 * Like `console.error` except if the string representation of the given
 * arguments has already been logged, it does nothing.
 *
 * @category Logging
 */
export const logError: LogFunction = (...args) => {
  if (
    (MH.lengthOf(args) > 1 || args[0] !== Callback.REMOVE) &&
    !isMessageSeen(args)
  ) {
    MH.consoleError(MC.LOG_PREFIX, ...args);
  }
};
 
const discardMessages = MH.newSet<string>();
const isMessageSeen = (args: unknown[]) => {
  const msg = joinAsString(" ", ...args);
  const isSeen = discardMessages.has(msg);
  discardMessages.add(msg);
  return isSeen;
};