@checksub_team/peaks_timeline 1.4.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.js ADDED
@@ -0,0 +1,339 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Some general utility functions.
5
+ *
6
+ * @module utils
7
+ */
8
+
9
+ define([
10
+ 'uuid'
11
+ ], function(UUID) {
12
+ 'use strict';
13
+
14
+ if (typeof Number.isFinite !== 'function') {
15
+ Number.isFinite = function isFinite(value) {
16
+ if (typeof value !== 'number') {
17
+ return false;
18
+ }
19
+
20
+ // Check for NaN and infinity
21
+ // eslint-disable-next-line no-self-compare
22
+ if (value !== value || value === Infinity || value === -Infinity) {
23
+ return false;
24
+ }
25
+
26
+ return true;
27
+ };
28
+ }
29
+
30
+ function zeroPad(number) {
31
+ return number < 10 ? '0' + number : number;
32
+ }
33
+
34
+ return {
35
+
36
+ /**
37
+ * Returns a formatted time string.
38
+ *
39
+ * @param {Number} time The time to be formatted, in seconds.
40
+ * @param {Boolean} dropHundredths Don't display hundredths of a second if true.
41
+ * @returns {String}
42
+ */
43
+
44
+ formatTime: function(time, dropHundredths) {
45
+ var result = [];
46
+
47
+ time = this.roundTime(time);
48
+
49
+ var hundredths = Math.round((time % 1) * 100);
50
+ var seconds = Math.floor(time);
51
+ var minutes = Math.floor(seconds / 60);
52
+ var hours = Math.floor(minutes / 60);
53
+
54
+ if (hours > 0) {
55
+ result.push(hours); // Hours
56
+ }
57
+ result.push(minutes % 60); // Mins
58
+ result.push(seconds % 60); // Seconds
59
+
60
+ for (var i = 0; i < result.length; i++) {
61
+ result[i] = zeroPad(result[i]);
62
+ }
63
+
64
+ result = result.join(':');
65
+
66
+ if (!dropHundredths) {
67
+ result += '.' + zeroPad(hundredths);
68
+ }
69
+
70
+ return result;
71
+ },
72
+
73
+ /**
74
+ * Rounds the given value up to the nearest given multiple.
75
+ *
76
+ * @param {Number} value
77
+ * @param {Number} multiple
78
+ * @returns {Number}
79
+ *
80
+ * @example
81
+ * roundUpToNearest(5.5, 3); // returns 6
82
+ * roundUpToNearest(141.0, 10); // returns 150
83
+ * roundUpToNearest(-5.5, 3); // returns -6
84
+ */
85
+
86
+ roundUpToNearest: function(value, multiple) {
87
+ if (multiple === 0) {
88
+ return 0;
89
+ }
90
+
91
+ var multiplier = 1;
92
+
93
+ if (value < 0.0) {
94
+ multiplier = -1;
95
+ value = -value;
96
+ }
97
+
98
+ var roundedUp = Math.ceil(value);
99
+
100
+ return multiplier * (((roundedUp + multiple - 1) / multiple) | 0) * multiple;
101
+ },
102
+
103
+ roundUpToNearestPositive: function(value, multiple) {
104
+ if (multiple === 0) {
105
+ return 0;
106
+ }
107
+
108
+ var multiplier = 1;
109
+
110
+ if (value < 0.0) {
111
+ value = 0;
112
+ }
113
+
114
+ var roundedUp = Math.ceil(value);
115
+
116
+ return multiplier * (((roundedUp + multiple - 1) / multiple) | 0) * multiple;
117
+ },
118
+
119
+ clamp: function(value, min, max) {
120
+ if (value < min) {
121
+ return min;
122
+ }
123
+ else if (value > max) {
124
+ return max;
125
+ }
126
+ else {
127
+ return value;
128
+ }
129
+ },
130
+
131
+ extend: function(to, from) {
132
+ for (var key in from) {
133
+ if (this.objectHasProperty(from, key)) {
134
+ to[key] = from[key];
135
+ }
136
+ }
137
+
138
+ return to;
139
+ },
140
+
141
+ /**
142
+ * Checks whether the given array contains values in ascending order.
143
+ *
144
+ * @param {Array<Number>} array The array to test
145
+ * @returns {Boolean}
146
+ */
147
+
148
+ isInAscendingOrder: function(array) {
149
+ if (array.length === 0) {
150
+ return true;
151
+ }
152
+
153
+ var value = array[0];
154
+
155
+ for (var i = 1; i < array.length; i++) {
156
+ if (value >= array[i]) {
157
+ return false;
158
+ }
159
+
160
+ value = array[i];
161
+ }
162
+
163
+ return true;
164
+ },
165
+
166
+ /**
167
+ * Checks whether the given value is a boolean.
168
+ *
169
+ * @param {Boolean} value The value to test
170
+ * @returns {Boolean}
171
+ */
172
+
173
+ isBoolean: function(value) {
174
+ return typeof value === 'boolean';
175
+ },
176
+
177
+ /**
178
+ * Checks whether the given value is a number.
179
+ *
180
+ * @param {Number} value The value to test
181
+ * @returns {Boolean}
182
+ */
183
+
184
+ isNumber: function(value) {
185
+ return typeof value === 'number';
186
+ },
187
+
188
+ /**
189
+ * Checks whether the given value is an integer.
190
+ *
191
+ * @param {Number} value The value to test
192
+ * @returns {Boolean}
193
+ */
194
+
195
+ isInteger: function(value) {
196
+ return typeof value === 'number' && Number.isInteger(value);
197
+ },
198
+
199
+ /**
200
+ * Checks whether the given value is a valid timestamp.
201
+ *
202
+ * @param {Number} value The value to test
203
+ * @returns {Boolean}
204
+ */
205
+
206
+ isValidTime: function(value) {
207
+ return (typeof value === 'number') && Number.isFinite(value);
208
+ },
209
+
210
+ /**
211
+ * Checks whether the given value is a valid object.
212
+ *
213
+ * @param {Object|Array} value The value to test
214
+ * @returns {Boolean}
215
+ */
216
+
217
+ isObject: function(value) {
218
+ return (value !== null) && (typeof value === 'object')
219
+ && !Array.isArray(value);
220
+ },
221
+
222
+ /**
223
+ * Checks whether the given value is a valid string.
224
+ *
225
+ * @param {String} value The value to test
226
+ * @returns {Boolean}
227
+ */
228
+
229
+ isString: function(value) {
230
+ return typeof value === 'string';
231
+ },
232
+
233
+ /**
234
+ * Checks whether the given value is a valid ArrayBuffer.
235
+ *
236
+ * @param {ArrayBuffer} value The value to test
237
+ * @returns {Boolean}
238
+ */
239
+
240
+ isArrayBuffer: function(value) {
241
+ return Object.prototype.toString.call(value).includes('ArrayBuffer');
242
+ },
243
+
244
+ /**
245
+ * Checks whether the given value is null or undefined.
246
+ *
247
+ * @param {Object} value The value to test
248
+ * @returns {Boolean}
249
+ */
250
+
251
+ isNullOrUndefined: function(value) {
252
+ return value === undefined || value === null;
253
+ },
254
+
255
+ /**
256
+ * Checks whether the given value is a function.
257
+ *
258
+ * @param {Function} value The value to test
259
+ * @returns {Boolean}
260
+ */
261
+
262
+ isFunction: function(value) {
263
+ return typeof value === 'function';
264
+ },
265
+
266
+ /**
267
+ * Checks whether the given value is a valid HTML element.
268
+ *
269
+ * @param {HTMLElement} value The value to test
270
+ * @returns {Boolean}
271
+ */
272
+
273
+ isHTMLElement: function(value) {
274
+ return value instanceof HTMLElement;
275
+ },
276
+
277
+ /**
278
+ * Checks whether the given value is a valid CSS color.
279
+ *
280
+ * @param {String} value The value to test
281
+ * @returns {Boolean}
282
+ */
283
+
284
+ isValidColor: function(value) {
285
+ var s = new Option().style;
286
+
287
+ s.color = value;
288
+
289
+ return s.color !== '';
290
+ },
291
+
292
+ objectHasProperty: function(object, field) {
293
+ return Object.prototype.hasOwnProperty.call(object, field);
294
+ },
295
+
296
+ roundTime: function(rawTime) {
297
+ return parseFloat((Math.round(rawTime * 100) / 100).toFixed(2));
298
+ },
299
+
300
+ removeLineBreaks: function(rawText) {
301
+ var text = rawText.replace(/(\r\n|\n|\r)/gm, ' ');
302
+
303
+ return text.replace(/ +(?= )/g,'');
304
+ },
305
+
306
+ createUuidv4: function() {
307
+ return UUID.v4();
308
+ },
309
+
310
+ shadeColor: function(color, percent) {
311
+ var R, G, B;
312
+
313
+ if (color.length === 4) {
314
+ R = parseInt(2 * color[1], 16);
315
+ G = parseInt(2 * color[2], 16);
316
+ B = parseInt(2 * color[3], 16);
317
+ }
318
+ else {
319
+ R = parseInt(color.substring(1,3), 16);
320
+ G = parseInt(color.substring(3,5), 16);
321
+ B = parseInt(color.substring(5,7), 16);
322
+ }
323
+
324
+ R = Math.round(R * (100 + percent) / 100);
325
+ G = Math.round(G * (100 + percent) / 100);
326
+ B = Math.round(B * (100 + percent) / 100);
327
+
328
+ R = (R < 255) ? R : 255;
329
+ G = (G < 255) ? G : 255;
330
+ B = (B < 255) ? B : 255;
331
+
332
+ var RR = ((R.toString(16).length === 1) ? '0' + R.toString(16) : R.toString(16));
333
+ var GG = ((G.toString(16).length === 1) ? '0' + G.toString(16) : G.toString(16));
334
+ var BB = ((B.toString(16).length === 1) ? '0' + B.toString(16) : B.toString(16));
335
+
336
+ return '#' + RR + GG + BB;
337
+ }
338
+ };
339
+ });