Skip to content

Commit

Permalink
Added expanding of \n to newlines if value is double quoted
Browse files Browse the repository at this point in the history
  • Loading branch information
calleluks committed Aug 23, 2013
1 parent 733642f commit 599144a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env
@@ -1,3 +1,6 @@
BASIC=basic
SINGLE_QUOTES='single_quotes'
DOUBLE_QUOTES="double_quotes"
EXPAND_NEWLINES="expand\nnewlines"
DONT_EXPAND_NEWLINES_1=dontexpand\nnewlines
DONT_EXPAND_NEWLINES_2='dontexpand\nnewlines'
5 changes: 5 additions & 0 deletions lib/main.js
Expand Up @@ -24,6 +24,11 @@ function dotenv() {
var key_value_array = lines[i].split("=");
var key = key_value_array[0].trim();
var value = key_value_array[1].trim();

if (value.charAt(0) === '"' && value.charAt(value.length-1) == '"') {
value = value.replace(/\\n/gm, "\n");
}

value = value.replace(/['"]/gm, '');

process.env[key] = value;
Expand Down
5 changes: 5 additions & 0 deletions test/main.js
Expand Up @@ -28,5 +28,10 @@ describe('dotenv', function() {
it('sets single quotes environment variables', function() {
process.env.SINGLE_QUOTES.should.eql("single_quotes");
});
it('expands newlines but only if double quoted', function() {
process.env.EXPAND_NEWLINES.should.eql("expand\nnewlines");
process.env.DONT_EXPAND_NEWLINES_1.should.eql("dontexpand\\nnewlines");
process.env.DONT_EXPAND_NEWLINES_2.should.eql("dontexpand\\nnewlines");
});
});
});

0 comments on commit 599144a

Please sign in to comment.