Giter Club home page Giter Club logo

Comments (15)

xavierchow avatar xavierchow commented on June 6, 2024 1

Do you guys think it's a good idea to add trim for the asArray, because there might be spaces in the csv format env var for readability, e.g. COLOR="red, green, blue"

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

@henriklundgren nice idea. Would you be able to provide some pseudo/sample code for how you'd like this to work?

I think the current asJson would work for some scenarios, but asArray could ensure the result is an Array and not an Object. For example, this is currently possible:

var env = require('env-var');

// ARRAY is set to "[1, 2, 3, 4]"
// OBJECT is set to "{"name":"env-var"}"

var arr = env('ARRAY').asJson();
var obj = env('OBJECT').asJson();

But neither of these check that the parsed entry is truly and Object or an Array. So were you thinking this maybe?

var env = require('env-var');

// ARRAY is set to "[1, 2, 3, 4]"
// OBJECT is set to "{"name":"env-var"}"

var arr = env('ARRAY').asArray();
var obj = env('OBJECT').asObject();

env('OBJECT').asArray(); // throws an error since the json is actually an object

from env-var.

henriklundgren avatar henriklundgren commented on June 6, 2024

Atm I do;

function asArray(str: string): string[] {
  return str.split(',');
}
asArray(env('LIST').required().asString());
$ env LIST=red,blue,green npm start

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

@henriklundgren I see, would you be opposed to doing the following (adding array braces in the env var):

$ env LIST=["red","blue","green"] npm start

and then this module could expose an asArray function, and your code would be like so:

env('LIST').required().asArray()

If this doesn't work for you then perhaps a function named asCommaSeparatedArray could be added. Thoughts?

from env-var.

henriklundgren avatar henriklundgren commented on June 6, 2024

I believe csv's is more or less defacto standard to pass ArgumentList,
at least thats how I pass lists to the cli applications I use on daily basis.
I am reluctant to change that unless you are able to convince me.

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

I'm with you, just trying to figure out the best overall approach.

JSON in environment variables is a little weird for sure, and many CLI tools just denote arrays using comma or space characters like you say. So perhaps we add a new function to env-var like so:

# your original setup
$env LIST=red,blue,green npm start
var colours = env('LIST').required().asArrayWithDelimieter(','); 

or

var colours = env('LIST').required().fromArrayWithDelimieter(','); 
console.log(colours) // => ['red','blue','green']

This means you can use comma, or other chars to separate array elements. Thoughts?

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

Actually, just asArray(',') is fine, since we can also add asJsonArray() which will implement my original suggestion.

from env-var.

henriklundgren avatar henriklundgren commented on June 6, 2024

Appreciate the follow ups,

I guess that works,
though,
for completeness sake,
may I through out a third alternative,

asArray({
	delimiter: β€˜,’, // any string
	type: 'csv', // enum [β€˜csv’, β€˜json’]
})

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

@henriklundgren can you explain the need for type with an example of what the environment variable should be in the case of each?

My understanding of your example is that type would determine how env-var does the parsing, but for json there's no need for a delimiter, right?

I think we're almost there on figuring out a solid approach.

from env-var.

henriklundgren avatar henriklundgren commented on June 6, 2024

Yes, delimiter would loose its meaning if json.

I guess mainly my attempt to avoid polluting the method name scope,
with names that means the same on face value level.
I.e. take a list of values (any) and turn it in to a javascript list (array).

as[Array/List](type[, delimiter])
// or
as[Array/List]([type][, delimiter]) // all optional, default json
// or
"red,greeb,blue".asList(',')
['red','blue','green'].asArray() // json, wouldnt this be the same as asJson()?

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

Gotcha, I'm thinking we need 3 functions:

  • asJsonObject (This is not related to this issue, but I need it)
  • asJsonArray (Is somewhat related to this issue, will be needed in the future and fits into the JSON namespace)
  • asArray([delimiter])

I don't think "type" is necessary right now based on this discussion.

I can start on this later, unless you already have something done? πŸ˜„

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

Released in version 2.4. Let me know how it is @henriklundgren πŸ˜„

from env-var.

henriklundgren avatar henriklundgren commented on June 6, 2024

Thank you, works great, I can finally get rid of my little helper function. πŸŽ‰

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

Great! TypeScript definitions are also available πŸŽ‰

from env-var.

evanshortiss avatar evanshortiss commented on June 6, 2024

@xavierchow I'd considered that, but decided against it to keep things simple. Might be able to support it via an option though.

from env-var.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.