forked from ishubin/schemio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
327 lines (291 loc) · 8.09 KB
/
Copy pathutils.js
File metadata and controls
327 lines (291 loc) · 8.09 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { forEach, map } from './collections';
import { encode } from 'js-base64';
function leadingZero(number) {
if (number < 10) {
return '0' + number;
} else {
return '' + number;
}
}
function sanitizeItem(oldItem) {
let item = {};
forEach(oldItem, (value, field) => {
if (field === 'childItems') {
item[field] = map(value, sanitizeItem);
} else if (field !== 'meta' && field !== '_childItems') {
item[field] = value;
}
});
return item;
}
const _defaultScripts = {
main: {
source: ''
},
functions: []
};
function sanitizeScheme(scheme) {
const items = map(scheme.items, sanitizeItem);
return {
id: scheme.id,
name: scheme.name,
description: scheme.description,
tags: scheme.tags,
modifiedTime: scheme.modifiedTime,
scripts: scheme.scripts || _defaultScripts,
items: items,
style: scheme.style || {}
}
}
function formatDateAndTime(dateInMillis) {
var d = new Date(dateInMillis);
return `${leadingZero(d.getFullYear())}.${leadingZero(d.getMonth())}.${leadingZero(d.getDate())} ${leadingZero(d.getHours())}:${leadingZero(d.getMinutes())}`;
}
function clone(obj) {
if (typeof obj === 'undefined' || obj === null) {
return null;
}
return JSON.parse(JSON.stringify(obj));
}
function extendObject(originalObject, overrideObject) {
forEach(overrideObject, (value, key) => {
if (!originalObject.hasOwnProperty(key)) {
originalObject[key] = clone(value);
} else {
if (typeof value === 'object') {
extendObject(originalObject[key], value);
}
}
});
return originalObject;
}
/**
*
* @param {object} obj
* @param {string|Array} propertyPath - a dot separated path to a property inside a given object
*/
function getObjectProperty(item, propertyPath) {
if (item) {
let objectPath = null;
if (Array.isArray(propertyPath)) {
objectPath = propertyPath;
} else {
objectPath = propertyPath.split('.');
}
let field = item;
let i = 0;
while(i < objectPath.length) {
const fieldName = objectPath[i].trim();
if (fieldName) {
if (i < objectPath.length - 1) {
field = field[fieldName];
if (typeof field !== 'object') {
// not doing anything since there is a conflict
return undefined;
}
} else {
// this is the lowest nested property
if (field.hasOwnProperty(fieldName)) {
return field[fieldName];
}
}
} else {
//Probably an error
return undefined;
}
i += 1;
}
}
return undefined;
}
/**
*
* @param {Object} obj
* @param {String|Array} propertyPath either encoded property path in string or an array of fields
* @param {*} value
* @returns
*/
function setObjectProperty(obj, propertyPath, value) {
changeObjectProperty(obj, propertyPath, value, false);
}
/**
*
* @param {Object} obj
* @param {String|Array} propertyPath
* @param {*} value
* @param {Boolean} shouldDelete
* @returns
*/
function changeObjectProperty(obj, propertyPath, value, shouldDelete) {
let objectPath = null;
if (Array.isArray(propertyPath)) {
objectPath = propertyPath;
} else {
objectPath = propertyPath.split('.');
}
let field = obj;
let i = 0;
while(i < objectPath.length) {
const fieldName = objectPath[i].trim();
if (fieldName) {
if (i < objectPath.length - 1) {
if (!field.hasOwnProperty(fieldName)) {
field[fieldName] = {};
}
field = field[fieldName];
if (typeof field !== 'object') {
// not doing anything since there is a conflict
return;
}
} else {
// this is the lowest nested property
if (shouldDelete && field.hasOwnProperty(fieldName)) {
delete field[fieldName];
} else {
field[fieldName] = value;
}
return;
}
} else {
//Probably an error, so return and don't do anything.
return;
}
i += 1;
}
}
function deleteObjectProperty(obj, propertyPath) {
changeObjectProperty(obj, propertyPath, null, true);
}
function rotatePointAroundCenter(px, py, angle, cx, cy) {
const vx = px - cx;
const vy = py - cy;
const rotated = rotateVector(vx, vy, angle);
return {
x: cx + rotated.x,
y: cy + rotated.y
};
}
function rotateVector(x, y, angle) {
const cs = Math.cos(angle * Math.PI / 180);
const sn = Math.sin(angle * Math.PI / 180);
return {
x: x * cs - y * sn,
y: x * sn + y * cs
};
}
function enumerateConstants(obj) {
const props = [];
for (let key in obj) {
// checking if property is all uppercase
if (obj.hasOwnProperty(key) && key === key.toUpperCase()) {
props.push(obj[key]);
}
}
return props;
}
function domHasParentNode(domElement, callbackCheck) {
if (callbackCheck(domElement)) {
return true;
};
if (domElement.parentElement) {
return domHasParentNode(domElement.parentElement, callbackCheck);
}
return false;
}
function domFindAncestorByClassUntil(domElement, cssClass, stopCallback) {
if (domElement.classList.contains(cssClass)) {
return domElement;
}
if (stopCallback) {
if (stopCallback(domElement)) {
return null;
}
}
if (domElement.parentElement) {
return domFindAncestorByClassUntil(domElement.parentElement, cssClass, stopCallback);
}
return null;
}
function forceDownload(fileName, contentType, content) {
const dataUrl = `data:${contentType};base64,${encode(content)}`;
const link = document.createElement('a');
document.body.appendChild(link);
try {
link.href = dataUrl;
link.download = fileName;
link.click();
} catch(e) {
console.error(e);
}
setTimeout(() => document.body.removeChild(link), 100);
}
/**
* Compares two objects for deep equality
* @param {*} v1
* @param {*} v2
*/
function equals(v1, v2) {
const t1 = typeof v1;
const t2 = typeof v2;
if (t1 !== t2) {
return false;
}
if (Array.isArray(v1)) {
if (v1.length !== v2.length) {
return false;
}
for (let i = 0; i < v1.length; i++) {
if (!equals(v1[i], v2[i])) {
return false;
}
}
return true;
} else if (t1 === 'object') {
for (let key in v1) {
if (v1.hasOwnProperty(key)) {
if (!equals(v1[key], v2[key])) {
return false;
}
}
}
for (let key in v2) {
if (!v1.hasOwnProperty(key)) {
return false;
}
}
return true;
}
return v1 === v2;
}
function hashString(text) {
let hash = 0;
if (text.length == 0) {
return hash;
}
for (let i = 0; i < text.length; i++) {
let symbol = text.charCodeAt(i);
hash = ((hash << 5) - hash) + symbol;
hash = hash & hash;
}
return hash;
}
export default {
formatDateAndTime,
clone,
extendObject,
sanitizeScheme,
getObjectProperty,
setObjectProperty,
deleteObjectProperty,
rotateVector,
rotatePointAroundCenter,
enumerateConstants,
domHasParentNode,
domFindAncestorByClassUntil,
forceDownload,
equals,
hashString
};