File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var mapQueryString = require('../index');
2+
3+
describe('Query String', () => {
4+
5+
test('Mapper', () => {
6+
7+
var querystring = {
8+
'legs[0][departure_airport]': 'MXP',
9+
'legs[0][arrival_airport]': 'TIA',
10+
'legs[0][outbound_date]': '2017-03-29',
11+
'legs[1][departure_airport]': 'oiue',
12+
'legs[1][arrival_airport]': 'csdjn',
13+
'legs[1][outbound_date]': '2017-03-21',
14+
'person[child][name]': 'oiue',
15+
'person[child][age]': 'csdjn',
16+
'person[parent][name]': '2017-03-21',
17+
'adult': 1,
18+
'search_type': 'oneway'
19+
};
20+
21+
var mapped = mapQueryString(querystring);
22+
23+
expect(mapped.legs[1].departure_airport).toBe('oiue');
24+
expect(Object..toString.call(mapped.legs)).toBe('[object Array]');
25+
expect(Object..toString.call(mapped.legs[0])).toBe('[object Object]');
26+
expect(mapped.person.child.name).toBe('oiue');
27+
expect(mapped.legs[0].outbound_date).toBe('2017-03-29');
28+
});
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function recursivelyCheckIfArray (parentObj) {
2+
if (Object..toString.call(parentObj) != '[object Object]') return parentObj;
3+
4+
Object.keys(parentObj).map((parentKey) => {
5+
var childObj = parentObj[parentKey];
6+
7+
if (Object..toString.call(childObj) != '[object Object]') return;
8+
9+
var keys = Object.keys(childObj);
10+
11+
if (keys.every((childKey) => { return /^(\d+)$/g.test(childKey); }))
12+
parentObj[parentKey] = keys.map((key) => { return childObj[key] });
13+
14+
recursivelyCheckIfArray(childObj);
15+
});
16+
17+
18+
return parentObj;
19+
}
20+
21+
function parseParam (json) {
22+
23+
Object.keys(json).map((paramName) => {
24+
25+
var segments = paramName.match(/([^\[\]]+)/g),
26+
step = json;
27+
28+
// No nested params found
29+
if (segments.length <= 1) return;
30+
31+
segments.map(function (segment, k) {
32+
if (k >= segments.length-1) {
33+
step[segment] = json[paramName];
34+
return;
35+
}
36+
37+
if (!step[segment]) step[segment] = {};
38+
step = step[segment];
39+
});
40+
41+
});
42+
43+
44+
return recursivelyCheckIfArray(json);
45+
}
46+
47+
48+
module.exports = function (json) {
49+
return parseParam(json);
50+
};
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "amazon-api-gateway-querystring",
3+
"version": "1.0.0",
4+
"description": "Utility for parse nested parameters in query strings",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://.com/StayDistributed/amazon-api-gateway-querystring.git"
12+
},
13+
"keywords": [
14+
"aws",
15+
"api gateway",
16+
"querystrings",
17+
"nested params"
18+
],
19+
"author": "Michele Salvini",
20+
"license": "ISC",
21+
"bugs": {
22+
"url": "https://.com/StayDistributed/amazon-api-gateway-querystring/issues"
23+
},
24+
"homepage": "https://.com/StayDistributed/amazon-api-gateway-querystring#readme"
25+
}

0 commit comments

Comments
 (0)