Skip to content

Commit f02614e

Browse files
authored
Merge pull request #52 from devhammed/devhammed/issue49
replace env vars with underscores, fixes #49
2 parents 62c462e + 708d335 commit f02614e

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/lib/applyEnv.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default function (value, env) {
44
}
55

66
Object.keys(env.data).forEach(key => {
7-
value = value.replace(new RegExp('{{(\\s*' + key + '\\s*)}}', 'g'), env.data[key]);
7+
value = value.replace(new RegExp('{{(\\s*(_.)?' + key + '\\s*)}}', 'g'), env.data[key]);
88
});
99

1010
return value;

src/lib/applyEnv.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@ describe('applyEnv', function () {
1515
);
1616
});
1717

18+
it('should replace the env vars with root underscores', function () {
19+
const env = {
20+
data: {
21+
url: 'http://localhost',
22+
method: 'GET'
23+
}
24+
};
25+
26+
return expect(applyEnv('The url is {{_.url}} and the method is {{ _.method }}', env)).to.eql(
27+
'The url is http://localhost and the method is GET'
28+
);
29+
});
30+
1831
it('should handle multiple spaces correctly', function () {
1932
const env = {
2033
data: {
2134
foo: 'bla'
2235
}
2336
};
2437

25-
return expect(applyEnv('foo is {{ foo }}', env)).to.eql('foo is bla');
38+
return expect(applyEnv('foo is {{ foo }} and {{ _.foo }}', env)).to.eql('foo is bla and bla');
2639
});
2740
});

src/lib/formatEnv.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default function (variable) {
2-
return variable.replace(/{{\s*([a-zA-Z0-9_]+)\s*}}/g, '<span class="env-variable">$1</span>');
2+
return variable.replace(/{{\s*(_.)?([a-zA-Z0-9_]+)\s*}}/g, '<span class="env-variable">$2</span>');
33
}

src/lib/formatEnv.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from 'chai';
2+
import formatEnv from './formatEnv';
3+
4+
describe('formatEnv', function () {
5+
it('should replace env vars', function () {
6+
return expect(formatEnv('The url is {{url}} and {{_.url}}')).to.eql(
7+
'The url is <span class="env-variable">url</span> and <span class="env-variable">url</span>'
8+
);
9+
});
10+
11+
it('should replace env vars with spaces', function () {
12+
return expect(formatEnv('The url is {{ url }} and {{ _.url }}')).to.eql(
13+
'The url is <span class="env-variable">url</span> and <span class="env-variable">url</span>'
14+
);
15+
});
16+
});

0 commit comments

Comments
 (0)