forked from datcuemil/helper-functions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions
707 lines (574 loc) · 17.2 KB
/
functions
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// TODO: add description & params to each function
export const replaceNLWithBreakspace = text => {
return text.replace(/\n\r?/g, '<br />');
};
export const replaceBreakspaceWithNL = text => {
return text.replace(/<br \/>?/gm, '\n');
};
export const containsSpecialCharacters = str => {
return /[~`!#$%^&*+=\-\[\]\\';,/{}|":<>?]/g.test(str);
};
export const isPhoneNumber = str => {
return /(\+\d)?\(?(\d{4})\)?[\s-]?\d{3}[\s-]?\d{3}/g.test(str);
};
export const containsOnlyWhitespaces = str => {
const regexStr = `\\s{${str.length}}`;
const regex = new RegExp(regexStr, 'g');
return regex.test(str);
};
export const capitalizeFirstLetter = string => {
return string.charAt(0).toUpperCase() + string.slice(1);
};
/**
* Returns the text which is before a specified character(first instance of the character)
*
* > getStringBeforeChar('https://github.com/datcuemil/helper-functions', '/');
* < "https:"
*
* @param {string} str
* @param {string} char
* @return {string}
*/
export const getStringBeforeChar = (str, char) => {
return str.substring(0, str.indexOf(char));
};
/**
* Returns the text which is after a specified character(last instance of the character)
*
* > getStringAfterChar('https://github.com/datcuemil/helper-functions', '/');
* < "helper-functions"
*
* @param {string} str
* @param {string} char
* @return {string}
*/
export const getStringAfterChar = (str, char) => {
return str.split(char).pop();
};
/**
* Coverts a string to a camelCase string and returns it
*
* > getCamelCaseText('background-color');
* < "backgroundColor"
*
* @param {string} str
* @return {string}
*/
export const getCamelCaseText = str => {
if (!str) {
return '';
}
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => {
return index === 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
};
// does the same thing as getCamelCaseText, different regex syntax
export const camelize = (str: string) => {
if (!str) {
return '';
}
return str.toLowerCase().replace(/[-_\s]+(.)?/g, (match, chr) => {
return chr ? chr.toUpperCase() : '';
});
};
export const generateUniqueId = () => {
return `${Math.random().toString(36).substr(2, 10)}`;
};
export const createHiddenIframe = src => {
const iFrame = document.createElement('iframe');
iFrame.style.display = 'none';
iFrame.src = src;
iFrame.onload = () => {
document.body.removeChild(iFrame);
};
document.body.appendChild(iFrame);
};
export const undef = x => typeof x === 'undefined';
export const getAsteriskValue = value => {
if (!value) {
return '';
}
return value.toString().replace(/./g, '*');
};
export const toUTCString = (currentDate: Date): string => {
return [
currentDate.getFullYear(),
('0' + (currentDate.getMonth() + 1)).slice(-2),
('0' + currentDate.getDate()).slice(-2)
].join('-');
};
export const generateIdUnsafe = () => `temp_${Math.random().toString(36).slice(2)}`;
export const getMonthString = (date) => {
const monthIndex = date.getMonth();
const monthNames = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];
return monthNames[monthIndex];
};
export const getShortDate = (d) => {
const date = new Date(d);
const day = date.getDate();
const monthString = getMonthString(date);
return `${day} ${monthString}`;
};
export const getCommaSeparatedValue = (val, decimals = false) => {
const value = val;
if (val) {
val = val.toString()
.split('.')[0]
.replace(/^0+/g, '')
.replace(/\D+/g, '')
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
if (decimals) {
const splt = value.toString().split('.');
return `${val}.${splt[1]}`;
}
if (val === 0) {
return null;
}
if (value < 0) {
return `-${val}`;
}
return val;
};
export const getLastPunctuationIndex = text => {
const punctuations = ['.', '?', '!', '...'];
const punctuationPosition = punctuations.map(punctuation => text.lastIndexOf(punctuation));
const max = Math.max(...punctuationPosition);
const fallback = text.lastIndexOf(' ') > -1 ? text.lastIndexOf(' ') : text.length;
return max > -1 ? max : fallback;
};
export const ieMenuFixOpen = (open: boolean) => {
if (open) {
document.querySelector('body').className += ' ie-menu-fix';
} else {
const className = document.querySelector('body').className;
document.querySelector('body').className = className.replace(' ie-menu-fix', '');
}
};
export const scrollTo = (element, to, duration) => {
if (duration <= 0) {
element.scrollTop = to;
return;
}
const difference = to - element.scrollTop;
const perTick = difference / duration * 10;
setTimeout(() => {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) {
return;
}
scrollTo(element, to, duration - 10);
}, 10);
};
export const findElementTop = obj => {
let top = 0;
if (obj.offsetParent) {
do {
top += obj.offsetTop;
} while (obj = obj.offsetParent);
return top;
}
return top;
};
export const scrollToTop = (duration = 0) => {
scrollTo(document.documentElement, 0, duration); // For Chrome, Firefox, IE and Opera
scrollTo(document.body, 0, duration); // For Safari
};
export const deviceSupportsTouch = () => 'ontouchstart' in document.documentElement;
export const parseFormattedNumberToInt = (number: string) => {
const parsed = parseInt((number || '').toString().split('.')[0].replace(/\D+/g, ''), 10);
if (isNaN(parsed)) {
return null;
}
return parsed;
};
export const replaceStringResourceParameters = (str, ...parameters) => {
parameters.forEach((v, i) => {
str = (str || '').replace(`{${i}}`, v);
});
return str;
};
export const optionsWithDefaults = ({
centerX,
centerY,
startDegrees,
endDegrees,
thickness,
innerRadius,
outerRadius,
}) => {
const o = {
cx: centerX || 0,
cy: centerY || 0,
startRadians: (startDegrees || 0) * Math.PI / 180,
closeRadians: (endDegrees || 0) * Math.PI / 180,
};
const t = thickness !== undefined ? thickness : 100;
let r1, r2;
if (innerRadius !== undefined) {
r1 = innerRadius;
} else if (outerRadius !== undefined) {
r1 = outerRadius - t;
} else {
r1 = 200 - t;
}
if (outerRadius !== undefined) {
r2 = outerRadius;
} else {
r2 = r1 + t;
}
if (r1 < 0) {
r1 = 0;
}
if (r2 < 0) {
r2 = 0;
}
return {
...o,
r1,
r2,
};
};
export const annularSector = (options) => {
const opts = optionsWithDefaults(options);
const p = [
[opts.cx + opts.r2 * Math.cos(opts.startRadians),
opts.cy + opts.r2 * Math.sin(opts.startRadians)],
[opts.cx + opts.r2 * Math.cos(opts.closeRadians),
opts.cy + opts.r2 * Math.sin(opts.closeRadians)],
[opts.cx + opts.r1 * Math.cos(opts.closeRadians),
opts.cy + opts.r1 * Math.sin(opts.closeRadians)],
[opts.cx + opts.r1 * Math.cos(opts.startRadians),
opts.cy + opts.r1 * Math.sin(opts.startRadians)],
];
const angleDiff = opts.closeRadians - opts.startRadians;
const largeArc = (angleDiff % (Math.PI * 2)) > Math.PI ? 1 : 0;
const cmds = [
'M' + p[0].join(),
'A' + [opts.r2, opts.r2, 0, largeArc, 1, p[1]].join(),
'L' + p[2].join(),
'A' + [opts.r1, opts.r1, 0, largeArc, 0, p[3]].join(),
'z'
];
return cmds.join(' ');
};
/**
* Adds delay/debounceTime to a function and it's used before the function
* like this -> @debounce()
* @param delay: number
*/
export function debounce (delay: number = 300): MethodDecorator {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
let timeout = null;
const original = descriptor.value;
descriptor.value = function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => original.apply(this, args), delay);
};
return descriptor;
};
}
export const removeTrailingLineBreaks = (text) => {
return text.replace(/(?:<br\s*\/?\s*>)+\s*$/, '');
};
export const removeLastSentence = (text) => {
const lastSeparator = Math.max(
text.lastIndexOf('.'),
text.lastIndexOf('!'),
text.lastIndexOf('?')
);
const revtext = text.split('').reverse().join('');
const sep = revtext.search(/[A-Z][\s | (;psbn&) | (>\s*\/?\s*rb<)]+(\")?[\.\!\?]/);
const lastTagPos = revtext.search(/\/\</);
const lastTag = lastTagPos > -1 ? text.length - revtext.search(/\/\</) - 2 : text.length;
const lastPtr = lastTag > lastSeparator ? lastTag : text.length;
if (sep > -1) {
const fText = revtext.substring(sep + 1, revtext.length).trim().split('').reverse().join('');
const lText = text.substring(lastPtr, text.length).replace(/['"]/g, '').trim();
return fText + lText;
}
const lastBr = text.lastIndexOf('<br>');
if (lastBr > -1) {
return text.substring(0, lastBr);
}
return '';
};
export const removeLastWord = (text) => {
const lastSeparator = Math.max(
text.lastIndexOf(' '),
text.lastIndexOf('<br>')
);
if (lastSeparator > -1) {
return text.substring(0, lastSeparator);
}
return '';
};
export const removeNode = (node: Node) => {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
};
export function sliceContentToFit(
{ classes = <any>[], styles = <any>{}, lineHeight },
postBody,
readMoreExpand,
POST_MAX_TEXT_LINES,
previousPostBody = null,
removeWords = false
) {
const container = document.createElement('div');
Object.keys(styles).forEach(k => {
container.style[k] = styles[k];
});
container.style.visibility = 'hidden';
container.setAttribute('class', classes.join(' '));
container.innerHTML = postBody;
document.body.appendChild(container);
const containerClientRect = container.getBoundingClientRect();
const numberOfLines = Math.ceil(containerClientRect.height / lineHeight);
const innerHTML = container.innerHTML;
if (previousPostBody && previousPostBody === innerHTML) {
removeNode(container);
return innerHTML;
}
const seeMoreLink = container.querySelector('.js-see-more');
const readMoreLink = container.querySelector('.js-read-more');
if (seeMoreLink) {
removeNode(seeMoreLink);
}
if (readMoreLink) {
removeNode(readMoreLink);
}
let innerHTMLWithoutSeeAndReadMore = container.innerHTML.trim();
if (
container.hasChildNodes() &&
container.childNodes.length === 1 &&
!['#text', 'A', 'BR'].includes(container.childNodes[0]['nodeName'])
) {
innerHTMLWithoutSeeAndReadMore = container.childNodes[0]['innerHTML'].trim();
}
removeNode(container);
if (numberOfLines > POST_MAX_TEXT_LINES) {
const anchorString = `<a class="js-see-more read-more tenant-primary-txt-color">${readMoreExpand}</a>`;
if (!removeWords) {
const textWithoutLastSentence = removeTrailingLineBreaks(
removeLastSentence(innerHTMLWithoutSeeAndReadMore)
);
if (textWithoutLastSentence) {
const textWithAnchor = `${textWithoutLastSentence} ${anchorString}`;
return sliceContentToFit(
{ classes, styles, lineHeight },
textWithAnchor,
readMoreExpand,
POST_MAX_TEXT_LINES,
innerHTML
);
}
}
const textWithoutLastWord = removeTrailingLineBreaks(
removeLastWord(innerHTMLWithoutSeeAndReadMore)
);
if (textWithoutLastWord) {
const textWithAnchor = `${textWithoutLastWord} ${anchorString}`;
return sliceContentToFit(
{ classes, styles, lineHeight },
textWithAnchor,
readMoreExpand,
POST_MAX_TEXT_LINES,
innerHTML,
true
);
}
}
return innerHTML;
}
export const postHasReadMoreLinkInContent = (postEntity) => {
return !postEntity.ActionName || !postEntity.ReadMoreLink;
};
export const sliceText = (
postEntity,
fontSize,
lineHeight,
containerWidth,
readMoreExpand,
readMoreLink,
POST_MAX_TEXT_LINES
) => {
if (!postEntity || !postEntity.Body) {
return '';
}
const postBody = postEntity.Body.replace(/<[^\/>][^>]*><\/[^>]+>/, '');
const anchor = `<a href="${postEntity.ReadMoreLink}"
class="js-read-more read-more tenant-primary-txt-color">${readMoreLink}</a>`;
const postBodyWithAnchor = `${postBody} ${anchor}`;
try {
return sliceContentToFit(
{
styles: {
fontSize: `${fontSize}px`,
lineHeight: `${lineHeight}px`,
width: `${containerWidth}px`,
wordWrap: 'break-word',
wordBreak: 'break-word',
},
lineHeight,
},
postHasReadMoreLinkInContent(postEntity) && postEntity.ReadMoreLink
? postBodyWithAnchor : postBody,
readMoreExpand,
POST_MAX_TEXT_LINES
);
} catch (e) {
return postBody;
}
};
export const transformElement = (element, transform, { x = '0px', y = '0px' } = {}) => {
Object.assign(element.style, {
mozTransform: transform,
mozTransformOrigin: `${x} ${y}`,
oTransform: transform,
oTransformOrigin: `${x} ${y}`,
webkitTransform: transform,
webkitTransformOrigin: `${x} ${y}`,
transform: transform,
transformOrigin: `${x} ${y}`,
});
};
export const deleteAllCookies = () => {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf('=');
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
}
};
export const shuffleString = (str) => (
str.split('').sort(() => 0.5 - Math.random()).join('')
);
export const groupBy = (xs, key) => (
xs.reduce((rv, x) => {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {})
);
export const humanFileSize = (bytes, si) => {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
do {
bytes /= thresh;
++u;
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1) + ' ' + units[u];
};
export const compare = (a, b, isAsc) => {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
};
export const isIOSDevice = () => {
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window['MSStream'];
};
export const isIE = () => {
return navigator.userAgent.match('MSIE 10.0;') ||
navigator.userAgent.indexOf('MSIE') !== -1 ||
navigator.appVersion.indexOf('Trident/') > 0;
};
export const isFirefox = () => {
return navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
};
export const isSafari = () => {
return navigator.vendor && navigator.vendor.indexOf('Apple') > -1 &&
navigator.userAgent && navigator.userAgent.indexOf('CriOS') === -1 &&
navigator.userAgent.indexOf('FxiOS') === -1;
};
export const validFilterObject = obj => {
const results = [];
if (obj.hasOwnProperty('Components') && Array.isArray(obj.Components)) {
obj.Components.forEach(component => {
results.push(validFilterObject(component));
});
} else if (obj.Values && Array.isArray(obj.Values)) {
if (obj.Values && obj.Values.length > 0 && obj.Values[0] !== '') {
return true;
}
return false;
}
return !results.some(res => res === false);
};
export const getFilteredFilterObject = obj => {
let values;
if (obj.hasOwnProperty('Components') && Array.isArray(obj.Components)) {
values = obj.Components.filter(com => {
if (com.hasOwnProperty('Components') && !!com.Components) {
const val = getFilteredFilterObject(com);
return val && val.length > 0 && val[0] !== '' &&
val.filter(x => !!x && x !== '').length > 0;
}
return com.Values && com.Values.length > 0 && com.Values[0] !== '' &&
(com.Values.filter(v => !!v && v !== '').length === com.Values.length);
});
}
return values;
};
export const getReadingTime = (text, hasImage, wpm = 250) => {
let words = 0;
let start = 0;
let end = text.length - 1;
let i;
const wordsPerMinute = wpm;
const wordBound = (c) => {
return (
(' ' === c) ||
('\n' === c) ||
('\r' === c) ||
('\t' === c)
);
};
// fetch bounds
while (wordBound(text[start])) {
start++;
}
while (wordBound(text[end])) {
end--;
}
// calculate the number of words
for (i = start; i <= end;) {
for (; i <= end && !wordBound(text[i]); i++) { }
words++;
for (; i <= end && wordBound(text[i]); i++) { }
}
// reading time stats
const minutes = (words / wordsPerMinute);
const extra = hasImage ? 12 : 0;
const time = minutes * 60 * 1000 + extra;
const minutesToDisplay = Math.ceil(minutes);
return {
minutesToDisplay,
minutes: minutes,
time: time,
words: words,
};
};
export const getStrippedText = string => {
const regex = /(<([^>]+)>)/ig;
return string.replace(regex, '');
};
export const elementIsFullyVisible = element => {
const bounding = element.getBoundingClientRect();
return (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};
export const shouldDisplayMobilePicker = () => (
(window.innerHeight || document.documentElement.clientHeight) < 540
);