Skip to content

Instantly share code, notes, and snippets.

View fostyfost's full-sized avatar
🤪
All you need is Half-Life 3

Fosty Fost fostyfost

🤪
All you need is Half-Life 3
View GitHub Profile
@fostyfost
fostyfost / toOrderedMap.ts
Created April 30, 2020 16:05
ImmutableJS: `toOrderedMap` util
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fromJS, OrderedMap } from 'immutable';
interface ObjectWithId {
id: number;
[property: string]: any;
}
export const toOrderedMap = <T = any>(arr: ObjectWithId[]): OrderedMap<number, T> => {
return arr.reduce(
@fostyfost
fostyfost / ImmutableFrom.ts
Created May 8, 2020 13:27
Immutable.js ImmutableFrom TS helper
import { Map } from 'immutable';
type ExpectedPrimitive = boolean | number | string | symbol | null;
/**
* Generic конвертор неиммутабельного интерфейса в иммутабельный.
* В отличие от RecordOf вычисляет return type для getIn и вложенных полей.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface ImmutableFrom<Interface> extends Map<string, any> {
@fostyfost
fostyfost / foreach-generator.js
Created May 29, 2020 15:09
Util: foreach-generator (ex. for redux-saga)
export default function* foreach(array, fn, context) {
let i = 0;
for (; i < array.length; i++) {
yield* fn.call(context, array[i], i, array);
}
}
@fostyfost
fostyfost / useInterval.ts
Last active October 30, 2020 14:46
useInterval react hook
import { useEffect, useRef } from 'react';
export const useInterval = (callback: () => void, timeout?: number): void => {
const intervalId = useRef<number>();
const savedCallback = useRef<() => void>();
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
@fostyfost
fostyfost / animate.test.ts
Created November 18, 2020 13:05
Raf animate
import createStub from 'raf-stub';
import { animate, AnimateUtilParams } from '../animate';
describe('`animate` util tests', () => {
const params = {} as AnimateUtilParams;
const DEFAULT_DURATION = 300;
const FRAME_DURATION = 10;
const DEFAULT_FRAMES_COUNT = DEFAULT_DURATION / FRAME_DURATION + 1;
@fostyfost
fostyfost / unflat.js
Created December 8, 2020 19:49 — forked from szanata/unflat.js
Unflat js object
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
@fostyfost
fostyfost / get-string-ref-counter.ts
Created May 25, 2021 15:47
Get string ref counter
export interface RefCounter<T = unknown> {
getCount(item: T): number
add(item: T): void
remove(item: T): void
}
export const getStringRefCounter = (): RefCounter<string> => {
const values: Record<string, number | undefined> = {}
const getCount = (key: string): number => values[key] || 0
@fostyfost
fostyfost / comparable-map.ts
Created July 4, 2021 19:28
comparable-map.ts
export interface ComparableMap<K = string, V = unknown> {
keys: K[]
get(key: K): V | undefined
add(key: K, value: V): void
remove(key: K): V | undefined
}
/**
* We will use it where we can not use the default Map as the Map class do not allow custom compare function.
*
@fostyfost
fostyfost / saga-equals.ts
Created July 6, 2021 08:11
Saga equals function
export interface SagaWithArguments<T = any> {
saga: (argument?: T) => Iterator<any>
argument?: T
}
export type SagaRegistration<T = any> = (() => Iterator<any>) | SagaWithArguments<T>
export const sagaEquals = (sagaA: SagaRegistration, sagaB: SagaRegistration): boolean => {
if (typeof sagaA === 'function' && typeof sagaB === 'function') {
return sagaA === sagaB
@fostyfost
fostyfost / extract-return-types.d.ts
Last active July 6, 2021 13:39
Extract return type
type ExtractReturnTypes<T extends ((...args: unknown[]) => unknown)[]> = [
...{ [K in keyof T]: T[K] extends ((...args: unknown[]) => infer R) ? R : never }
]