Skip to content

Commit

Permalink
add array support in assembleQueryData method
Browse files Browse the repository at this point in the history
  • Loading branch information
mahsa shadi committed Sep 6, 2021
1 parent 3ecb734 commit d1335a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
36 changes: 34 additions & 2 deletions __tests__/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('VFS Utils', () => {
});

test('assembleQueryData', () => {
const result = utils.assembleQueryData({
const result1 = utils.assembleQueryData({
'a': 'b',
'b.a': 'foo',
'b.b.a': 'foo',
Expand All @@ -258,7 +258,21 @@ describe('VFS Utils', () => {
'e': '1'
});

expect(result).toEqual({
const result2 = utils.assembleQueryData({
'a.0': 'foo',
'a.1': 'foo',
'b.0': 'foo',
'b.1': 'foo',
'b.a': 'foo',
'c.a': 'foo',
'c.b.0': 'foo',
'c.b.1': 'foo',
'c.c.0': 'foo',
'c.c.1': 'foo',
'c.c.a': 'foo',
});

expect(result1).toEqual({
a: 'b',
b: {
a: 'foo',
Expand All @@ -274,5 +288,23 @@ describe('VFS Utils', () => {
d: 'true',
e: '1'
});

expect(result2).toEqual({
a: ['foo', 'foo'],
b:{
'0': 'foo',
'1': 'foo',
'a': 'foo',
},
c: {
a: 'foo',
b: ['foo', 'foo'],
c: {
'0': 'foo',
'1': 'foo',
'a': 'foo'
}
}
});
});
});
8 changes: 7 additions & 1 deletion src/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,20 @@ const assembleQueryData = (object) => {
const dots = key.split('.');

let last = assembled;
let parent = null;
for (let j = 0; j < dots.length; j++) {
const dot = dots[j];
if (j >= dots.length - 1) {
if (!/^\d+$/.test(dot) && Array.isArray(last)) {
last = Object.fromEntries(last.map((value, i) => [i, value]));
parent[dots[j - 1]] = last;
}
last[dot] = object[key];
} else {
last[dot] = last[dot] || {};
last[dot] = last[dot] || (/^\d+$/.test(dots[j + 1]) ? [] : {});
}

parent = last;
last = last[dot];
}
}
Expand Down

0 comments on commit d1335a2

Please sign in to comment.