UNPKG

2.9 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
5 | |
6 | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
7 | All rights reserved. |
8 | |
9 | http://rippleosi.org |
10 | Email: code.custodian@rippleosi.org |
11 | |
12 | Author: Will Weatherill |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 10 March 2017
28
29*/
30
31
32function create(obj, propertyName, value) {
33 if (propertyName.indexOf(':') !== -1) {
34 var pieces = propertyName.split(':');
35 var prop = pieces[0];
36 var pos = parseInt(pieces[1]);
37 console.log('pos = ' + pos);
38 if (!obj[prop] || !Array.isArray(obj[prop])) obj[prop] = [];
39 if (!obj[prop][pos]) obj[prop][pos] = {};
40 return obj[prop][pos];
41 }
42 if (!obj[propertyName]) {
43 if (!value) obj[propertyName] = {};
44 }
45 if (value) obj[propertyName] = value;
46 return obj[propertyName];
47}
48
49function flatJSONToObject(flatInput) {
50 var pieces;
51 var value;
52 var output = {};
53 //var pathStr;
54 for (var path in flatInput) {
55 //pathStr = path.split('|').join('/');
56 value = flatInput[path];
57 pieces = path.split('/');
58 var obj = output;
59 var count;
60 var val;
61 for (var i = 0; i < pieces.length; i++) {
62 val = null;
63 if (i === (pieces.length - 1)) val = value;
64 obj = create(obj, pieces[i], val);
65 }
66 }
67 return output;
68}
69
70module.exports = flatJSONToObject;