|
2 | 2 | * Created by codeslayer on 3/13/16. |
3 | 3 | */ |
4 | 4 |
|
5 | | -var JSONMapper = { |
| 5 | +var JSONTransform = { |
6 | 6 |
|
7 | 7 | /** |
8 | | - * Routes request to mapObject function after sanitation checks in input/mapping |
| 8 | + * Routes request to transformObject function after sanitation checks in input/template |
9 | 9 | * |
10 | | - * @param {JSON} input, {JSON} mapping |
| 10 | + * @param {JSON} input, {JSON} template |
11 | 11 | * @return {JSON} |
12 | 12 | */ |
13 | | - map: function(input, mapping){ |
14 | | - if(Utils.isEmpty(input) || Utils.isEmpty(mapping)){ |
| 13 | + transform: function(input, template){ |
| 14 | + if(Utils.isEmpty(input) || Utils.isEmpty(template)){ |
15 | 15 | return null |
16 | 16 | } |
17 | 17 |
|
18 | | - return JSONMapper.mapObject(input,mapping); |
| 18 | + return JSONTransform.transformObject(input,template); |
19 | 19 | }, |
20 | 20 |
|
21 | 21 | /** |
22 | | - * This function iterates the specified mapping and populates the desired keys from the input |
| 22 | + * This function iterates the specified template and populates the desired keys from the input |
23 | 23 | * |
24 | | - * @param {JSON} input, {JSON} mapping |
| 24 | + * @param {JSON} input, {JSON} template |
25 | 25 | * @return {JSON} |
26 | 26 | */ |
27 | | - mapObject: function(input, mapping){ |
28 | | - var mappedObject = {}; |
| 27 | + transformObject: function(input, template){ |
| 28 | + var transformedObject = {}; |
29 | 29 |
|
30 | | - for(var actualKey in mapping){ |
| 30 | + for(var actualKey in template){ |
31 | 31 | /* |
32 | | - -> if an object or array needs to be mapped, we take the desired key name and data to be mapped inside an object, |
33 | | - and parse all the keys in data one by one and set the mapped output against the desired key name |
34 | | - -> if a single variable needs to be mapped, we set the desired value against the desired key |
| 32 | + -> if an object or array needs to be transformed, we take the desired key name and data to be transformed inside an object, |
| 33 | + and parse all the keys in data one by one and set the transformed output against the desired key name |
| 34 | + -> if a single variable needs to be transformed, we set the desired value against the desired key |
35 | 35 | */ |
36 | | - if(Utils.isObject(mapping[actualKey])){ |
37 | | - var desiredKeyName = mapping[actualKey]['desiredKey']; |
38 | | - var desiredDataMapping = mapping[actualKey]['desiredData']; |
| 36 | + if(Utils.isObject(template[actualKey])){ |
| 37 | + var desiredKeyName = template[actualKey]['desiredKey']; |
| 38 | + var desiredDatatemplate = template[actualKey]['desiredData']; |
39 | 39 |
|
40 | | - if(Utils.isObject(desiredDataMapping)){ |
41 | | - mappedObject[desiredKeyName] = JSONMapper.mapObject(input[actualKey],desiredDataMapping); |
| 40 | + if(Utils.isObject(desiredDatatemplate)){ |
| 41 | + transformedObject[desiredKeyName] = JSONTransform.mapObject(input[actualKey],desiredDatatemplate); |
42 | 42 | } |
43 | | - else if(Utils.isArray(desiredDataMapping)){ |
44 | | - mappedObject[desiredKeyName] = JSONMapper.mapArray(input[actualKey],desiredDataMapping[0]); |
| 43 | + else if(Utils.isArray(desiredDatatemplate)){ |
| 44 | + transformedObject[desiredKeyName] = JSONTransform.mapArray(input[actualKey],desiredDatatemplate[0]); |
45 | 45 | } |
46 | 46 | } |
47 | 47 | else{ |
48 | | - var desiredKeyName = mapping[actualKey]; |
49 | | - mappedObject[desiredKeyName] = input[actualKey]; |
| 48 | + var desiredKeyName = template[actualKey]; |
| 49 | + transformedObject[desiredKeyName] = input[actualKey]; |
50 | 50 | } |
51 | 51 | } |
52 | 52 |
|
53 | | - return mappedObject; |
| 53 | + return transformedObject; |
54 | 54 | }, |
55 | 55 |
|
56 | 56 | /** |
57 | | - * This function iterates an array and maps each object of the array one by one |
| 57 | + * This function iterates an array and transforms each object of the array one by one |
58 | 58 | * |
59 | | - * @param {JSON} input, {JSON} mapping |
| 59 | + * @param {JSON} input, {JSON} template |
60 | 60 | * @return {JSON Array} |
61 | 61 | */ |
62 | | - mapArray: function(input, mapping){ |
63 | | - var mappedArray = []; |
| 62 | + transformArray: function(input, template){ |
| 63 | + var transformedArray = []; |
64 | 64 | for(var index in input){ |
65 | 65 | var inputObject = input[index]; |
66 | | - var mappedObject = JSONMapper.mapObject(inputObject,mapping); |
67 | | - mappedArray.push(mappedObject); |
| 66 | + var transformedObject = JSONTransform.mapObject(inputObject,template); |
| 67 | + transformedArray.push(transformedObject); |
68 | 68 | } |
69 | 69 |
|
70 | | - return mappedArray; |
| 70 | + return transformedArray; |
71 | 71 | } |
72 | 72 |
|
73 | 73 | }; |
74 | 74 |
|
75 | | -module.exports = JSONMapper; |
| 75 | +module.exports = JSONTransform; |
0 commit comments