Skip to content

Commit 6b1f3e4

Browse files
fix: clean null values out of arrays (#63)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 015bfc8 commit 6b1f3e4

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

index.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,25 @@ function isEmptyValue(value) {
7070
return true;
7171
}
7272

73-
if (Array.isArray(value) && value.length === 0) {
73+
if (Array.isArray(value)) {
74+
for (var element of value) {
75+
if (!isEmptyValue(element)) {
76+
// the array has at least one non-empty element
77+
return false;
78+
}
79+
}
80+
// the array has no non-empty elements
7481
return true;
7582
}
7683

77-
if (typeof value === 'object' && Object.values(value).length === 0) {
84+
if (typeof value === 'object') {
85+
for (var childValue of Object.values(value)) {
86+
if (!isEmptyValue(childValue)) {
87+
// the object has at least one non-empty property
88+
return false;
89+
}
90+
}
91+
// the object has no non-empty property
7892
return true;
7993
}
8094

@@ -86,15 +100,8 @@ function emptyValueReplacer(_, value) {
86100
return undefined;
87101
}
88102

89-
if (typeof value === 'object') {
90-
for (var childValue of Object.values(value)) {
91-
if (!isEmptyValue(childValue)) {
92-
// the object has at least one non-empty property
93-
return value;
94-
}
95-
}
96-
// the object has no non-empty property
97-
return undefined;
103+
if (Array.isArray(value)) {
104+
return value.filter(e => !isEmptyValue(e));
98105
}
99106

100107
return value;

index.test.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,37 @@ describe('Deploy to ECS', () => {
198198
throw new Error(`Wrong encoding ${encoding}`);
199199
}
200200

201-
return '{ "memory": "", "containerDefinitions": [ { "name": "sample-container", "logConfiguration": {}, "repositoryCredentials": { "credentialsParameter": "" }, "cpu": 0, "essential": false } ], "requiresCompatibilities": [ "EC2" ], "family": "task-def-family" }';
201+
return `
202+
{
203+
"memory": "",
204+
"containerDefinitions": [ {
205+
"name": "sample-container",
206+
"logConfiguration": {},
207+
"repositoryCredentials": { "credentialsParameter": "" },
208+
"command": [
209+
""
210+
],
211+
"environment": [
212+
{
213+
"name": "hello",
214+
"value": "world"
215+
},
216+
{
217+
"name": "",
218+
"value": ""
219+
}
220+
],
221+
"secretOptions": [ {
222+
"name": "",
223+
"valueFrom": ""
224+
} ],
225+
"cpu": 0,
226+
"essential": false
227+
} ],
228+
"requiresCompatibilities": [ "EC2" ],
229+
"family": "task-def-family"
230+
}
231+
`;
202232
});
203233

204234
await run();
@@ -209,7 +239,11 @@ describe('Deploy to ECS', () => {
209239
{
210240
name: 'sample-container',
211241
cpu: 0,
212-
essential: false
242+
essential: false,
243+
environment: [{
244+
name: 'hello',
245+
value: 'world'
246+
}]
213247
}
214248
],
215249
requiresCompatibilities: [ 'EC2' ]

0 commit comments

Comments
 (0)