Skip to content

Commit

Permalink
fix: empty string defaults (#106)
Browse files Browse the repository at this point in the history
* fix: support empty string as a default

* fix: de-duplicate contributing info

* chore: ignore ts generated files

* chore: bump version and add patch note
  • Loading branch information
evanshortiss authored Feb 29, 2020
1 parent b5e6adc commit 098aa3d
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
node_modules
*.log
example/typescript.js*
coverage
.nyc_output
package-lock.json
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## 6.0.2 (29/02/20)
* Fix `default()` so that it correctly returns an empty string value if provided.
* README improvement by @joh-klein for positive/negative number parsing rules.

## 6.0.1 (12/02/20)
* Fix typings for the `default(value)` function.

Expand Down
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ There are just a few things to be aware of when making a contribution. If you ha

### Adding an Accessor

If you want to add a new accessor it's pretty straightforward, and an example is outlined below - just make sure it's a reasonably generic use case!
If you want to add a new accessor it's pretty straightforward, and an example is outlined below - just make sure it's a reasonably generic use case!

If you want to add a new global accessor, it's easy. Add a file to
`lib/accessors`, with the name of the type e.g add a file named `number-zero.js`
into that folder and populate it with code following this structure:
Start by adding a file to `lib/accessors`, with the name of the type e.g add a
file named `number-zero.js` into that folder and populate it with code
following this structure:

```js
/**
* Validate that the environment value is an integer and equals zero.
* This is a strange example, but hopefully demonstrates the idea.
* @param {String} environmentValue this is the string from process.env
*/
module.exports = function numberZero (environmentValue) {
Expand Down
46 changes: 4 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,55 +522,17 @@ env.get('ADMIN').asEmail()
```

## Contributing
Contributions are welcomed. If you'd like to discuss an idea open an issue, or a
PR with an initial implementation.

If you want to add a new global accessor add a file to `lib/accessors`, with
the name of the type e.g add a file named `number-zero.js` into that folder
and populate it with code following this structure:

```js
/**
* Validate that the environment value is an integer and equals zero.
* This is a strange example, but hopefully demonstrates the idea.
* @param {String} environmentValue this is the string from process.env
*/
module.exports = function numberZero (environmentValue) {

// Your custom code should go here...below code is an example

const val = parseInt(environmentValue)

if (val === 0) {
return ret;
} else {
throw new Error('should be zero')
}
}
```

Next update the `accessors` Object in `getVariableAccessors()` in
`lib/variable.js` to include your new module. The naming convention should be of
the format "asTypeSubtype", so for our `number-zero` example it would be done
like so:

```js
asNumberZero: generateAccessor(container, varName, defValue, require('./accessors/number-zero')),
```

Once you've done that, add some unit tests and use it like so:

```js
// Uses your new function to ensure the SOME_NUMBER is the integer 0
env.get('SOME_NUMBER').asNumberZero()
```
Contributions are welcomed and discussed in the `CONTRIBUTING.md` file in this
repo. If you'd like to discuss an idea open an issue, or a PR with an initial
implementation.

## Contributors
* @caccialdo
* @evanshortiss
* @gabrieloczkowski
* @hhravn
* @itavy
* @joh-klein
* @MikeyBurkman
* @pepakriz
* @rmblstrp
Expand Down
2 changes: 1 addition & 1 deletion lib/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function getVariableAccessors (container, varName, extraAccesso
if (typeof defValue === 'undefined' && isRequired) {
// Var is not set, nor is a default. Throw an error
raiseError(undefined, 'is a required variable, but it was not set')
} else if (defValue) {
} else if (typeof defValue !== 'undefined') {
value = defValue
} else {
// return undefined since variable is not required and
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "env-var",
"version": "6.0.1",
"version": "6.0.2",
"description": "Verification, sanatization, and type coercion for environment variables in Node.js",
"main": "env-var.js",
"typings": "env-var.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ describe('env-var', function () {
})

describe('default values', function () {
it('should return the default empty string value', () => {
const ret = mod.get('XXX_NOT_DEFINED').default('').asString()

expect(ret).to.equal('')
})

it('should return the default', function () {
const ret = mod.get('XXX_NOT_DEFINED').default('default').asString()

Expand Down

0 comments on commit 098aa3d

Please sign in to comment.