github.com/ethereum-optimism/optimism@v1.7.2/packages/core-utils/src/common/misc.ts (about)

     1  /**
     2   * Basic timeout-based async sleep function.
     3   *
     4   * @param ms Number of milliseconds to sleep.
     5   */
     6  export const sleep = async (ms: number): Promise<void> => {
     7    return new Promise<void>((resolve) => {
     8      setTimeout(() => {
     9        resolve(null)
    10      }, ms)
    11    })
    12  }
    13  
    14  /**
    15   * Returns a clone of the object.
    16   *
    17   * @param obj Object to clone.
    18   * @returns Clone of the object.
    19   */
    20  export const clone = (obj: any): any => {
    21    if (typeof obj === 'undefined') {
    22      throw new Error(`Trying to clone undefined object`)
    23    }
    24    return { ...obj }
    25  }
    26  
    27  /**
    28   * Loads a variable from the environment and throws if the variable is not defined.
    29   *
    30   * @param name Name of the variable to load.
    31   * @returns Value of the variable as a string.
    32   */
    33  export const reqenv = (name: string): string => {
    34    const value = process.env[name]
    35    if (value === undefined) {
    36      throw new Error(`missing env var ${name}`)
    37    }
    38    return value
    39  }
    40  
    41  /**
    42   * Loads a variable from the environment and returns a fallback if not found.
    43   *
    44   * @param name Name of the variable to load.
    45   * @param [fallback] Optional value to be returned as fallback.
    46   * @returns Value of the variable as a string, fallback or undefined.
    47   */
    48  export const getenv = (name: string, fallback?: string): string | undefined => {
    49    return process.env[name] || fallback
    50  }
    51  
    52  /**
    53   * Returns true if the given string is a valid address.
    54   *
    55   * @param a First address to check.
    56   * @param b Second address to check.
    57   * @returns True if the given addresses match.
    58   */
    59  export const compareAddrs = (a: string, b: string): boolean => {
    60    return a.toLowerCase() === b.toLowerCase()
    61  }