Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clean null values out of arrays #63

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 18 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,25 @@ function isEmptyValue(value) {
return true;
}

if (Array.isArray(value) && value.length === 0) {
if (Array.isArray(value)) {
for (var element of value) {
if (!isEmptyValue(element)) {
// the array has at least one non-empty element
return false;
}
}
// the array has no non-empty elements
return true;
}

if (typeof value === 'object' && Object.values(value).length === 0) {
if (typeof value === 'object') {
for (var childValue of Object.values(value)) {
if (!isEmptyValue(childValue)) {
// the object has at least one non-empty property
return false;
}
}
// the object has no non-empty property
return true;
}

Expand All @@ -86,15 +100,8 @@ function emptyValueReplacer(_, value) {
return undefined;
}

if (typeof value === 'object') {
for (var childValue of Object.values(value)) {
if (!isEmptyValue(childValue)) {
// the object has at least one non-empty property
return value;
}
}
// the object has no non-empty property
return undefined;
if (Array.isArray(value)) {
return value.filter(e => !isEmptyValue(e));
}

return value;
Expand Down
38 changes: 36 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,37 @@ describe('Deploy to ECS', () => {
throw new Error(`Wrong encoding ${encoding}`);
}

return '{ "memory": "", "containerDefinitions": [ { "name": "sample-container", "logConfiguration": {}, "repositoryCredentials": { "credentialsParameter": "" }, "cpu": 0, "essential": false } ], "requiresCompatibilities": [ "EC2" ], "family": "task-def-family" }';
return `
{
"memory": "",
"containerDefinitions": [ {
"name": "sample-container",
"logConfiguration": {},
"repositoryCredentials": { "credentialsParameter": "" },
"command": [
""
],
"environment": [
{
"name": "hello",
"value": "world"
},
{
"name": "",
"value": ""
}
],
"secretOptions": [ {
"name": "",
"valueFrom": ""
} ],
"cpu": 0,
"essential": false
} ],
"requiresCompatibilities": [ "EC2" ],
"family": "task-def-family"
}
`;
});

await run();
Expand All @@ -209,7 +239,11 @@ describe('Deploy to ECS', () => {
{
name: 'sample-container',
cpu: 0,
essential: false
essential: false,
environment: [{
name: 'hello',
value: 'world'
}]
}
],
requiresCompatibilities: [ 'EC2' ]
Expand Down