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 / 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 / utils.d.ts
Last active March 1, 2023 10:05
My used TypeScript advanced types
type Maybe<T> = T | null | undefined;
/**
* Позволяет переопределить тип свойства {PropertyName} у типа {T} на новое значение {NewValue}
*/
type Override<T, PropertyName extends keyof T, NewValue> = Omit<T, PropertyName> &
{ [K in keyof T]: K extends PropertyName ? NewValue : T[K] };
interface FixedLengthArray<T extends unknown, L extends number> extends Array<T> {
@js2me
js2me / copy.ts
Last active December 17, 2021 09:40
deep copy js
export const clone = <O extends unknown | unknown[]>(value: O, history?: Set<unknown>): O => {
if (value == null || typeof value !== "object") return value;
const stack = history || new WeakSet();
if (stack.has(value)) {
return value;
}
stack.add(value);
@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;
@js2me
js2me / md5.js
Created January 2, 2020 23:16
MD5 browser javascript
MD5 = function(d){result = M(V(Y(X(d),8*d.length)));return result.toLowerCase()};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r<d.length;r++)_=d.charCodeAt(r),f+=m.charAt(_>>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<<m%32;return _}function V(d){for(var _="",m=0;m<32*d.length;m+=8)_+=String.fromCharCode(d[m>>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16){var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+4],7,-176418897),f,r,d[n+5],12,1200080426),m,f,d[n+6],17,-1473231341),i,m,d[n+7
@js2me
js2me / media-query.css
Created December 27, 2019 10:57 — forked from AyoAlfonso/media-query.css
CSS Media Queries for Desktop, Tablet, Mobile.
/*
##Device = Desktops
##Screen = 1281px to higher resolution desktops
*/
@media (min-width: 1281px) {
//CSS