Skip to content

Commit eb40fe6

Browse files
committed
Merge pull request #3 from timruffles/add-url
add url() support
2 parents 770cdd7 + 08641c9 commit eb40fe6

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ var config = getenv.multi({
9898

9999
```
100100

101+
### env.url(name, [fallback])
102+
103+
Return a parsed URL as per Node's `require("url").parse`. N.B `url` doesn't validate URLs, so be sure it includes a protocol or you'll get deeply weird results.
104+
105+
```javascript
106+
var serviceUrl = getenv.url('SERVICE_URL');
107+
108+
serviceUrl.port; // parsed port number
109+
```
110+
101111
### env.disableFallbacks()
102112

103113
Disallows fallbacks in environments where you don't want to rely on brittle development defaults (e.g production, integration testing). For example, to disable fallbacks if we indicate production via `NODE_ENV`:
@@ -110,6 +120,9 @@ if (process.env.NODE_ENV === 'production') {
110120

111121
## Changelog
112122

123+
### v0.5.0
124+
- Add getenv.url() support.
125+
113126
### v0.4.0
114127
- Add getenv.disableFallbacks() support.
115128

@@ -128,7 +141,7 @@ if (process.env.NODE_ENV === 'production') {
128141
- Christoph Tavan <[email protected]>
129142
- Jonas Dohse <[email protected]>
130143
- Jan Lehnardt (@janl): `getenv.multi()` support.
131-
- Tim Ruffles <[email protected]>: `disableFallbacks()`
144+
- Tim Ruffles <[email protected]>: `disableFallbacks()`, `url()`
132145

133146
## License
134147

lib/getenv.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var util = require("util");
2+
var url = require("url");
23

34
var fallbacksDisabled = false;
45

@@ -50,7 +51,8 @@ var convert = {
5051
}
5152

5253
return (value === 'true');
53-
}
54+
},
55+
url: url.parse
5456
};
5557

5658
function converter(type) {

test/getenv.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ process.env.TEST_GETENV_BOOL_ARRAY_INVALID1 = 'true, 1, true';
3535
process.env.TEST_GETENV_BOOL_ARRAY_INVALID2 = 'true, 1.2, true';
3636
process.env.TEST_GETENV_BOOL_ARRAY_INVALID3 = 'true, abc, true';
3737

38+
process.env.TEST_GETENV_URL_1 = "tcp://localhost:80";
39+
process.env.TEST_GETENV_URL_2 = "tcp://localhost:2993";
40+
process.env.TEST_GETENV_URL_3 = "http://192.162.22.11:2993";
41+
3842
var tests = {};
3943

4044
tests['getenv() same as getenv.string()'] = function() {
@@ -480,6 +484,26 @@ tests['getenv.multi([string/single/throw]) multiple env vars'] = function() {
480484
});
481485
};
482486

487+
tests['getenv.url() valid input'] = function() {
488+
489+
var expected = [
490+
{hostname: "localhost", port: "80", protocol: "tcp:"},
491+
{hostname: "localhost", port: "2993", protocol: "tcp:"},
492+
{hostname: "192.162.22.11", port: "2993", protocol: "http:"},
493+
];
494+
495+
var prefix = "TEST_GETENV_URL_";
496+
497+
expected.forEach(function(expectation, i) {
498+
var parsed = getenv.url(prefix + (i + 1));
499+
var actual = Object.keys(expectation).reduce(function(h, key) {
500+
h[key] = parsed[key];
501+
return h;
502+
}, {});
503+
assert.deepEqual(actual, expectation);
504+
});
505+
};
506+
483507

484508
Object.keys(tests).forEach(function(key) {
485509
console.log('Test: %s', key);

0 commit comments

Comments
 (0)