mirror of
https://github.com/betagouv/mon-entreprise
synced 2025-02-09 05:15:02 +00:00
Cela permet l'inférence de type à partir des fichiers js qui ne sont pas encore convertis en TypeScript. Par ailleurs suppression des dernières traces de Flow. Ajout d'options plus strictes pour dans la config tsconfig.js
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
export let capitalise0 = (name: string): string =>
|
|
name && name[0].toUpperCase() + name.slice(1)
|
|
|
|
export function debounce<ArgType>(
|
|
timeout: number,
|
|
fn: (arg?: ArgType) => void
|
|
): (arg?: ArgType) => void {
|
|
let timeoutId: ReturnType<typeof setTimeout>
|
|
return (...args) => {
|
|
clearTimeout(timeoutId)
|
|
timeoutId = setTimeout(() => fn(...args), timeout)
|
|
}
|
|
}
|
|
|
|
export function isIE(): boolean {
|
|
return (
|
|
navigator.appName == 'Microsoft Internet Explorer' ||
|
|
(navigator.appName == 'Netscape' &&
|
|
new RegExp('Trident/.*rv:([0-9]{1,}[.0-9]{0,})').exec(
|
|
navigator.userAgent
|
|
) != null)
|
|
)
|
|
}
|
|
|
|
export function inIframe(): boolean {
|
|
try {
|
|
return window.self !== window.top
|
|
} catch (e) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
export function softCatch<ArgType, ReturnType>(
|
|
fn: (arg: ArgType) => ReturnType
|
|
): (arg: ArgType) => ReturnType | null {
|
|
return function(...args) {
|
|
try {
|
|
return fn(...args)
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn(e)
|
|
return null
|
|
}
|
|
}
|
|
}
|
|
|
|
export function mapOrApply<A, B>(fn: (a: A) => B, x: A): B
|
|
export function mapOrApply<A, B>(fn: (a: A) => B, x: Array<A>): Array<B>
|
|
export function mapOrApply(fn, x) {
|
|
return Array.isArray(x) ? x.map(fn) : fn(x)
|
|
}
|
|
|
|
export function coerceArray<A>(x: A | Array<A>): Array<A> {
|
|
return Array.isArray(x) ? x : [x]
|
|
}
|
|
|
|
export function getSessionStorage() {
|
|
// In some browsers like Brave, even just reading the variable sessionStorage
|
|
// is throwing an error in the iframe, so we can't do things if sessionStorage !== undefined
|
|
// and we need to wrap it in a try { } catch { } logic
|
|
try {
|
|
return window.sessionStorage
|
|
} catch (e) {
|
|
return undefined
|
|
}
|
|
}
|