Skip to content

Commit

Permalink
Merge e4c3564 into 1945a05
Browse files Browse the repository at this point in the history
  • Loading branch information
TheColorRed committed Mar 28, 2018
2 parents 1945a05 + e4c3564 commit aa6d945
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
48 changes: 44 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs')
const path = require('path')

/*
/**
* Parses a string or buffer into an object
* @param {(string|Buffer)} src - source to be parsed
* @returns {Object} keys and values from src
Expand All @@ -20,6 +20,7 @@ function parse (src) {
const key = keyValueArr[1]

// default undefined or missing values to empty string
/** @type {string|number|boolean} */
let value = keyValueArr[2] || ''

// expand newlines in quoted values
Expand All @@ -31,17 +32,25 @@ function parse (src) {
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

if (value.match(/^-?(\d|\.)+$/) && value.split('.').length <= 2) {
// convert numeric strings to numbers
value = parseFloat(value)
} else if (value.match(/^(true|false)$/i)) {
// convert true/false strings to booleans
value = value === 'true'
}

obj[key] = value
}
})

return obj
}

/*
/**
* Main entry point into dotenv. Allows configuration before loading .env
* @param {Object} options - options for parsing .env file
* @param {string} [options.path=.env] - path to .env file
* @param {string} [options.path='.env'] - path to .env file
* @param {string} [options.encoding=utf8] - encoding of .env file
* @returns {Object} parsed object or error
*/
Expand All @@ -68,7 +77,38 @@ function config (options) {
}
})

return { parsed }
return {
parsed,
/**
* Gets a value from the parsed object, if it doesn't exist return the default
*
* @param {string} key The environment key
* @param {any} [defaultValue=''] A fallback value to return
*/
get: function (key, defaultValue) {
if (typeof defaultValue === 'undefined') defaultValue = ''
return typeof this.parsed[key] !== 'undefined' ? this.parsed[key] : defaultValue
},
/**
* Adds a key to the parsed object only if it doesn't exist
*
* @param {string} key The key to add
* @param {any} value The value of the key
*/
add: function (key, value) {
typeof this.parsed[key] === 'undefined' && this.set(key, value)
},
/**
* Sets the value of a key, the key is created if it doesn't exist
*
* @param {string} key The key to add
* @param {any} value The value of the key
*/
set: function (key, value) {
process.env[key] = value
this.parsed[key] = value
}
}
} catch (e) {
return { error: e }
}
Expand Down
10 changes: 10 additions & 0 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ RETAIN_INNER_QUOTES={"foo": "bar"}
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
INCLUDE_SPACE=some spaced out string
USERNAME="therealnerdybeast@example.tld"
NUMBER1=0.1234
NUMBER2=.01234
NUMBER3=-0.1234
NUMBER4=-.1234
NUMBER5=1234
NUMBER6=1-234
NUMBER7=127.0.0.1
BOOL1='true'
BOOL2=FALSE
BOOL3=!true
34 changes: 30 additions & 4 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('dotenv', function () {
done()
})

const mockParseResponse = {test: 'val'}
const mockParseResponse = { test: 'val' }

describe('config', function () {
var readFileSyncStub
Expand All @@ -37,15 +37,15 @@ describe('dotenv', function () {

it('takes option for path', function (done) {
var testPath = 'test/.env'
dotenv.config({path: testPath})
dotenv.config({ path: testPath })

readFileSyncStub.args[0][0].should.eql(testPath)
done()
})

it('takes option for encoding', function (done) {
var testEncoding = 'base64'
dotenv.config({encoding: testEncoding})
dotenv.config({ encoding: testEncoding })

readFileSyncStub.args[0][1].should.have.property('encoding', testEncoding)
done()
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('dotenv', function () {
var parsed
before(function (done) {
process.env.TEST = 'test'
parsed = dotenv.parse(fs.readFileSync('test/.env', {encoding: 'utf8'}))
parsed = dotenv.parse(fs.readFileSync('test/.env', { encoding: 'utf8' }))
done()
})

Expand Down Expand Up @@ -193,5 +193,31 @@ describe('dotenv', function () {
parsed.should.have.property('USERNAME', 'therealnerdybeast@example.tld')
done()
})

it('is an integer', function (done) {
parsed.should.have.property('NUMBER1').and.be.a.Number()
parsed.should.have.property('NUMBER2').and.be.a.Number()
parsed.should.have.property('NUMBER3').and.be.a.Number()
parsed.should.have.property('NUMBER4').and.be.a.Number()
parsed.should.have.property('NUMBER5').and.be.a.Number()
done()
})

it('is not an integer', function (done) {
parsed.should.have.property('NUMBER6').and.not.be.a.Number()
parsed.should.have.property('NUMBER7').and.not.be.a.Number()
done()
})

it('is an boolean', function (done) {
parsed.should.have.property('BOOL1').and.be.a.Boolean()
parsed.should.have.property('BOOL2').and.be.a.Boolean()
done()
})

it('is not an boolean', function (done) {
parsed.should.have.property('BOOL3').and.not.be.a.Boolean()
done()
})
})
})

0 comments on commit aa6d945

Please sign in to comment.