Skip to content

Return environment variable as a date object #13

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ Return as boolean. Allows true/false/1/0 as valid values.

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`.

### env.date(name, [fallback])

Return as date object. Will validate that what is given is a valid date format.

### env.multi({spec})

Return a list of environment variables based on a `spec`:
Expand Down
9 changes: 8 additions & 1 deletion lib/getenv.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var util = require("util");
var url = require("url");
var dayjs = require("dayjs");

var fallbacksDisabled = false;
var throwError = true;
Expand Down Expand Up @@ -69,7 +70,13 @@ var convert = {
return (value === '1');
}
},
url: url.parse
url: url.parse,
date: function(value) {
if (dayjs(value).isValid()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could find a way to do this without adding a comparably large dependency like dayjs (dayjs is much bigger than this library so it feels odd to add it as a first and only dependency).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes total sense. I'll look at how the dayjs library does the validation and see if its a simple replication or find some other way of validating!

return new Date(value);
}
throw new Error('GetEnv.NoDate: ' + value + ' is not a valid date.');
},
};

function converter(type) {
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"engines": {
"node": "*"
},
"dependencies": {},
"dependencies": {
"dayjs": "^1.8.12"
},
"devDependencies": {},
"keywords": [
"env",
Expand Down
33 changes: 33 additions & 0 deletions test/getenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ process.env.TEST_GETENV_BOOL_ARRAY = 'true, false, true';
process.env.TEST_GETENV_BOOL_ARRAY_INVALID1 = 'true, 1, true';
process.env.TEST_GETENV_BOOL_ARRAY_INVALID2 = 'true, 1.2, true';
process.env.TEST_GETENV_BOOL_ARRAY_INVALID3 = 'true, abc, true';
process.env.TEST_GETENV_DATE_VALID1 = '2019-04-08T12:51:21.137Z';
process.env.TEST_GETENV_DATE_VALID2 = 'Sun Mar 10 2019 12:51:21 GMT+0000 (GMT)'
process.env.TEST_GETENV_DATE_INVALID1 = '10 2019 12:51:21 GMT+0000 (GMT)'
process.env.TEST_GETENV_DATE_INVALID2 = '201-04-08T12:51:21.137Z';

process.env.TEST_GETENV_URL_1 = 'tcp://localhost:80';
process.env.TEST_GETENV_URL_2 = 'tcp://localhost:2993';
Expand Down Expand Up @@ -256,6 +260,35 @@ tests['getenv.boolish() invalid input'] = function() {
});
};

tests['getenv.date() valid input'] = function() {
var data = [{
varName: 'TEST_GETENV_DATE_VALID1',
expected: new Date('2019-04-08T12:51:21.137Z')
},
{
varName: 'TEST_GETENV_DATE_VALID2',
expected: new Date('Sun Mar 10 2019 12:51:21 GMT+0000 (GMT)')
}];

data.forEach(function(item) {
var dateVar = getenv.date(item.varName);
assert.deepEqual(dateVar, item.expected);
});
};


tests['getenv.date() invalid input'] = function() {
var data = [
{ varName: 'TEST_GETENV_DATE_INVALID1', varName: 'TEST_GETENV_DATE_INVALID2' }
];

data.forEach(function(item) {
assert.throws(function() {
var dateVar = getenv.date(item.varName);
});
});
};


tests['getenv.bool() nonexistent variable'] = function() {
assert.throws(function() {
Expand Down