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 | 4 April March 2017
|
28 |
|
29 | */
|
30 |
|
31 |
|
32 | var traverse = require('traverse');
|
33 |
|
34 | function isNumeric(n) {
|
35 | return !isNaN(parseFloat(n)) && isFinite(n);
|
36 | }
|
37 |
|
38 | function flatten(obj) {
|
39 | var flatObj = {};
|
40 |
|
41 | var outputObj = traverse(obj).map(function(node) {
|
42 | if (this.isLeaf) {
|
43 | var flatPath = '';
|
44 | var slash = '';
|
45 | var colon = '';
|
46 | var lastPathIndex = this.path.length - 1;
|
47 | var pathArr = this.path;
|
48 | pathArr.forEach(function(path, index) {
|
49 | if (isNumeric(path)) {
|
50 | flatPath = flatPath + colon + path
|
51 | }
|
52 | else {
|
53 | if (index === lastPathIndex && path[0] === '|' && isNumeric(pathArr[index -1])) slash = '';
|
54 | flatPath = flatPath + slash + path;
|
55 | }
|
56 | slash = '/';
|
57 | colon = ':';
|
58 | });
|
59 | flatObj[flatPath] = node;
|
60 | }
|
61 | });
|
62 | return flatObj;
|
63 | }
|
64 |
|
65 | module.exports = flatten;
|