-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.js
262 lines (197 loc) · 6.06 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import React from 'react';
import {JOIN_SYMBOL, FIELD_NAME_PREFIX} from './constants';
export const EditorContext = React.createContext();
export function capitalize(string) {
if (!string)
return '';
return string.charAt(0).toUpperCase() + string.substr(1).toLowerCase();
}
export function convertType(value, to) {
if (typeof value === to)
return value;
if (to === 'number' || to === 'integer') {
if (typeof value === 'string') {
value = value.trim();
if (value === '')
value = null;
else if (!isNaN(Number(value)))
value = Number(value);
} else if (typeof value === 'boolean') {
value = value === true ? 1 : 0;
}
} else if (to === 'boolean') {
if (value === 'false' || value === false)
value = false;
else
value = true;
}
return value;
}
export function actualType(value) {
/* Returns the "actual" type of the given value.
- array -> 'array'
- null -> 'null'
*/
let type = typeof value;
if (type === 'object') {
if (Array.isArray(value))
type = 'array';
else if (value === null)
type = 'null';
}
return type;
}
export function getSchemaType(schema) {
/* Returns type of the given schema.
If schema.type is not present, it tries to guess the type.
If data is given, it will try to use that to guess the type.
*/
let type;
if (schema.hasOwnProperty('const'))
type = actualType(schema.const);
else
type = normalizeKeyword(schema.type);
if (!type) {
if (schema.hasOwnProperty('properties') ||
schema.hasOwnProperty('keys')
)
type = 'object';
else if (schema.hasOwnProperty('items'))
type = 'array';
else if (schema.hasOwnProperty('allOf'))
type = 'allOf';
else if (schema.hasOwnProperty('oneOf'))
type = 'oneOf';
else if (schema.hasOwnProperty('anyOf'))
type = 'anyOf';
else
type = 'string';
}
return type;
}
export function getVerboseName(name) {
if (name === undefined || name === null)
return '';
name = name.replace(/_/g, ' ');
return capitalize(name);
}
export function getCsrfCookie() {
let csrfCookies = document.cookie.split(';').filter((item) => item.trim().indexOf('csrftoken=') === 0);
if (csrfCookies.length) {
return csrfCookies[0].split('=')[1];
} else {
// if no cookie found, get the value from the csrf form input
let input = document.querySelector('input[name="csrfmiddlewaretoken"]');
if (input)
return input.value;
}
return null;
}
export function joinCoords() {
/* Generates coordinates from given arguments */
return Array.from(arguments).join(JOIN_SYMBOL);
}
export function splitCoords(coords) {
/* Generates coordinates */
return coords.split(JOIN_SYMBOL);
}
export function getCoordsFromName(name) {
/* Returns coordinates of a field in the data from
* the given name of the input.
* Field names have FIELD_NAME_PREFIX prepended but the coordinates don't.
* e.g.:
* name: rjf-0-field (where rjf- is the FIELD_NAME_PREFIX)
* coords: 0-field
*/
return name.slice((FIELD_NAME_PREFIX + JOIN_SYMBOL).length);
}
export function debounce(func, wait) {
let timeout;
return function() {
clearTimeout(timeout);
let args = arguments;
let context = this;
timeout = setTimeout(function() {
func.apply(context, args);
}, (wait || 1));
}
}
export function normalizeKeyword(kw) {
/* Converts custom supported keywords to standard JSON schema keywords */
if (Array.isArray(kw))
kw = kw.find((k) => k !== 'null') || 'null';
switch (kw) {
case 'list': return 'array';
case 'dict': return 'object';
case 'keys': return 'properties';
case 'choices': return 'enum';
case 'datetime': return 'date-time';
default: return kw;
}
}
export function getKeyword(obj, keyword, alias, default_value) {
/* Function useful for getting value from schema if a
* keyword has an alias.
*/
return getKey(obj, keyword, getKey(obj, alias, default_value));
}
export function getKey(obj, key, default_value) {
/* Approximation of Python's dict.get() function. */
let val = obj[key];
return (typeof val !== 'undefined') ? val : default_value;
}
export function choicesValueTitleMap(choices) {
/* Returns a mapping of {value: title} for the given choices.
* E.g.:
* Input: [{'title': 'One', 'value': 1}, 2]
* Output: {1: 'One', 2: 2}
*/
let map = {};
for (let i = 0; i < choices.length; i++) {
let choice = choices[i];
let value, title;
if (actualType(choice) === 'object') {
value = choice.value;
title = choice.title;
} else {
value = choice;
title = choice;
}
map[value] = title;
}
return map;
}
export function valueInChoices(schema, value) {
/* Checks whether the given value is in schema choices or not.
If schema doesn't have choices, returns true.
*/
let choices = getKeyword(schema, 'choices', 'enum');
if (!choices)
return true;
let found = choices.find((choice) => {
if (typeof choice == 'object')
choice = choice.value;
return value == choice;
})
return found !== undefined ? true : false;
}
/* Set operations */
export function isEqualset(a, b) {
return a.size === b.size && Array.from(a).every((i) => b.has(i));
}
export function isSuperset(set, subset) {
for (const elem of subset) {
if (!set.has(elem)) {
return false;
}
}
return true;
}
export function isSubset(set, superset) {
for (const elem of set) {
if (!superset.has(elem)) {
return false;
}
}
return true;
}