Hi,
Is it possible to convert "true" and "false" string values to boolean, so that on JS we can do something like if (Config.DEBUG) instead of if (Config.DEBUG == 'true').
I've tried the following approach, but it does not work.
key, val = line.split("=", 2)
if line.strip.empty? or line.start_with?('#')
h
else
key, val = line.split("=", 2)
if %w(true false).include?(val)
val = val == 'true'
end
h.merge!(key => val)
end
Thanks.
I'd encourage you to do that in JS land! Maybe define your own Config module, that sits between your app and this package, casting values accordingly?
Long term I'd love to support different types, but since .env doesn't have it we'd have to either use another format to define the env (most likely json) or allow apps to somehow configure this package telling it what config it needs and what are their types. For a reference this is exactly what we've done in Pliny, a Ruby framework, so I think this could work well!
Let me know if you do this in your app or want to take a stab at a pull here! Closing in the meantime.
Hi @pedro,
Thanks for your help. I made the cast with JS, as you suggested. The idea of supporting different types is really cool.
In case anyone else wants to implement this, I've found an easy solution that works well. I'm using https://github.com/niftylettuce/dotenv-parse-variables to parse the variables provided by react-native-config.
Here's an example .env file:
STRING="https://www.google.com"
EMPTY_STRING=
BOOLEAN=true
NUMBER=5
Here's the code:
// env.js
import Config from 'react-native-config';
import dotenvParseVariables from 'dotenv-parse-variables';
const ENV = {
STRING: Config.STRING,
EMPTY_STRING: Config.EMPTY_STRING,
BOOLEAN: Config.BOOLEAN,
NUMBER: Config.NUMBER,
};
dotenvParseVariables(ENV);
export default ENV;
The ENV object using the values directly from react-native-config (before parsing using dotenv-parse-variables) looks like this:
{
"STRING": "https://www.google.com",
"EMPTY_STRING": "",
"BOOLEAN": "true",
"NUMBER": "5"
}
And after parsing using dotenv-parse-variables, it looks like this:
{
"STRING": "https://www.google.com",
"EMPTY_STRING": "",
"BOOLEAN": true,
"NUMBER": 5
}
Most helpful comment
In case anyone else wants to implement this, I've found an easy solution that works well. I'm using https://github.com/niftylettuce/dotenv-parse-variables to parse the variables provided by
react-native-config.Here's an example
.envfile:Here's the code:
The
ENVobject using the values directly fromreact-native-config(before parsing usingdotenv-parse-variables) looks like this:And after parsing using
dotenv-parse-variables, it looks like this: