2018-06-20 14:09:14 +00:00
|
|
|
/* @flow */
|
2019-02-18 14:36:09 +00:00
|
|
|
import { map } from 'ramda'
|
2018-06-20 14:09:14 +00:00
|
|
|
|
|
|
|
export let capitalise0 = (name: string) => name[0].toUpperCase() + name.slice(1)
|
2018-01-18 14:53:20 +00:00
|
|
|
|
|
|
|
export let getUrl = () => window.location.href.toString()
|
2018-05-14 08:47:20 +00:00
|
|
|
|
2018-06-20 14:09:14 +00:00
|
|
|
export let parseDataAttributes = (value: any) =>
|
2018-05-14 08:47:20 +00:00
|
|
|
value === 'undefined'
|
|
|
|
? undefined
|
|
|
|
: value === null
|
2018-11-20 18:34:45 +00:00
|
|
|
? null
|
|
|
|
: !isNaN(value)
|
|
|
|
? +value
|
|
|
|
: /* value is a normal string */
|
|
|
|
value
|
2018-05-14 08:47:20 +00:00
|
|
|
|
2018-06-20 14:09:14 +00:00
|
|
|
export let getIframeOption = (optionName: string) => {
|
2018-01-18 14:53:20 +00:00
|
|
|
let url = getUrl(),
|
|
|
|
hasOption = url.includes(optionName + '=')
|
2018-05-14 08:47:20 +00:00
|
|
|
return parseDataAttributes(
|
|
|
|
hasOption && url.split(optionName + '=')[1].split('&')[0]
|
|
|
|
)
|
2018-01-18 14:53:20 +00:00
|
|
|
}
|
2018-06-13 08:26:27 +00:00
|
|
|
|
2018-09-30 18:38:55 +00:00
|
|
|
export function isNumeric(val: number) {
|
|
|
|
return Number(parseFloat(val)) === val
|
|
|
|
}
|
2018-06-20 14:09:14 +00:00
|
|
|
|
|
|
|
export function debounce<ArgType: any>(
|
|
|
|
timeout: number,
|
|
|
|
fn: ArgType => void
|
|
|
|
): ArgType => void {
|
|
|
|
let timeoutId
|
|
|
|
return (...args) => {
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
timeoutId = setTimeout(() => fn(...args), timeout)
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 13:14:55 +00:00
|
|
|
|
|
|
|
export function isIE() {
|
|
|
|
return (
|
|
|
|
navigator.appName == 'Microsoft Internet Explorer' ||
|
|
|
|
(navigator.appName == 'Netscape' &&
|
|
|
|
new RegExp('Trident/.*rv:([0-9]{1,}[.0-9]{0,})').exec(
|
|
|
|
navigator.userAgent
|
|
|
|
) != null)
|
|
|
|
)
|
|
|
|
}
|
2018-07-23 13:21:00 +00:00
|
|
|
|
|
|
|
export const mapDispatchWithRouter = (actionCreators: Object) => (
|
|
|
|
dispatch: (...any) => void,
|
|
|
|
ownProps: Object
|
|
|
|
) =>
|
|
|
|
map(
|
|
|
|
actionCreator => (...args) =>
|
|
|
|
dispatch(actionCreator(...args, ownProps.router)),
|
|
|
|
actionCreators
|
|
|
|
)
|
2018-08-23 14:42:47 +00:00
|
|
|
|
|
|
|
export function inIframe() {
|
|
|
|
try {
|
|
|
|
return window.self !== window.top
|
|
|
|
} catch (e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 13:20:57 +00:00
|
|
|
|
2018-12-05 14:49:56 +00:00
|
|
|
export function softCatch<ArgType: any, ReturnType: any>(
|
|
|
|
fn: ArgType => ReturnType
|
|
|
|
): ArgType => ReturnType | null {
|
|
|
|
return function(...args) {
|
|
|
|
try {
|
|
|
|
return fn(...args)
|
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn(e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 14:36:09 +00:00
|
|
|
export function mapOrApply<A, B>(fn: A => B, x: Array<A> | A): Array<B> | B {
|
2019-02-01 12:31:57 +00:00
|
|
|
return Array.isArray(x) ? x.map(fn) : fn(x)
|
|
|
|
}
|
2019-01-31 18:30:39 +00:00
|
|
|
|
2019-01-23 17:04:22 +00:00
|
|
|
export const constructSitePaths = (
|
|
|
|
root: string,
|
|
|
|
{ index, ...sitePaths }: { index: string }
|
|
|
|
) => ({
|
|
|
|
index: root + index,
|
|
|
|
...map(
|
|
|
|
value =>
|
|
|
|
typeof value === 'string'
|
|
|
|
? root + index + value
|
|
|
|
: typeof value === 'function'
|
2019-01-31 18:30:39 +00:00
|
|
|
? (...args) => mapOrApply(x => root + index + x, value(...args))
|
2019-01-23 17:04:22 +00:00
|
|
|
: constructSitePaths(root + index, value),
|
|
|
|
sitePaths
|
|
|
|
)
|
|
|
|
})
|