Skip to content

Commit af5a669

Browse files
committed
+ ignore env vars with values not string
1 parent f19a023 commit af5a669

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ a string contains `${value}` separated by commas;
554554
the `value` could be `null`,`boolean`, `number`, `"string"`, `[..array..]` or `{..object..}`;
555555
and all could be wrapped or not wrapped by `[` and `]` (*must*, when the string is empty).
556556

557-
*Special case:* Values that have only a string enclosed by double quotes
557+
*Special case:* Values that have only a string enclosed by double quotes
558558
also match the above format.
559559

560560
*Spaces will be trimmed.*
@@ -1752,22 +1752,22 @@ See [this feature](#prevent-variables-from-conversion).
17521752

17531753
Type: `object`. *Default:* `{}`.
17541754

1755-
List of custom conversions for specific environment variables.
1755+
Contains custom conversions for specific environment variables.
17561756

17571757
See [this feature](#custom-conversion-for-a-specific-variable).
17581758

17591759
##### `methods`
17601760

17611761
Type: `object`. *Default:* `{}`.
17621762

1763-
List of custom conversion methods.
1763+
Contains custom conversion methods.
17641764

17651765
See [this feature](#custom-methods).
17661766

17671767
##### `methodAliases`
17681768

17691769
Type: `object`. *Default:* `{}`.
17701770

1771-
List of conversion method aliases.
1771+
Contains conversion method aliases.
17721772

17731773
See [this feature](#method-aliases).

dist/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ function parseSymbol(str) {
129129

130130
/**
131131
*
132-
* @param {string} value
132+
* @param {string|*} value
133133
* @param {object} valueTable
134134
* @param {boolean} fromDotEnv
135135
* @returns {null|undefined|boolean|number|bigint|string|symbol|array|object}
136136
*/
137137
function restoreValue(value, valueTable, fromDotEnv) {
138+
if (!(typeof value == 'string' || value instanceof String)) {
139+
return value;
140+
}
138141
if (fromDotEnv) {
139142
value = unescapeValue(value);
140143
}

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ function parseSymbol(str) {
129129

130130
/**
131131
*
132-
* @param {string} value
132+
* @param {string|*} value
133133
* @param {object} valueTable
134134
* @param {boolean} fromDotEnv
135135
* @returns {null|undefined|boolean|number|bigint|string|symbol|array|object}
136136
*/
137137
function restoreValue(value, valueTable, fromDotEnv) {
138+
if (!(typeof value == 'string' || value instanceof String)) {
139+
return value
140+
}
138141
if (fromDotEnv) {
139142
value = unescapeValue(value)
140143
}

tests/index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ describe('dotenv-conversion', function () {
7474
it('set', function (done) {
7575
// input
7676
const inputConfig = {
77-
parsed: {},
77+
parsed: {
78+
TRUE: true,
79+
FALSE: 'false',
80+
NUMBER: new String('4.5e10'),
81+
},
7882
fromDotEnv: false,
7983
ignoreProcessEnv: true,
8084
prevents: ['BASIC'],
@@ -85,8 +89,8 @@ describe('dotenv-conversion', function () {
8589
},
8690
methods: {
8791
// override existing built-in conversion method
88-
auto(value) {
89-
return 'auto'
92+
number(value) {
93+
return 'number'
9094
},
9195
// add new conversion method
9296
basic(value) {
@@ -102,7 +106,11 @@ describe('dotenv-conversion', function () {
102106
}
103107

104108
// output
105-
const expectedParsed = inputConfig.parsed
109+
const expectedParsed = {
110+
TRUE: true,
111+
FALSE: false,
112+
NUMBER: 45000000000,
113+
}
106114
const expectedFromDotEnv = inputConfig.fromDotEnv
107115
const expectedIgnoreProcessEnv = inputConfig.ignoreProcessEnv
108116
const expectedPrevents = inputConfig.prevents
@@ -119,7 +127,7 @@ describe('dotenv-conversion', function () {
119127

120128
'basic',
121129
]
122-
const expectedMethodAutoReturn = 'auto'
130+
const expectedMethodNumberReturn = 'number'
123131
const expectedMethodBasicReturn = 'basic'
124132
const expectedMethodAliases = {
125133
bool: 'boolean',
@@ -150,7 +158,7 @@ describe('dotenv-conversion', function () {
150158
dotenvConversionConfig.specs.should.deep.equal(expectedSpecs)
151159
Object.keys(dotenvConversionConfig.methods).should.deep.equal(expectedMethods)
152160
dotenvConversionConfig.methodAliases.should.deep.equal(expectedMethodAliases)
153-
dotenvConversionConfig.methods.auto('value').should.equal(expectedMethodAutoReturn)
161+
dotenvConversionConfig.methods.number('value').should.equal(expectedMethodNumberReturn)
154162
dotenvConversionConfig.methods.basic('value').should.equal(expectedMethodBasicReturn)
155163

156164
done()

0 commit comments

Comments
 (0)