-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathutils.js
More file actions
73 lines (62 loc) · 2.41 KB
/
Copy pathutils.js
File metadata and controls
73 lines (62 loc) · 2.41 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
import React from 'react';
export function insertScript(src, id, parentElement) {
const script = window.document.createElement('script');
script.async = true;
script.src = src;
script.id = id;
parentElement.appendChild(script);
return script;
}
export function removeScript(id, parentElement) {
const script = window.document.getElementById(id);
if (script)
parentElement.removeChild(script);
}
export function removeResources() {
// Remove the bundles that the Disqus scripts add to prevent duplicated resources when navigating between pages
const disqusResources = window.document.querySelectorAll(
// eslint-disable-next-line max-len
'link[href*="disquscdn.com/next/embed"], link[href*="disquscdn.com/next/recommendations"], link[href*="disqus.com/next/config.js"], script[src*="disquscdn.com/next/embed"], script[src*="disqus.com/count-data.js"], iframe[title="Disqus"]'
);
disqusResources.forEach(el => el.remove());
}
export function debounce(func, wait, runOnFirstCall) {
let timeout;
return function () {
const context = this; // eslint-disable-line consistent-this
const args = arguments;
const deferredExecution = function () {
timeout = null;
if (!runOnFirstCall)
func.apply(context, args);
};
const callNow = runOnFirstCall && !timeout;
window.clearTimeout(timeout);
timeout = setTimeout(deferredExecution, wait);
if (callNow)
func.apply(context, args);
};
}
export function isReactElement(element) {
if (React.isValidElement(element)) {
return true;
} else if (Array.isArray(element)) {
return element.some((value) =>
React.isValidElement(value)
);
}
return false;
}
export function shallowComparison(currentProps, nextProps) {
// Perform a comparison of all props, excluding React Elements, to prevent unnecessary updates
const propNames = new Set(Object.keys(currentProps), Object.keys(nextProps)); // eslint-disable-line no-undef
for (const name of propNames) {
if (typeof currentProps[name] === 'object') {
if (shallowComparison(currentProps[name], nextProps[name]))
return true;
} else if (currentProps[name] !== nextProps[name] && !isReactElement(currentProps[name])) {
return true;
}
}
return false;
}