3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import changeCase
from 'change-case';
24 addAuthorizationStub(xhr
) {
25 xhr
.setRequestHeader('Authorization', 'Basic YWRtaW46YWRtaW4=');
27 getSearchParams(url
) {
28 var a
= document
.createElement('a');
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]);
40 parseJSONIgnoreErrors(txt
) {
42 return JSON
.parse(txt
);
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
) => {
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
? [] : {}
72 resolvedObj
[name
] = value
;
74 updatePathValue(obj
, path
, value
) {
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
? [] : {}
91 if (isRemove
&& ((i
+ 1) === path
.length
)) {
93 r
[p
] = r
[p
].filter((d
, i
) => i
!== index
);
101 resolvedObj
[name
] = value
;
104 removePathValue(obj
, path
) {
105 // note updatePathValue removes value if third argument is undefined
106 return this.updatePathValue(obj
, path
);
109 suffixAsInteger
: (field
) => {
111 const str
= String(obj
[field
]);
112 const value
= str
.replace(str
.replace(/[\d]+$/, ''), '');
113 return 1 + parseInt(value
, 10) || 0;
117 toBiggestValue
: (maxIndex
, curIndex
) => Math
.max(maxIndex
, curIndex
)