90 lines
2.2 KiB
JavaScript
90 lines
2.2 KiB
JavaScript
/**
|
|
* Get value of an object property/path even if it's nested
|
|
*/
|
|
export function getValueByPath(obj, path) {
|
|
const value = path.split('.').reduce((o, i) => o[i], obj)
|
|
return value
|
|
}
|
|
|
|
/**
|
|
* Extension of indexOf method by equality function if specified
|
|
*/
|
|
export function indexOf(array, obj, fn) {
|
|
if (!array) return -1
|
|
|
|
if (!fn || typeof fn !== 'function') return array.indexOf(obj)
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
if (fn(array[i], obj)) {
|
|
return i
|
|
}
|
|
}
|
|
|
|
return -1
|
|
}
|
|
|
|
/**
|
|
* Mobile detection
|
|
* https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript
|
|
*/
|
|
export const isMobile = {
|
|
Android: function () {
|
|
return (
|
|
typeof window !== 'undefined' &&
|
|
window.navigator.userAgent.match(/Android/i)
|
|
)
|
|
},
|
|
BlackBerry: function () {
|
|
return (
|
|
typeof window !== 'undefined' &&
|
|
window.navigator.userAgent.match(/BlackBerry/i)
|
|
)
|
|
},
|
|
iOS: function () {
|
|
return (
|
|
typeof window !== 'undefined' &&
|
|
window.navigator.userAgent.match(/iPhone|iPad|iPod/i)
|
|
)
|
|
},
|
|
Opera: function () {
|
|
return (
|
|
typeof window !== 'undefined' &&
|
|
window.navigator.userAgent.match(/Opera Mini/i)
|
|
)
|
|
},
|
|
Windows: function () {
|
|
return (
|
|
typeof window !== 'undefined' &&
|
|
window.navigator.userAgent.match(/IEMobile/i)
|
|
)
|
|
},
|
|
any: function () {
|
|
return (
|
|
isMobile.Android() ||
|
|
isMobile.BlackBerry() ||
|
|
isMobile.iOS() ||
|
|
isMobile.Opera() ||
|
|
isMobile.Windows()
|
|
)
|
|
}
|
|
}
|
|
|
|
export function removeElement(el) {
|
|
if (typeof el.remove !== 'undefined') {
|
|
el.remove()
|
|
} else if (typeof el.parentNode !== 'undefined') {
|
|
el.parentNode.removeChild(el)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Escape regex characters
|
|
* http://stackoverflow.com/a/6969486
|
|
*/
|
|
export function escapeRegExpChars(value) {
|
|
if (!value) return value
|
|
|
|
// eslint-disable-next-line
|
|
return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')
|
|
}
|