Skip to content

Commit b28bdce

Browse files
committed
Use const/let instead of var
1 parent a70b09e commit b28bdce

File tree

5 files changed

+115
-116
lines changed

5 files changed

+115
-116
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ export PRIMES="2,3,5,7"
2828
Get and use them:
2929

3030
```javascript
31-
var getenv = require('getenv');
31+
const getenv = require('getenv');
3232

33-
var host = getenv('HTTP_HOST'); // same as getenv.string('HTTP_HOST');
34-
var port = getenv.int('HTTP_PORT');
35-
var start = getenv.bool('HTTP_START');
33+
const host = getenv('HTTP_HOST'); // same as getenv.string('HTTP_HOST');
34+
const port = getenv.int('HTTP_PORT');
35+
const start = getenv.bool('HTTP_START');
3636

3737
if (start === true) {
38-
// var server = http.createServer();
38+
// const server = http.createServer();
3939
// server.listen(port, host);
4040
}
4141

42-
var abTestRatio = getenv.float('AB_TEST_RATIO');
42+
const abTestRatio = getenv.float('AB_TEST_RATIO');
4343

4444
if (Math.random() < abTestRatio) {
4545
// test A
4646
} else {
4747
// test B
4848
}
4949

50-
var keywords = getenv.array('KEYWORDS');
50+
const keywords = getenv.array('KEYWORDS');
5151
keywords.forEach(function(keyword) {
5252
// console.log(keyword);
5353
});
5454

55-
var primes = getenv.array('PRIMES', 'int');
55+
const primes = getenv.array('PRIMES', 'int');
5656
primes.forEach(function(prime) {
5757
// console.log(prime, typeof prime);
5858
});
@@ -95,7 +95,7 @@ Split value of the environment variable at each comma and return the resulting a
9595
Return a list of environment variables based on a `spec`:
9696

9797
```javascript
98-
var config = getenv.multi({
98+
const config = getenv.multi({
9999
foo: 'FOO', // throws if FOO doesn't exist
100100
bar: ['BAR', 'defaultval'], // set a default value
101101
baz: ['BAZ', 'defaultval', 'string'], // parse into type
@@ -108,7 +108,7 @@ var config = getenv.multi({
108108
Return a parsed URL as per Node's `require("url").parse`. N.B `url` doesn't validate URLs, so be sure it includes a protocol or you'll get deeply weird results.
109109

110110
```javascript
111-
var serviceUrl = getenv.url('SERVICE_URL');
111+
const serviceUrl = getenv.url('SERVICE_URL');
112112

113113
serviceUrl.port; // parsed port number
114114
```

index.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
var util = require('util');
2-
var url = require('url');
1+
const util = require('util');
2+
const url = require('url');
33

4-
var fallbacksDisabled = false;
5-
var throwError = true;
4+
let fallbacksDisabled = false;
5+
let throwError = true;
66

77
function _value(varName, fallback) {
8-
var value = process.env[varName];
8+
const value = process.env[varName];
99
if (value === undefined) {
1010
if (fallback === undefined && !throwError) {
1111
return value;
@@ -28,33 +28,33 @@ function _value(varName, fallback) {
2828
return value;
2929
}
3030

31-
var convert = {
31+
const convert = {
3232
string: function(value) {
3333
return '' + value;
3434
},
3535
int: function(value) {
36-
var isInt = value.match(/^-?\d+$/);
36+
const isInt = value.match(/^-?\d+$/);
3737
if (!isInt) {
3838
throw new Error('GetEnv.NoInteger: ' + value + ' is not an integer.');
3939
}
4040

4141
return +value;
4242
},
4343
float: function(value) {
44-
var isInfinity = +value === Infinity || +value === -Infinity;
44+
const isInfinity = +value === Infinity || +value === -Infinity;
4545
if (isInfinity) {
4646
throw new Error('GetEnv.Infinity: ' + value + ' is set to +/-Infinity.');
4747
}
4848

49-
var isFloat = !(isNaN(value) || value === '');
49+
const isFloat = !(isNaN(value) || value === '');
5050
if (!isFloat) {
5151
throw new Error('GetEnv.NoFloat: ' + value + ' is not a number.');
5252
}
5353

5454
return +value;
5555
},
5656
bool: function(value) {
57-
var isBool = value === 'true' || value === 'false';
57+
const isBool = value === 'true' || value === 'false';
5858
if (!isBool) {
5959
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
6060
}
@@ -65,7 +65,7 @@ var convert = {
6565
try {
6666
return convert.bool(value);
6767
} catch (err) {
68-
var isBool = value === '1' || value === '0';
68+
const isBool = value === '1' || value === '0';
6969
if (!isBool) {
7070
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
7171
}
@@ -80,7 +80,7 @@ function converter(type) {
8080
return function(varName, fallback) {
8181
if (typeof varName == 'string') {
8282
// default
83-
var value = _value(varName, fallback);
83+
const value = _value(varName, fallback);
8484
return convert[type](value);
8585
} else {
8686
// multibert!
@@ -89,7 +89,7 @@ function converter(type) {
8989
};
9090
}
9191

92-
var getenv = converter('string');
92+
const getenv = converter('string');
9393

9494
Object.keys(convert).forEach(function(type) {
9595
getenv[type] = converter(type);
@@ -100,15 +100,14 @@ getenv.array = function array(varName, type, fallback) {
100100
if (Object.keys(convert).indexOf(type) === -1) {
101101
throw new Error('GetEnv.ArrayUndefinedType: Unknown array type ' + type);
102102
}
103-
var value = _value(varName, fallback);
103+
const value = _value(varName, fallback);
104104
return value.split(/\s*,\s*/).map(convert[type]);
105105
};
106106

107107
getenv.multi = function multi(spec) {
108-
var key, value;
109-
var result = {};
110-
for (var key in spec) {
111-
var value = spec[key];
108+
const result = {};
109+
for (let key in spec) {
110+
const value = spec[key];
112111
if (util.isArray(value)) {
113112
// default value & typecast
114113
switch (value.length) {

test/disableErrors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var assert = require('assert');
1+
const assert = require('assert');
22

3-
var getenv = require('../index');
3+
const getenv = require('../index');
44

5-
var tests = {};
5+
const tests = {};
66

77
tests['getenv.disableErrors() should disable any errors'] = function() {
88
getenv.disableErrors();

test/fallbacks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var assert = require('assert');
1+
const assert = require('assert');
22

3-
var getenv = require('../index');
3+
const getenv = require('../index');
44

5-
var tests = {};
5+
const tests = {};
66

77
// order dependent test
88
tests['getenv.disableFallbacks() makes relying on fallbacks an error'] = function() {

0 commit comments

Comments
 (0)