Skip to content

Commit 5b02fee

Browse files
authored
Merge pull request #19 from sealsystems/master
Added separator parameter to array function
2 parents 98da607 + 450dc3f commit 5b02fee

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ Return as boolean. Only allows true/false as valid values.
8686

8787
Return as boolean. Allows true/false/1/0 as valid values.
8888

89-
### env.array(name, [type], [fallback])
89+
### env.array(name, [type], [fallback], [separator])
9090

91-
Split value of the environment variable at each comma and return the resulting array where each value has been typecast according to the `type` parameter. An array can be provided as `fallback`.
91+
Split value of the environment variable at each comma (default) and return the resulting array where each value has been typecast according to the `type` parameter. An array can be provided as `fallback` and a regular expression can be provided as `separator` in case commas don't fit.
9292

9393
### env.multi({spec})
9494

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const util = require('util');
21
const url = require('url');
32

43
let fallbacksDisabled = false;
@@ -95,20 +94,21 @@ Object.keys(convert).forEach(function(type) {
9594
getenv[type] = converter(type);
9695
});
9796

98-
getenv.array = function array(varName, type, fallback) {
97+
getenv.array = function array(varName, type, fallback, separator) {
9998
type = type || 'string';
99+
separator = separator || /\s*,\s*/;
100100
if (Object.keys(convert).indexOf(type) === -1) {
101101
throw new Error('GetEnv.ArrayUndefinedType: Unknown array type ' + type);
102102
}
103103
const value = _value(varName, fallback);
104-
return value.split(/\s*,\s*/).map(convert[type]);
104+
return value.split(separator).map(convert[type]);
105105
};
106106

107107
getenv.multi = function multi(spec) {
108108
const result = {};
109109
for (let key in spec) {
110110
const value = spec[key];
111-
if (util.isArray(value)) {
111+
if (Array.isArray(value)) {
112112
// default value & typecast
113113
switch (value.length) {
114114
case 1: // no default value

test/getenv.js

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ process.env.TEST_GETENV_STRING_ARRAY2 = 'one, two ,three , four';
2222
process.env.TEST_GETENV_STRING_ARRAY3 = 'one, two,';
2323
process.env.TEST_GETENV_STRING_ARRAY4 = ' ';
2424
process.env.TEST_GETENV_STRING_ARRAY5 = 'one;two:three,four';
25+
process.env.TEST_GETENV_STRING_ARRAY6 = 'one two ';
2526
process.env.TEST_GETENV_INT_ARRAY = '1,2, 3';
27+
process.env.TEST_GETENV_INT_ARRAY2 = '1 2 3';
2628
process.env.TEST_GETENV_INT_ARRAY_INVALID1 = '1, 2.2, 3';
2729
process.env.TEST_GETENV_INT_ARRAY_INVALID2 = '1, true, 3';
2830
process.env.TEST_GETENV_INT_ARRAY_INVALID3 = '1, abc, 3';
2931
process.env.TEST_GETENV_FLOAT_ARRAY = '1.9,2, 3e5';
32+
process.env.TEST_GETENV_FLOAT_ARRAY2 = '1.9 2 3e5';
3033
process.env.TEST_GETENV_FLOAT_ARRAY_INVALID1 = '1.9,true, 3e5';
3134
process.env.TEST_GETENV_FLOAT_ARRAY_INVALID2 = '1.9, abc, 3e5';
3235
process.env.TEST_GETENV_FLOAT_ARRAY_INVALID3 = '1.9, Infinity, 3e5';
3336
process.env.TEST_GETENV_BOOL_ARRAY = 'true, false, true';
37+
process.env.TEST_GETENV_BOOL_ARRAY2 = 'true false true';
3438
process.env.TEST_GETENV_BOOL_ARRAY_INVALID1 = 'true, 1, true';
3539
process.env.TEST_GETENV_BOOL_ARRAY_INVALID2 = 'true, 1.2, true';
3640
process.env.TEST_GETENV_BOOL_ARRAY_INVALID3 = 'true, abc, true';
@@ -303,7 +307,37 @@ tests['getenv.array() valid string (default) input'] = function() {
303307

304308
data.forEach(function(item) {
305309
const arrayVar = getenv.array(item.varName);
306-
assert.deepEqual(arrayVar, item.expected);
310+
assert.deepStrictEqual(arrayVar, item.expected);
311+
});
312+
};
313+
314+
tests['getenv.array() valid inputs split by separator'] = function() {
315+
const data = [
316+
{
317+
varName: 'TEST_GETENV_STRING_ARRAY6',
318+
type: 'string',
319+
expected: ['one', 'two', ''],
320+
},
321+
{
322+
varName: 'TEST_GETENV_INT_ARRAY2',
323+
type: 'int',
324+
expected: [1, 2, 3],
325+
},
326+
{
327+
varName: 'TEST_GETENV_FLOAT_ARRAY2',
328+
type: 'float',
329+
expected: [1.9, 2, 3e5],
330+
},
331+
{
332+
varName: 'TEST_GETENV_BOOL_ARRAY2',
333+
type: 'boolish',
334+
expected: [true, false, true],
335+
},
336+
];
337+
338+
data.forEach(function(item) {
339+
const arrayVar = getenv.array(item.varName, item.type, [], /\s+/);
340+
assert.deepStrictEqual(arrayVar, item.expected);
307341
});
308342
};
309343

@@ -411,7 +445,7 @@ tests['getenv.array() nonexistent variable'] = function() {
411445
tests['getenv.array() nonexistent variable with fallback'] = function() {
412446
const expect = ['A', 'B', 'C'];
413447
const arrayVar = getenv.array('TEST_GETENV_NONEXISTENT', 'string', expect);
414-
assert.deepEqual(arrayVar, expect);
448+
assert.deepStrictEqual(arrayVar, expect);
415449
};
416450

417451
tests['getenv.array() nonexistent type'] = function() {
@@ -428,7 +462,7 @@ tests['getenv.multi([string]) multiple env vars'] = function() {
428462
const expect = {
429463
foo: process.env.TEST_GETENV_STRING,
430464
};
431-
assert.deepEqual(expect, config);
465+
assert.deepStrictEqual(expect, config);
432466
};
433467

434468
tests['getenv([string]) multiple env vars shortcut'] = function() {
@@ -439,7 +473,7 @@ tests['getenv([string]) multiple env vars shortcut'] = function() {
439473
const expect = {
440474
foo: process.env.TEST_GETENV_STRING,
441475
};
442-
assert.deepEqual(expect, config);
476+
assert.deepStrictEqual(expect, config);
443477
};
444478

445479
tests['getenv.multi([string/throw]) multiple env vars'] = function() {
@@ -459,7 +493,7 @@ tests['getenv.multi([string/typecast]) multiple env vars'] = function() {
459493
const expect = {
460494
foo: process.env.TEST_GETENV_STRING,
461495
};
462-
assert.deepEqual(expect, config);
496+
assert.deepStrictEqual(expect, config);
463497
};
464498

465499
tests['getenv.multi([string/typecast/defaultval]) multiple env vars'] = function() {
@@ -470,7 +504,7 @@ tests['getenv.multi([string/typecast/defaultval]) multiple env vars'] = function
470504
const expect = {
471505
foo: 'default',
472506
};
473-
assert.deepEqual(expect, config);
507+
assert.deepStrictEqual(expect, config);
474508
};
475509

476510
tests['getenv.multi([string/typecast/throw]) multiple env vars'] = function() {
@@ -490,7 +524,7 @@ tests['getenv.multi([string/defaultval]) multiple env vars'] = function() {
490524
const expect = {
491525
foo: process.env.TEST_GETENV_STRING,
492526
};
493-
assert.deepEqual(expect, config);
527+
assert.deepStrictEqual(expect, config);
494528
};
495529

496530
tests['getenv.multi([string/defaultval/throw]) multiple env vars'] = function() {
@@ -501,7 +535,7 @@ tests['getenv.multi([string/defaultval/throw]) multiple env vars'] = function()
501535
const expect = {
502536
foo: 'default',
503537
};
504-
assert.deepEqual(expect, config);
538+
assert.deepStrictEqual(expect, config);
505539
};
506540

507541
tests['getenv.multi([string/single]) multiple env vars'] = function() {
@@ -512,7 +546,7 @@ tests['getenv.multi([string/single]) multiple env vars'] = function() {
512546
const expect = {
513547
foo: process.env.TEST_GETENV_STRING,
514548
};
515-
assert.deepEqual(expect, config);
549+
assert.deepStrictEqual(expect, config);
516550
};
517551

518552
tests['getenv.multi([string/single/throw]) multiple env vars'] = function() {
@@ -539,7 +573,7 @@ tests['getenv.url() valid input'] = function() {
539573
h[key] = parsed[key];
540574
return h;
541575
}, {});
542-
assert.deepEqual(actual, expectation);
576+
assert.deepStrictEqual(actual, expectation);
543577
});
544578
};
545579

0 commit comments

Comments
 (0)