All files / utils misc.ts

94.11% Statements 48/51
89.18% Branches 33/37
85.71% Functions 6/7
93.18% Lines 41/44

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117            94x   94x         94x 4x 4x 6x 3x       4x     94x       4454x 13367x       13367x 8420x 2x   8418x                 94x             2728x     2728x 7535x 4344x       2728x         94x         42x 294x       294x 294x   294x 4x 2x   290x 287x 5x   3x 1x     34x     94x         94x 135x   94x 66x                     94x  
/**
 * @module
 * @ignore
 * @internal
 */
 
import * as MH from "@lisn/globals/minification-helpers";
 
import { roundNumTo } from "@lisn/utils/math";
 
/**
 * @since v1.3.0
 */
export const deepCopy = <T extends object>(obj: T): T => {
  const clone = MH.copyObject(obj);
  for (const key in clone) {
    if (MH.isNonPrimitive(obj[key])) {
      obj[key] = deepCopy(obj[key]);
    }
  }
 
  return clone;
};
 
export const copyExistingKeys = <T extends object, F extends T>(
  fromObj: F,
  toObj: T,
) => {
  for (const key in toObj) {
    Iif (!MH.hasOwnProp(toObj, key)) {
      continue;
    }
 
    if (key in fromObj) {
      if (MH.isNonPrimitive(fromObj[key]) && MH.isNonPrimitive(toObj[key])) {
        copyExistingKeys(fromObj[key], toObj[key]);
      } else {
        toObj[key] = fromObj[key];
      }
    }
  }
};
 
// Omits the keys in object keysToRm from obj. This is to avoid hardcording the
// key names as a string so as to allow minifier to mangle them, and to avoid
// using object spread.
export const omitKeys = <
  O extends object,
  R extends { [K in keyof O]?: unknown },
>(
  obj: O,
  keysToRm: R,
): Omit<O, keyof R> => {
  const res: Partial<O> = {};
  let key: keyof O;
 
  for (key in obj) {
    if (!(key in keysToRm)) {
      res[key] = obj[key];
    }
  }
 
  return res as Omit<O, keyof R>;
};
 
// Returns true if the two objects are equal. If values are numeric, it will
// round to the given number of decimal places.
export const compareValuesIn = <T extends object>(
  objA: T,
  objB: T,
  roundTo = 3,
) => {
  for (const key in objA) {
    Iif (!MH.hasOwnProp(objA, key)) {
      continue;
    }
 
    const valA = objA[key];
    const valB = objB[key];
 
    if (MH.isNonPrimitive(valA) && MH.isNonPrimitive(valB)) {
      if (!compareValuesIn(valA, valB)) {
        return false;
      }
    } else if (MH.isNumber(valA) && MH.isNumber(valB)) {
      if (roundNumTo(valA, roundTo) !== roundNumTo(valB, roundTo)) {
        return false;
      }
    } else if (valA !== valB) {
      return false;
    }
  }
  return true;
};
 
export const keyExists = <T extends object>(
  obj: T,
  key: string | number | symbol,
): key is keyof T => MH.isNonPrimitive(obj) && key in obj;
 
export const toArrayIfSingle = <T>(value?: T | T[] | null | undefined): T[] =>
  MH.isArray(value) ? value : !MH.isNullish(value) ? [value] : [];
 
export const toBoolean = (value: unknown) =>
  value === true || value === "true" || value === ""
    ? true
    : MH.isNullish(value) || value === false || value === "false"
      ? false
      : null;
 
/**
 * @deprecated
 *
 * Deprecated alias for {@link toBoolean}
 */
export const toBool = toBoolean;