Last active
August 23, 2018 13:24
-
-
Save cognitivim/9a554b0fe841a554b6d3d7f81d7adf28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ComponentType } from 'react'; | |
| /** | |
| * function that performs no operations | |
| */ | |
| const noop = Object.freeze(() => { | |
| /**/ | |
| }); | |
| const arrayToMap = (list: any) => list.reduce( | |
| (acc: any, item: any) => ({ | |
| ...acc, | |
| [item.id]: item, | |
| }), | |
| {} | |
| ); | |
| function deepClone(object: object) { | |
| const serialized = JSON.stringify(object); | |
| return JSON.parse(serialized); | |
| } | |
| const delay = (duration: number) => new Promise(resolve => window.setTimeout(resolve, duration)); | |
| const capitalize = (str: string) => str[0].toUpperCase() + str.slice(1); | |
| const capitalizeAll = (str: string) => | |
| str | |
| .split(' ') | |
| .map(capitalize) | |
| .join(' '); | |
| const isEmpty = (obj: object) => Object.keys(obj).length === 0; | |
| const roundTo = (num: number, places: number = 0) => Math.round(num * 10 ** places) / 10 ** places; | |
| const random = (min: number, max: number) => Math.floor(Math.random() * (max - min)) + min; | |
| const enum SortDirection { | |
| DESC, | |
| ASC | |
| } | |
| interface ISort { | |
| direction: SortDirection; | |
| fieldName: string; | |
| } | |
| function arraySort(sort: ISort, flatData: any[]) { | |
| // TODO async | |
| return flatData.sort((a, b) => { | |
| if (sort.direction === SortDirection.DESC) { | |
| if (b[sort.fieldName] > a[sort.fieldName]) return -1; | |
| if (a[sort.fieldName] > b[sort.fieldName]) return 1; | |
| } else { | |
| if (b[sort.fieldName] > a[sort.fieldName]) return 1; | |
| if (a[sort.fieldName] > b[sort.fieldName]) return -1; | |
| } | |
| return 0; | |
| }); | |
| } | |
| // react | |
| const getDisplayName = (component: ComponentType<any>) => component.displayName || component.name || 'Component'; | |
| const getHOCDisplayName = (hocName: string, component: ComponentType<any>) => `${hocName}(${getDisplayName(component)})`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment