-
Notifications
You must be signed in to change notification settings - Fork 519
/
helpers.js
239 lines (206 loc) · 5.61 KB
/
helpers.js
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
// @flow
import { isValidElement } from 'react';
import { createPortal } from 'react-dom';
import ExecutionEnvironment from 'exenv';
import is from 'is-lite';
export const { canUseDOM } = ExecutionEnvironment;
export const isReact16 = createPortal !== undefined;
/**
* Get the current browser
*
* @param {string} userAgent
*
* @returns {String}
*/
export function getBrowser(userAgent: string = navigator.userAgent): string {
let browser = userAgent;
if (typeof window === 'undefined') {
browser = 'node';
} else if (document.documentMode) {
browser = 'ie';
} else if (/Edge/.test(userAgent)) {
browser = 'edge';
}
// Opera 8.0+
else if (Boolean(window.opera) || userAgent.indexOf(' OPR/') >= 0) {
browser = 'opera';
}
// Firefox 1.0+
else if (typeof window.InstallTrigger !== 'undefined') {
browser = 'firefox';
}
// Chrome 1+
else if (window.chrome) {
browser = 'chrome';
}
// Safari (and Chrome iOS, Firefox iOS)
else if (/(Version\/([0-9._]+).*Safari|CriOS|FxiOS| Mobile\/)/.test(userAgent)) {
browser = 'safari';
}
return browser;
}
/**
* Get the toString Object type
* @param {*} value
* @returns {string}
*/
export function getObjectType(value: any): string {
return Object.prototype.toString
.call(value)
.slice(8, -1)
.toLowerCase();
}
/**
* Get text from React components
*
* @param {*} root
*
* @returns {string}
*/
export function getText(root: any): string {
const content = [];
const recurse = child => {
/* istanbul ignore else */
if (typeof child === 'string' || typeof child === 'number') {
content.push(child);
} else if (Array.isArray(child)) {
child.forEach(c => recurse(c));
} else if (child && child.props) {
const { children } = child.props;
if (Array.isArray(children)) {
children.forEach(c => recurse(c));
} else {
recurse(children);
}
}
};
recurse(root);
return content.join(' ').trim();
}
export function hasOwnProperty(value: Object, key: string): boolean {
return Object.prototype.hasOwnProperty.call(value, key);
}
export function hasValidKeys(value: Object, keys: Array<any>): boolean {
if (!is.plainObject(value) || !is.array(keys)) {
return false;
}
return Object.keys(value).every(d => keys.includes(d));
}
/**
* Convert hex to RGB
*
* @param {string} hex
* @returns {Array}
*/
export function hexToRGB(hex: string): Array<number> {
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
const properHex = hex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(properHex);
return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : [];
}
/**
* Decide if the step shouldn't skip the beacon
* @param {Object} step
*
* @returns {boolean}
*/
export function hideBeacon(step: Object): boolean {
return step.disableBeacon || step.placement === 'center';
}
/**
* Compare if two variables are equal
*
* @param {*} left
* @param {*} right
*
* @returns {boolean}
*/
export function isEqual(left: any, right: any): boolean {
let type;
const hasReactElement = isValidElement(left) || isValidElement(right);
const hasUndefined = is.undefined(left) || is.undefined(right);
if (getObjectType(left) !== getObjectType(right) || hasReactElement || hasUndefined) {
return false;
}
if (is.domElement(left)) {
return left.isSameNode(right);
}
if (is.number(left)) {
return left === right;
}
if (is.function(left)) {
return left.toString() === right.toString();
}
for (const key in left) {
/* istanbul ignore else */
if (hasOwnProperty(left, key)) {
if (typeof left[key] === 'undefined' || typeof right[key] === 'undefined') {
return false;
}
type = getObjectType(left[key]);
if (['object', 'array'].includes(type) && isEqual(left[key], right[key])) {
continue;
}
if (type === 'function' && isEqual(left[key], right[key])) {
continue;
}
if (left[key] !== right[key]) {
return false;
}
}
}
for (const p in right) {
/* istanbul ignore else */
if (hasOwnProperty(right, p)) {
if (typeof left[p] === 'undefined') {
return false;
}
}
}
return true;
}
/**
* Detect legacy browsers
*
* @returns {boolean}
*/
export function isLegacy(): boolean {
return !['chrome', 'safari', 'firefox', 'opera'].includes(getBrowser());
}
/**
* Log method calls if debug is enabled
*
* @private
* @param {Object} arg
* @param {string} arg.title - The title the logger was called from
* @param {Object|Array} [arg.data] - The data to be logged
* @param {boolean} [arg.warn] - If true, the message will be a warning
* @param {boolean} [arg.debug] - Nothing will be logged unless debug is true
*/
export function log({ title, data, warn = false, debug = false }: Object) {
/* eslint-disable no-console */
const logFn = warn ? console.warn || console.error : console.log;
if (debug) {
if (title && data) {
console.groupCollapsed(
`%creact-joyride: ${title}`,
'color: #ff0044; font-weight: bold; font-size: 12px;',
);
if (Array.isArray(data)) {
data.forEach(d => {
if (is.plainObject(d) && d.key) {
logFn.apply(console, [d.key, d.value]);
} else {
logFn.apply(console, [d]);
}
});
} else {
logFn.apply(console, [data]);
}
console.groupEnd();
} else {
console.error('Missing title or data props');
}
}
/* eslint-enable */
}