Comments (15)
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.
@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.
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.
@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.
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.
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.
Actually, just asArray(',')
is fine, since we can also add asJsonArray()
which will implement my original suggestion.
from env-var.
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.
@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.
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.
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.
Released in version 2.4. Let me know how it is @henriklundgren π
from env-var.
Thank you, works great, I can finally get rid of my little helper function. π
from env-var.
Great! TypeScript definitions are also available π
from env-var.
@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)
- Feature request: asRegExp() HOT 4
- Add 'example/README.md' HOT 4
- Add description() function HOT 3
- The number accessors are too permissive HOT 3
- asEnum should accept readonly T[] HOT 3
- add asMailString() HOT 4
- Configure default behavior / Support dotenv-defaults HOT 5
- Duplicate identifier causing broken build with typescript. HOT 3
- Duplicate identifier 'EnvVarError'. HOT 4
- Deno Support HOT 3
- TypeError: env.get is not a function HOT 3
- Don't default export `from(process.env)` for environments that don't have `process` HOT 6
- v8.0.0 HOT 1
- Custom validation extension/custom accessor before `as` check. HOT 2
- Client side. HOT 6
- Required() in React HOT 1
- Allow asUrlString (and maybe other checks) to be optional HOT 2
- 7.4.0 is breaking create-react-app projects that worked in prior versions HOT 11
- When testing, env variables can't be change from the test HOT 3
- Bug with `default` and `asEnum` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from env-var.