v1.2.0.1-beta1
|
The Env object provides access to the system environment variables.
It has various modes of operation, depending on how it is created/used:
new Env()
creates a new instance with all current environment variables as properties.hasOwnProperty()
, etc).for...of
loop. For example: Or Java style: 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.new Env(variableName)
or new Env(variableName, defaultValue)
creates a new instance of a named variable, optionally with a default value.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()
).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) | |
Env | ( | ) |
Creates a new instance with all current environment variables as properties.
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 | ( | varName | ) |
Creates a new instance of a named variable with an undefined default value.