1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 23:55:01 +00:00
mon-entreprise/site/source/utils.ts
Jérémy Rialland 80be3f9229
#2007 add yes no switch (#2037)
* Ajout de storybook

Ajout d'un Switch oui/non

* Fix lint

* Add react-router and fix type

* Fix lint

* Resolution du conflit de version de prettier avec storybook

* Fix storybook

* Reduce Storybook bundle size
Refacto css in QuickLinks
Remove useless comment
Add default theme to CSS prop

* Déploiement de Storybook

* Fix déploiement

* Fix déploiement storybook url

* Fix Switch style

* Remplace les oui/non radio bouton par un Switch

* Filter aria props + react props in Storybook controls
Sort props in Storybook
Add global style in Storybook decorator

* Update Storybook packages

* Ajout d'un debounce dans OuiNonInput

* Fix du Switch

* Refacto des alias

* Fix lint error avec Storybook

* Fix eslint error

* Refacto deploy for Storybook

* Ajout de type pour les yaml d'economie collaborative
Ajout de type sur les fonction du locale storage
+ Autre fix de type

* Deploy storybook in dist dir in prod

* Fix focus on Switch

* Fix cy test

*  Remplace l'alias ~ par @

*  Refacto du Switch

* Remplace la checkbox par un Switch dans ChiffreAffairesActivitéMixte

* Ajout des stories RadioGroup et ToggleGroup

* Remplace le Switch oui/non par un ToggleGroup

* Ajout d'un label dans le Switch
Ajout du mode light sur le Switch

* Fix autofocus

* Fix cypress test

* 🐛 Ajout du polyfill replaceAll

* Test de deploiement

* Ajout d'une redirection pour Storybook

* Fix Storybook url

* Fix du deploiement de Storybook
2022-03-03 14:37:53 +01:00

95 lines
2.5 KiB
TypeScript

import { formatValue } from 'publicodes'
export function capitalise0(name: undefined): undefined
export function capitalise0(name: string): string
export function capitalise0(name?: string) {
return name && name[0].toUpperCase() + name.slice(1)
}
export const debounce = <T>(waitFor: number, fn: (...args: T[]) => void) => {
let timeoutId: ReturnType<typeof setTimeout>
return (...args: T[]) => {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => fn(...args), waitFor)
}
}
export const fetcher = (url: RequestInfo) => fetch(url).then((r) => r.json())
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 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
}
}
export const currencyFormat = (language: string) => ({
isCurrencyPrefixed: !!formatValue(12, { language, displayedUnit: '€' }).match(
/^€/
),
thousandSeparator: formatValue(1000, { language }).charAt(1),
decimalSeparator: formatValue(0.1, { language }).charAt(1),
})
export function hash(str: string): number {
let hash = 0
let chr
for (let i = 0; i < str.length; i++) {
chr = str.charCodeAt(i)
hash = (hash << 5) - hash + chr
hash |= 0 // Convert to 32bit integer
}
return hash
}
export function omit<T, K extends keyof T>(obj: T, key: K): Omit<T, K> {
const returnObject = { ...obj }
delete returnObject[key]
return returnObject
}
export function isIterable<T>(obj: unknown): obj is Iterable<T> {
return Symbol.iterator in Object(obj)
}
/**
* Check if a key exists in the object and return its value or undefined,
* usefull for type check
* @param obj any object
* @param key key to get value from object
* @returns value or undefined
*/
export const getValueFrom = <
T extends Record<string | number | symbol, unknown>,
K extends string | number | symbol
>(
obj: T,
key: K
): Extract<T, { [k in K]?: unknown }>[K] | undefined =>
key in obj ? obj[key] : undefined