Skip to content

Commit

Permalink
Check for numbers when parsing variables (#30)
Browse files Browse the repository at this point in the history
* Ensure numbers are not parsed as JSON

* Bump to v0.5.2
  • Loading branch information
kitforbes committed May 10, 2021
1 parent b0908df commit 512b539
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cda0/terrajs",
"version": "0.5.1",
"version": "0.5.2",
"description": "A node interface to Terraform",
"main": "src/index.js",
"scripts": {
Expand Down
9 changes: 5 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ function parseVariable(key, value) {
return `'${parsedKey}=${parsedValue.replace(/'/g, '\\\'')}'`;
}

if (typeof value === 'boolean') {
return `"${parsedKey}=${value}"`;
if (typeof value === 'string') {
// Double-quotes can prematurely close the string, so they need to be escaped.
return `"${parsedKey}=${value.replace(/"/g, '\\"')}"`;
}

// Double-quotes can prematurely close the string, so they need to be escaped.
return `"${parsedKey}=${value.replace(/"/g, '\\"')}"`;
// Booleans and numbers can be returned without parsing.
return `"${parsedKey}=${value}"`;
}

module.exports = {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ describe('helpers', () => {
assert.equal(response, '"some_var=West Europe"');
});

it('should correctly parse a number variable', () => {
const variable = 42;
const response = parseVariable('someVar', variable);
assert.equal(response, '"some_var=42"');
});

it('should correctly parse a boolean variable', () => {
const variable = true;
const response = parseVariable('someVar', variable);
Expand Down Expand Up @@ -137,6 +143,12 @@ describe('helpers', () => {
assert.equal(response, '"some_var=West Europe"');
});

it('should correctly parse a number variable', () => {
const variable = 42;
const response = parseVariable('someVar', variable);
assert.equal(response, '"some_var=42"');
});

it('should correctly parse a boolean variable', () => {
const variable = true;
const response = parseVariable('someVar', variable);
Expand Down

0 comments on commit 512b539

Please sign in to comment.