import { isEmpty, isNil, pipe, range } from 'ramda'; import type { SyntheticEvent } from 'react'; type Optional = T | null | undefined; export type OptionalString = Optional; export const handleEventPreventingDefault = (handler: () => T) => pipe( (e: SyntheticEvent) => e.preventDefault(), handler, ); export const rangeOf = (size: number, mappingFn: (value: number) => T, startAt = 1): T[] => range(startAt, size + 1).map(mappingFn); export type Empty = null | undefined | '' | never[]; export const hasValue = (value: T | Empty): value is T => !isNil(value) && !isEmpty(value); export type Nullable = { [P in keyof T]: T[P] | null }; export const nonEmptyValueOrNull = (value: T): T | null => (isEmpty(value) ? null : value); export type BooleanString = 'true' | 'false'; export const parseBooleanToString = (value: boolean): BooleanString => (value ? 'true' : 'false'); export const parseOptionalBooleanToString = (value?: boolean): BooleanString | undefined => ( value === undefined ? undefined : parseBooleanToString(value) );