tguard-js
    Preparing search index...

    Function isEnvDefined

    • Checks if an environment variable is defined and has a non-empty string value.

      Parameters

      • envVar: string | undefined

        The environment variable value to check.

      Returns envVar is string

      true if the given environment variable is a non-empty string, otherwise false.

      This function is specifically designed for checking process.env variables. An environment variable is considered "defined" if it exists (is not undefined) and its value is a non-empty string. An empty string ("") is treated as "not defined" in this context, as is common for environment flags.

      # Setting environment variables
      env SOME_VAR="hello"
      env EMPTY_VAR=""

      Now, we can use isEnvDefined to check if an environment variable is defined and has a non-empty string value.

      // Checking environment variables
      isEnvDefined(process.env.SOME_VAR); // true
      isEnvDefined(process.env.EMPTY_VAR); // false

      // Environment variable `UNDEFINED_VAR` is not set yet
      isEnvDefined(process.env.UNDEFINED_VAR); // false

      let myEnvVar: string | undefined = process.env.MY_FLAG;
      if (isEnvDefined(myEnvVar)) {
      // myEnvVar is now safely typed as 'string' in this scope
      console.log(`My flag is set to: ${myEnvVar}`);
      }

      1.0

      isNonNullish - Use this function to check if a value is "non-nullish".