Skip to content

Instantly share code, notes, and snippets.

@Reshetnyak
Reshetnyak / Array_methods.md
Last active April 18, 2023 15:59
Array methods cheat sheet

Filter

When to use filter

Filter is used when we need to remove some elements from an array by some condition

How to use filter

It expects a function which returns boolean. If this function returns true, that means current element will be added to returned array. If this function returns false, it wont. Docs:

@Reshetnyak
Reshetnyak / Number.types.task.js
Created June 24, 2022 13:14
TS types task. Number types.
const age = 30;
let yearsOfExperience = 0.5;
const notANumber = NaN;
// we know exactly that we have only 4 possible values: 1, 2, 3, 4.
let accessRigth = 2;
accessRigth = 5; // should be a TS error;
yearsOfExperience = 'hello'; // should be a TS error;
// 'target' property of the tsconfig.json file has to be `ES2020`;
@Reshetnyak
Reshetnyak / type_guard_helpers.ts
Last active February 5, 2020 09:38
Type guard helpers
/**
* @type T {Object} expected type of provided object
* @param obj {Object} object
* @param prop {string} key of an object which indicates expected type
* @returns {boolean} provided object is type T
*
* @example
* interface Base {
* name: string;
* age: number;
enum Types {
Start = 'start',
End = 'end',
Hello = 'hello'
};
const freezed = Object.freeze({
Start: 'start' as const,
End: 'end' as const,
Hello = 'hello' as const
@Reshetnyak
Reshetnyak / classCreator.ts
Last active February 24, 2019 08:11
Redux Actions class creator and actionCreatorFactory
function actionCreator<
C extends new (...args) => { type: string, payload: any },
T extends InstanceType<C>['type'],
P extends InstanceType<C>['payload']
>(action: C): (payload: P) => { type: InstanceType<C>['type'], payload: P }
function actionCreator<
C extends new (...args) => { type: string },
T extends InstanceType<C>['type']
//@ts-check
const express = require('express');
const session = require('express-session');
const app = express();
const MAX_AGE = 5000;
// Use the session middleware
@Reshetnyak
Reshetnyak / get.ts
Created July 27, 2018 12:58
Safely get deep nested property of object
function get<
T,
K extends keyof T,
T1 extends T[K],
K1 extends keyof T1,
T2 extends T1[K1],
K2 extends keyof T2,
T3 extends T2[K2],
K3 extends keyof T3,
R extends T3[K3]
@Reshetnyak
Reshetnyak / getKeys.ts
Last active May 22, 2018 13:19
Object.keys behavior but with typings
// function getKeys<T>(obj: T): Array<keyof T> {
// let keys: Array<keyof T> = [];
// for (const key in obj) {
// keys.push(key);
// }
// return keys;
// }
function getKeys<T, K extends keyof T>(obj: T): Array<K> {
return <Array<K>>Object.keys(obj)
@Reshetnyak
Reshetnyak / ReduxActionCreator2.ts
Created March 8, 2018 17:53
Action creator function
// needs to be feagure out creation of type Actions = increment | openModal....
function actionCreator<T>(type: T): { type: T, new(): {type: T} }
function actionCreator<T, P>(type: T): { type: T, new(payload: P): {type: T, payload: P} }
function actionCreator<T, P>(type: T): { new(...args: any[]): {type: T, payload?: P} } {
return class {
static type: T = type;
type: T = type;
@Reshetnyak
Reshetnyak / ReduxActionCreators.ts
Created March 8, 2018 12:19
Redux Action creators. Class + enum approach
/*
* 'type' function is taken from here https://github.com/Reshetnyak/toolbox/blob/master/type/type.ts
*/
/* [start] current approach */
// action without payload
export const OPEN_MODAL_TYPE = type('OPEN_MODAL');
export interface OpenModalAction {
type: typeof OPEN_MODAL_TYPE;
}
export function openModalAction(): OpenModalAction {