This file contains 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
/** | |
* Reduce array of items into the Record with provided keys | |
* reduceToRecord('id', 'name', items): [{ id: 'id1', name: 'John', type: 'web' }] => { id1: 'John' } | |
*/ | |
export const reduceToRecord = <T, U>(keyName: string, valueName: string, items: U[]): Record<string, T> => { | |
const keyWithNameReducer = (acc: Record<string, T>, item: U) => { | |
const key = item[keyName]; | |
const value = item[valueName]; | |
return { |
This file contains 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
const getUniqueId = () => { | |
return Math.random() | |
.toString(36) | |
.replace(/[^a-z]+/g, '') | |
.substring(0, 5); | |
}; |
This file contains 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
const randomElement = (arr) => arr[Math.floor(Math.random() * arr.length)]; |
This file contains 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
enum Status { | |
DONE: 'done', | |
ERROR: 'error' | |
} | |
const a: Partial<Record<Status, string>> = {} | |
// or | |
const a: { [K in Status]?: string } = {} |
This file contains 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
a, button { | |
touch-action: manipulation; | |
} |
This file contains 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
<!-- Models --> | |
interface FilterModel { | |
name: string; | |
date: [number, number]; | |
} | |
type PredicamentFn = <T>(item: T, filters: FilterModel) => boolean; | |
<!-- Matchers --> | |
const FILTERS_MATCHERS: Record<keyof FilterModel, PredicamentFn> = { | |
name: (item, filters) => filters.name.includes(item.name), |
This file contains 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
<!-- Helpers --> | |
const isFiniteNumber = (n: any): n is number => Number.isFinite(n); | |
const isString = (s: any): s is string => s === '' || (s && typeof s.valueOf() === 'string'); | |
const matchId = (valueToMatch: string) => (item: { id: string }) => valueToMatch === item.id; | |
const sortBy = <T>(valueGetterFn: (item: T) => number | string, direction: 'asc' | 'desc') => (itemA: T, itemB: T): number => { | |
const a = valueGetterFn(itemA); | |
const b = valueGetterFn(itemB); |
This file contains 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
const hasValueValidator = (formGroup: FormGroup) => { | |
const hasValue = (val: string | string[]) => Array.isArray(val) ? val.length > 0 : !!val; | |
const formIsFilled = Object.values(formGroup.value).some(hasValue); | |
return formIsFilled ? null : { empty: true }; | |
} |
This file contains 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
constructor( | |
private componentFactoryResolver: ComponentFactoryResolver, | |
private appRef: ApplicationRef, | |
private injector: Injector | |
) { } | |
appendComponentToBody(component: any) { | |
// 1. Create a component reference from the component | |
const componentRef = this.componentFactoryResolver | |
.resolveComponentFactory(component) |
This file contains 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
var copy = JSON.parse(JSON.stringify(tags)); |
NewerOlder