Skip to content

Commit

Permalink
Fix nested array deconstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
manishoo committed Mar 29, 2020
1 parent 309ea5b commit 4930e1c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/deflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const deflate = (node: Object, index: Object, path: $ReadOnlyArray<string>) => {

const fieldNames = Object.keys(node);

const result = {};
const result = Array.isArray(node) ? [] : {};

for (const fieldName of fieldNames) {
const value = node[fieldName];
Expand Down
2 changes: 1 addition & 1 deletion src/inflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const inflate = (node: Object, index: Object, path: $ReadOnlyArray<string>) => {

const fieldNames = Object.keys(node);

const result = {};
const result = Array.isArray(node) ? [] : {};

for (const fieldName of fieldNames) {
const value = node[fieldName];
Expand Down
30 changes: 30 additions & 0 deletions test/deflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,33 @@ test('does not deconstruct an array of string', (t) => {
}
});
});

test('does not deconstruct a nested array', (t) => {
const response = {
data: {
__typename: 'foo',
names: [
[
'foo',
'bar1',
'bar2'
]
]
}
};

const deflatedResponse = deflate(response);

t.deepEqual(deflatedResponse, {
data: {
__typename: 'foo',
names: [
[
'foo',
'bar1',
'bar2'
]
]
}
});
});
30 changes: 30 additions & 0 deletions test/inflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,33 @@ test('does not deconstruct an array of string', (t) => {
}
});
});

test('does not deconstruct a nested array', (t) => {
const response = {
data: {
__typename: 'foo',
names: [
[
'foo',
'bar1',
'bar2'
]
]
}
};

const inflatedResponse = inflate(response);

t.deepEqual(inflatedResponse, {
data: {
__typename: 'foo',
names: [
[
'foo',
'bar1',
'bar2'
]
]
}
});
});

0 comments on commit 4930e1c

Please sign in to comment.