RIFT-14828, RIFT-15031: choice/case for top level:2
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / utils.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 'use strict';
20
21 import changeCase from 'change-case';
22
23 export default {
24 addAuthorizationStub(xhr) {
25 xhr.setRequestHeader('Authorization', 'Basic YWRtaW46YWRtaW4=');
26 },
27 getSearchParams(url) {
28 var a = document.createElement('a');
29 a.href = url;
30 var params = {};
31 var items = a.search.replace('?', '').split('&');
32 for (var i = 0; i < items.length; i++) {
33 if (items[i].length > 0) {
34 var key_value = items[i].split('=');
35 params[key_value[0]] = decodeURIComponent(key_value[1]);
36 }
37 }
38 return params;
39 },
40 parseJSONIgnoreErrors(txt) {
41 try {
42 return JSON.parse(txt);
43 } catch (ignore) {
44 return {};
45 }
46 },
47 resolvePath(obj, path) {
48 // supports a.b, a[1] and foo[bar], etc.
49 // where obj is ['nope', 'yes', {a: {b: 1}, foo: 2}]
50 // then [1] returns 'yes'; [2].a.b returns 1; [2].a[foo] returns 2;
51 path = path.split(/[\.\[\]]/).filter(d => d);
52 return path.reduce((r, p) => {
53 if (r) {
54 return r[p];
55 }
56 }, obj);
57 },
58 assignPathValue(obj, path, value) {
59 path = path.split(/[\.\[\]]/).filter(d => d);
60 // enable look-ahead to determine if type is array or object
61 const pathCopy = path.slice();
62 // last item in path used to assign value on the resolved object
63 const name = path.pop();
64 const resolvedObj = path.reduce((r, p, i) => {
65 if (typeof(r[p]) !== 'object') {
66 // look-ahead to see if next path item is a number
67 const isArray = !isNaN(parseInt(pathCopy[i + 1], 10));
68 r[p] = isArray ? [] : {}
69 }
70 return r[p];
71 }, obj);
72 resolvedObj[name] = value;
73 },
74 updatePathValue(obj, path, value, isCase) {
75 // todo: replace implementation of assignPathValue with this impl and
76 // remove updatePathValue (only need one function, not both)
77 // same as assignPathValue except removes property if value is undefined
78 path = path.split(/[\.\[\]]/).filter(d => d);
79 const isRemove = typeof value === 'undefined';
80 // enable look-ahead to determine if type is array or object
81 const pathCopy = path.slice();
82 // last item in path used to assign value on the resolved object
83 const name = path.pop();
84 const resolvedObj = path.reduce((r, p, i) => {
85 // look-ahead to see if next path item is a number
86 const index = parseInt(pathCopy[i + 1], 10);
87 const isArray = !isNaN(index);
88 if (typeof(r[p]) !== 'object') {
89 r[p] = isArray ? [] : {}
90 }
91 if (isRemove && ((i + 1) === path.length)) {
92 if (isArray) {
93 r[p] = r[p].filter((d, i) => i !== index);
94 } else {
95 if(isCase) {
96 delete r[name];
97 } else {
98 delete r[p][name];
99 }
100 }
101 }
102 if(isCase) {
103 return r;
104 } else {
105 return r[p];
106 }
107
108 }, obj);
109 if (!isRemove) {
110 resolvedObj[name] = value;
111 }
112 },
113 removePathValue(obj, path, isCase) {
114 // note updatePathValue removes value if third argument is undefined
115 return this.updatePathValue(obj, path, undefined, isCase);
116 },
117
118 suffixAsInteger: (field) => {
119 return (obj) =>{
120 const str = String(obj[field]);
121 const value = str.replace(str.replace(/[\d]+$/, ''), '');
122 return 1 + parseInt(value, 10) || 0;
123 };
124 },
125
126 toBiggestValue: (maxIndex, curIndex) => Math.max(maxIndex, curIndex)
127
128 }