Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert "true" and "false" string values to boolean #49

Closed
rpereira opened this issue Nov 11, 2016 · 3 comments
Closed

Convert "true" and "false" string values to boolean #49

rpereira opened this issue Nov 11, 2016 · 3 comments

Comments

@rpereira
Copy link

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.

@pedro
Copy link
Contributor

pedro commented Nov 11, 2016

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.

@pedro pedro closed this as completed Nov 11, 2016
@rpereira
Copy link
Author

Hi @pedro,

Thanks for your help. I made the cast with JS, as you suggested. The idea of supporting different types is really cool.

@jordanmkoncz
Copy link

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants