forked from zombieFox/nightTab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
380 lines (352 loc) · 10.2 KB
/
Copy pathhelper.js
File metadata and controls
380 lines (352 loc) · 10.2 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
var helper = (function() {
var e = function(selector) {
return document.querySelector(selector);
};
var eA = function(selector) {
return document.querySelectorAll(selector);
};
var toggleClass = function(element, theClassName) {
element.classList.toggle(theClassName);
};
var addClass = function(element, theClassName) {
element.classList.add(theClassName);
};
var removeClass = function(element, theClassName) {
element.classList.remove(theClassName);
};
var getDateTime = function() {
var dateStamp = new Date();
var object = {
// string: dateStamp.constructor(),
// time: dateStamp.getTime()
date: dateStamp.getDate(),
day: dateStamp.getDay(),
year: dateStamp.getFullYear(),
hours: dateStamp.getHours(),
milliseconds: dateStamp.getMilliseconds(),
minutes: dateStamp.getMinutes(),
month: dateStamp.getMonth(),
seconds: dateStamp.getSeconds()
}
return object;
};
var month = function(index) {
var all = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return all[index];
};
var day = function(index) {
var all = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return all[index];
};
var sortObject = function(object, key) {
object.sort(function(a, b) {
var textA = a[key];
if (typeof textA == "string") {
textA = textA.toLowerCase();
};
var textB = b[key];
if (typeof textB == "string") {
textB = textB.toLowerCase();
};
if (textA < textB) {
return -1;
} else if (textA > textB) {
return 1;
} else {
return 0;
};
});
return object;
};
var applyOptions = function(options, override) {
if (options && override) {
if (override) {
for (var key in override) {
if (key in options) {
options[key] = override[key];
};
};
};
return options;
} else {
return null;
};
};
var hslToRgb = function(hslObject) {
if (hslObject == undefined) {
return null;
};
var chroma = (1 - Math.abs((2 * hslObject.l) - 1)) * hslObject.s;
var huePrime = hslObject.h / 60;
var secondComponent = chroma * (1 - Math.abs((huePrime % 2) - 1));
huePrime = Math.floor(huePrime);
var red;
var green;
var blue;
if (huePrime === 0 || huePrime === 6) {
red = chroma;
green = secondComponent;
blue = 0;
} else if (huePrime === 1) {
red = secondComponent;
green = chroma;
blue = 0;
} else if (huePrime === 2) {
red = 0;
green = chroma;
blue = secondComponent;
} else if (huePrime === 3) {
red = 0;
green = secondComponent;
blue = chroma;
} else if (huePrime === 4) {
red = secondComponent;
green = 0;
blue = chroma;
} else if (huePrime === 5) {
red = chroma;
green = 0;
blue = secondComponent;
};
var lightnessAdjustment = hslObject.l - (chroma / 2);
red += lightnessAdjustment;
green += lightnessAdjustment;
blue += lightnessAdjustment;
var result = {
r: Math.round(red * 255),
g: Math.round(green * 255),
b: Math.round(blue * 255)
};
return result;
};
var hexToRgb = function(hex) {
if (hex == undefined) {
return null;
};
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (result) {
result = {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
};
};
return result;
};
var rgbToHex = function(rgbObject) {
if (rgbObject == undefined) {
return null;
};
var componentToHex = function(hexPart) {
hexPart = hexPart.toString(16);
if (hexPart.length == 1) {
hexPart = "0" + hexPart
};
return hexPart;
};
var result = "#" + componentToHex(rgbObject.r) + componentToHex(rgbObject.g) + componentToHex(rgbObject.b);
return result;
};
var makeNode = function(override) {
var options = {
tag: null,
text: null,
attr: null
};
if (override) {
options = helper.applyOptions(options, override);
};
var element = document.createElement(options.tag);
if (options.text != null) {
element.textContent = options.text;
};
if (options.attr != null) {
options.attr.forEach(function(arrayItem, index) {
if ("key" in arrayItem && "value" in arrayItem) {
element.setAttribute(arrayItem.key, arrayItem.value);
} else if ("key" in arrayItem) {
element.setAttribute(arrayItem.key, "");
}
});
};
return element;
};
function _makeAddress(path) {
var array;
if (path.indexOf("[") != -1 && path.indexOf("]") != -1) {
array = path.split(".").join(",").split("[").join(",").split("]").join(",").split(",");
for (var i = 0; i < array.length; i++) {
if (array[i] == "") {
array.splice(i, 1);
};
if (!isNaN(parseInt(array[i], 10))) {
array[i] = parseInt(array[i], 10);
};
};
} else {
array = path.split(".");
};
return array;
};
function setObject(override) {
var options = {
path: null,
object: null,
newValue: null
};
if (override) {
var options = applyOptions(options, override);
};
var address = _makeAddress(options.path);
var _setData = function() {
while (address.length > 1) {
// shift off and store the first key
var currentKey = address.shift();
// if the key is not found make a new object
if (!(currentKey in options.object)) {
// make an empty object in the current object level
if (isNaN(currentKey)) {
options.object[currentKey] = {};
} else {
options.object[currentKey] = [];
};
};
// drill down the object with the first key
options.object = options.object[currentKey];
};
var finalKey = address.shift();
options.object[finalKey] = options.newValue;
};
if (options.object != null && options.path != null && options.newValue != null) {
_setData();
} else {
return false;
};
};
function getObject(override) {
var options = {
object: null,
path: null
};
if (override) {
var options = applyOptions(options, override);
};
var address = _makeAddress(options.path);
var _getData = function() {
while (address.length > 1) {
// shift off and store the first key
var currentKey = address.shift();
// if the key is not found make a new object
if (!(currentKey in options.object)) {
// make an empty object in the current object level
if (isNaN(currentKey)) {
options.object[currentKey] = {};
} else {
options.object[currentKey] = [];
};
};
// drill down the object with the first key
options.object = options.object[currentKey];
};
var finalKey = address.shift();
if (!(finalKey in options.object)) {
return "";
} else {
return options.object[finalKey];
};
};
if (options.object != null && options.path != null) {
return _getData();
} else {
return false;
};
};
function makeObject(string) {
var _stringOrBooleanOrNumber = function(stringToTest) {
if (stringToTest == "true") {
return true;
} else if (stringToTest == "false") {
return false;
} else if (stringToTest.indexOf("#") != -1) {
return stringToTest.substr(1, kevValuePair[1].length);
} else {
return "\"" + stringToTest + "\"";
};
};
// if argument is a string
if (typeof string == "string") {
// start building the object
var objectString = "{";
// split the string on comma not followed by a space
// split on character if not followed by a space
var items = string.split(/,(?=\S)/);
// loop over each item
for (var i = 0; i < items.length; i++) {
// split each would be object key values pair
// split on character if not followed by a space
var kevValuePair = items[i].split(/:(?=\S)/);
// get the key
var key = "\"" + kevValuePair[0] + "\"";
var value;
// if the value has + with a space after it
if (/\+(?=\S)/.test(kevValuePair[1])) {
// remove first + symbol
kevValuePair[1] = kevValuePair[1].substr(1, kevValuePair[1].length);
// split the would be values
// split on character if not followed by a space
var all_value = kevValuePair[1].split(/\+(?=\S)/);
// if there are multiple values make an array
value = "["
for (var q = 0; q < all_value.length; q++) {
value += _stringOrBooleanOrNumber(all_value[q]) + ",";
};
// remove last comma
value = value.substr(0, value.length - 1);
// close array
value += "]"
} else {
value = _stringOrBooleanOrNumber(kevValuePair[1]);
};
objectString += key + ":" + value + ",";
};
// remove last comma
objectString = objectString.substr(0, objectString.length - 1);
// close object
objectString += "}";
var object = JSON.parse(objectString);
return object;
} else {
return false;
};
};
var allEqual = function(array) {
return array.every(function(arrayItem) {
return arrayItem === array[0];
});
};
var randomNumber = function(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
};
// exposed methods
return {
e: e,
eA: eA,
toggleClass: toggleClass,
addClass: addClass,
removeClass: removeClass,
allEqual: allEqual,
getDateTime: getDateTime,
month: month,
day: day,
sortObject: sortObject,
applyOptions: applyOptions,
hexToRgb: hexToRgb,
rgbToHex: rgbToHex,
hslToRgb: hslToRgb,
makeNode: makeNode,
setObject: setObject,
getObject: getObject,
makeObject: makeObject,
randomNumber: randomNumber
};
})();