Skip to content

Instantly share code, notes, and snippets.

View js2me's full-sized avatar
🚙
I may be slow to respond.

Sergey S. Volkov js2me

🚙
I may be slow to respond.
View GitHub Profile
@js2me
js2me / random.ts
Created August 29, 2021 22:43
randoms ts
import * as getRandomWord from "random-words";
export const getRandomFloat = <T extends number = number>(min = 0, max = 1): T => {
return (Math.random() * (max - min) + min) as T;
};
export const getRandomInt = <T extends number = number>(min = 0, max = 1): T => {
if (min === max) return min as T;
return Math.round(getRandomFloat(min, max)) as T;
@js2me
js2me / status.ts
Created June 18, 2021 22:41
Effector status of effects
import {
Effect,
Store,
combine,
guard,
createEvent,
restore,
sample,
} from "effector";
import { status as patronumStatus } from "patronum";
@js2me
js2me / undoRedoEffector.ts
Created March 22, 2021 13:03
Undo redo effecto draft
import { combine, createEvent, createStore, Event, guard, sample, StoreValue } from "effector";
export const createUndoRedo = <S extends unknown>({ defaultState, filter, limit, name }: UndoRedoOptions<S>) => {
const storeName = name || "unknown-store";
const $present = createStore(defaultState, { name: `history/${storeName}/present` });
const limitPerTime = Math.round(limit / 2);
const $past = createStore<S[]>([], { name: `history/${storeName}/past` });
const $future = createStore<S[]>([], { name: `history/${storeName}/future` });
@js2me
js2me / launch.json
Last active February 16, 2021 17:24
ts-node debugger for vs code
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
// https://github.com/wclr/ts-node-dev/issues/9
"version": "0.2.0",
"configurations": [
{
"name":"ts-node (src/index.ts)",
"type":"node",
@js2me
js2me / utils.ts
Last active February 4, 2021 13:17
Effector utils for own usage
import { Effect, Event, Store, createEvent, combine } from "effector";
import { status } from "patronum";
import { EffectState } from "patronum/status";
export interface FetchingState {
loading: boolean;
done: boolean;
fail: boolean;
}
@js2me
js2me / useFocused.ts
Created July 13, 2020 11:29
React hook with focused element
import { useState, RefObject, useEffect } from "react";
export const useFocused = (inputRef: RefObject<HTMLInputElement>): boolean => {
const [isFocused, setFocused] = useState(false);
useEffect(() => {
if (inputRef.current) {
const handleFocus = (): void => setFocused(true);
const handleBlur = (): void => setFocused(false);
@js2me
js2me / createMemoContainer.ts
Created April 30, 2020 10:05
createMemoContainer
/**
*
* @param initialValue {T} initial value
* @param callback {function} used to update value
*/
export const createMemoContainer = <T, ExtraArgs extends unknown[] = []>(
initialValue: T,
callback: (currentValue: T, ...args: ExtraArgs) => T | null,
): ((...args: ExtraArgs) => T) => {
@js2me
js2me / string.js
Created March 1, 2020 09:24
get hash from string
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
@js2me
js2me / launch.json
Created February 16, 2020 12:53 — forked from cecilemuller/launch.json
Run ts-node in VSCode Debugger
{
"version": "0.2.0",
"configurations": [
{
"name": "Node Inspector",
"type": "node",
"request": "launch",
"args": ["${workspaceRoot}/src/service.ts"],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
@js2me
js2me / mongoose.ts
Created January 27, 2020 20:26
mongoose little enhacenment of schema types
import { Schema } from "mongoose"
const modifierGetters = {
'array': input => [{...input}],
'required': input => {
const inputClone = input instanceof Array ? [{...input[0]}] : {...input}
if (inputClone instanceof Array) {
inputClone[0].required = true;