Skip to content

Instantly share code, notes, and snippets.

Avatar
🌴
On vacation

Sergey S. Volkov js2me

🌴
On vacation
View GitHub Profile
@js2me
js2me / useFocused.ts
Created Jul 13, 2020
React hook with focused element
View useFocused.ts
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);
View createMemoContainer.ts
/**
*
* @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 Jun 24, 2020
My used TypeScript advanced types
View utils.d.ts
type Nullable<T> = T | null;
type Maybe<T> = Nullable<T> | 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] };
@js2me
js2me / copy.ts
Created Mar 10, 2020
deep copy js
View copy.ts
const copy = (data: any): any => {
if (null == data || typeof data != "object")
return data;
if (data instanceof Date)
return new Date().setTime(data.getTime());
if (data instanceof Array)
return data.map(copy)
var newObject = {};
@js2me
js2me / string.js
Created Mar 1, 2020
get hash from string
View string.js
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 Feb 16, 2020 — forked from cecilemuller/launch.json
Run ts-node in VSCode Debugger
View launch.json
{
"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 Jan 27, 2020
mongoose little enhacenment of schema types
View mongoose.ts
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 Jan 2, 2020
MD5 browser javascript
View md5.js
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 Dec 27, 2019 — forked from AyoAlfonso/media-query.css
CSS Media Queries for Desktop, Tablet, Mobile.
View media-query.css
/*
##Device = Desktops
##Screen = 1281px to higher resolution desktops
*/
@media (min-width: 1281px) {
//CSS
@js2me
js2me / media-query.css
Created Dec 27, 2019 — forked from AyoAlfonso/media-query.css
CSS Media Queries for Desktop, Tablet, Mobile.
View media-query.css
/*
##Device = Desktops
##Screen = 1281px to higher resolution desktops
*/
@media (min-width: 1281px) {
//CSS
You can’t perform that action at this time.