shlink-web-client/src/utils/utils.ts

32 lines
886 B
TypeScript
Raw Normal View History

2020-08-22 18:32:48 +02:00
import { isEmpty, isNil, range } from 'ramda';
export type OrderDir = 'ASC' | 'DESC' | undefined;
2020-08-22 19:03:25 +02:00
export const determineOrderDir = (currentField: string, newField: string, currentOrderDir?: OrderDir): OrderDir => {
2020-08-22 18:32:48 +02:00
if (currentField !== newField) {
return 'ASC';
}
const newOrderMap: Record<'ASC' | 'DESC', OrderDir> = {
ASC: 'DESC',
DESC: undefined,
};
return currentOrderDir ? newOrderMap[currentOrderDir] : 'ASC';
};
export const rangeOf = <T>(size: number, mappingFn: (value: number) => T, startAt = 1): T[] =>
range(startAt, size + 1).map(mappingFn);
export type Empty = null | undefined | '' | never[];
export const hasValue = <T>(value: T | Empty): value is T => !isNil(value) && !isEmpty(value);
export type Nullable<T> = {
[P in keyof T]: T[P] | null
};
2020-08-26 20:03:23 +02:00
type Optional<T> = T | null | undefined;
export type OptionalString = Optional<string>;