Skip to content
Merged
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
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: ['master']
pull_request:
branches: ['master']

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ if (Math.random() < abTestRatio) {
}

const keywords = getenv.array('KEYWORDS');
keywords.forEach(function(keyword) {
keywords.forEach(function (keyword) {
// console.log(keyword);
});

const primes = getenv.array('PRIMES', 'int');
primes.forEach(function(prime) {
primes.forEach(function (prime) {
// console.log(prime, typeof prime);
});
```
Expand Down Expand Up @@ -149,6 +149,10 @@ console.log(getenv('RANDOM'));

## Changelog

### v1.1.0

- Add separator option to `env.array()` (#19)

### v1.0.0

- Drop support for Node.js older than 6.
Expand Down
25 changes: 13 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ function _value(varName, fallback) {
}

const convert = {
string: function(value) {
string: function (value) {
return '' + value;
},
int: function(value) {
int: function (value) {
const isInt = value.match(/^-?\d+$/);
if (!isInt) {
throw new Error('GetEnv.NoInteger: ' + value + ' is not an integer.');
}

return +value;
},
float: function(value) {
float: function (value) {
const isInfinity = +value === Infinity || +value === -Infinity;
if (isInfinity) {
throw new Error('GetEnv.Infinity: ' + value + ' is set to +/-Infinity.');
Expand All @@ -52,15 +52,16 @@ const convert = {

return +value;
},
bool: function(value) {
const isBool = (value || '').toLowerCase() === 'true' || (value || '').toLowerCase() === 'false';
bool: function (value) {
const isBool =
(value || '').toLowerCase() === 'true' || (value || '').toLowerCase() === 'false';
if (!isBool) {
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
}

return (value || '').toLowerCase() === 'true';
},
boolish: function(value) {
boolish: function (value) {
try {
return convert.bool(value);
} catch (err) {
Expand All @@ -76,7 +77,7 @@ const convert = {
};

function converter(type) {
return function(varName, fallback) {
return function (varName, fallback) {
if (typeof varName == 'string') {
// default
const value = _value(varName, fallback);
Expand All @@ -90,7 +91,7 @@ function converter(type) {

const getenv = converter('string');

Object.keys(convert).forEach(function(type) {
Object.keys(convert).forEach(function (type) {
getenv[type] = converter(type);
});

Expand Down Expand Up @@ -131,19 +132,19 @@ getenv.multi = function multi(spec) {
return result;
};

getenv.disableFallbacks = function() {
getenv.disableFallbacks = function () {
fallbacksDisabled = true;
};

getenv.enableFallbacks = function() {
getenv.enableFallbacks = function () {
fallbacksDisabled = false;
};

getenv.disableErrors = function() {
getenv.disableErrors = function () {
throwError = false;
};

getenv.enableErrors = function() {
getenv.enableErrors = function () {
throwError = true;
};

Expand Down
35 changes: 35 additions & 0 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
},
"main": "index.js",
"scripts": {
"prettier": "prettier --write *.{js,md} **/*.js",
"test": "bash -ec 'for F in test/*.js; do echo \"$F\": ; node $F; done;'"
"prettier": "prettier --check .",
"unit": "bash -ec 'for F in test/*.js; do echo \"$F\": ; node $F; done;'",
"test": "npm run prettier && npm run unit"
},
"engines": {
"node": ">=6"
},
"dependencies": {},
"devDependencies": {
"prettier": "^1.18.2"
"prettier": "^3.5.3"
},
"keywords": [
"env",
Expand Down
8 changes: 4 additions & 4 deletions test/disableErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ const getenv = require('../index');

const tests = {};

tests['getenv.disableErrors() should disable any errors'] = function() {
tests['getenv.disableErrors() should disable any errors'] = function () {
getenv.disableErrors();
assert.strictEqual(getenv.string('url', 'http://localhost'), 'http://localhost');
assert(getenv.string('url'), undefined);
};

tests['getenv.enableErrors() should enable errors'] = function() {
tests['getenv.enableErrors() should enable errors'] = function () {
getenv.enableErrors();
assert.throws(function() {
assert.throws(function () {
getenv.string('url');
});
assert.strictEqual(getenv.string('url', 'http://localhost'), 'http://localhost');
};

Object.keys(tests).forEach(function(key) {
Object.keys(tests).forEach(function (key) {
console.log('Test: %s', key);
tests[key]();
});
6 changes: 3 additions & 3 deletions test/fallbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const getenv = require('../index');
const tests = {};

// order dependent test
tests['getenv.disableFallbacks() makes relying on fallbacks an error'] = function() {
tests['getenv.disableFallbacks() makes relying on fallbacks an error'] = function () {
getenv.disableFallbacks();
assert.throws(function() {
assert.throws(function () {
getenv.string('url', 'http://localhost');
});
getenv.enableFallbacks();
};

Object.keys(tests).forEach(function(key) {
Object.keys(tests).forEach(function (key) {
console.log('Test: %s', key);
tests[key]();
});
Loading