v1.2.1.0
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Modules Pages

Description

The Env object provides access to the system environment variables.

It has various modes of operation, depending on how it is created/used:

  • As new Env() creates a new instance with all current environment variables as properties.
    • Variables could then be accessed directly, for example:
      var env = new Env();
      console.log(env.PATH);
      Env()
      Creates a new instance with all current environment variables as properties.
    • You can check if a variable exists in the usual ways of JS object properties (check if !== undefined, hasOwnProperty(), etc).
    • You can iterate over the variables as key-value pairs using a for...of loop. For example:
      var env = new Env();
      for (const [name, value] of env)
      console.log(name, "=", value);
      Or Java style:
      var it = new Env().iterator;
      while (it.hasNext()) {
      const [name, value] = it.next();
      console.log(name, "=", value);
      }
    • The variable names can be retrieved with names, the values with values and the name-value pairs with entries properties (entries is an Iteratable).
    • toString() and valueOf() return a JSON.stringify() representation of the full variables list with an indentation of 2 spaces.
  • As new Env(variableName) or new Env(variableName, defaultValue) creates a new instance of a named variable, optionally with a default value.
    Eg. new Env("PATH") or new Env("MYVAR", "Not defined yet").
    • isSet returns true/false based on if the variable exists in the environment;
    • value is the current value of the variable. If the variable doesn't exist in the current environment then defaultValue used in constructor is returned, or undefined if one wasn't set.
    • name is the variable name the instance was created with.
    • isValid indicates that a variable name was specified in the constructor and is not blank.
    • set(value) sets the value if the variable in the current environment (this will create it if it doesn't exist). Returns true/false to indicate success or failure. Note that setting a variable with an undefined value will remove it from the environment (same as unset());
    • unset() removes the variable from the current environment. Returns true/false to indicate success or failure.
    • toString() and valueOf() return the variable value (same as value()).
  • Static methods:
    • Env or Env.entries - Iteratable of name-value pairs of all current environment variables, eg. use in for ... of loop (see example above).
    • Env.iterator - Java style iterator of name-value pairs of all current environment variables (see example above).
    • Env.names - Array of all current environment variable names.
    • Env.values - Array of all current environment variable values.
    • Env.isSet(variableName) - returns true/false based on if the variableName exists in the environment.
    • Env.value(variableName, defaultValue) - returns the value of variableName or defaultValue if variable doesn't exist in the environment.
    • Env.set(variableName, value) - sets the value of variableName in the current environment (this will also create it if it doesn't exist). Returns true/false to indicate success or failure. Note that setting a variable with an undefined value will remove it from the environment (same as unset(variableName));
    • Env.unset(variableName) - removes variableName from the current environment. Returns true/false to indicate success or failure.

Public Member Functions

 Env ()
 
 Env (varName, defaultValue)
 
 Env (varName)
 

Constructor & Destructor Documentation

◆ Env() [1/3]

Env ( )

Creates a new instance with all current environment variables as properties.

◆ Env() [2/3]

Env (   varName,
  defaultValue 
)

Creates a new instance of a named variable with a default value.

The default is used if trying to get the value of this variable before it exists in the actual environment.

◆ Env() [3/3]

Env (   varName)

Creates a new instance of a named variable with an undefined default value.