Skip to content

Instantly share code, notes, and snippets.

@MeyCry
MeyCry / getUrlParameter.js
Last active August 10, 2018 15:00
get get param from url
function getUrlParameter(param) {
var pageUrl = decodeURIComponent(window.location.search.substring(1)),
urlVariables = pageUrl.split('&'),
parameterName,
i;
for (i = 0; i < urlVariables.length; i++) {
parameterName = urlVariables[i].split('=');
if (parameterName[0] === param) {
@MeyCry
MeyCry / js
Created August 17, 2018 17:09
time in millisecond
// time const in millisecond
export const SECOND = 1000;
export const MINUTE = 60 * SECOND; // 60 000
export const HOUR = 60 * MINUTE; // 3 600 000
export const DAY = 24 * HOUR; // 86 400 000
export const WEEK = 7 * DAY; // 604 800 000
// export const YEAR = 365 * DAY; // 31 536 000 000
export const YEAR = 365.25 * DAY; // 31 557 600 000
@MeyCry
MeyCry / ObjectArray.js
Last active October 15, 2018 15:59
ObjectArray =))
class ObjectArray extends Object {
/**
* @param {Object} obj
*/
constructor(obj = {}) {
super();
this.length = 0;
const keys = Object.keys(obj);
keys.forEach((key) => {
this.length += 1;
function getBoundingClientRect(el) {
return new Promise((res, rej) => {
requestAnimationFrame(() => {
res(el.getBoundingClientRect());
});
});
}
@MeyCry
MeyCry / str-finder.js
Last active December 12, 2019 18:03
find longest substring in string
function find2(str) {
let _helper = [];
return str
.split('')
.reduce((acum, item, i, originalArr) => {
if (_helper.some(char => char === item)) {
acum.push(_helper);
_helper = [];
}
_helper.push(item);
@MeyCry
MeyCry / flatter.js
Last active January 20, 2020 22:53
flatter for array
function flatter(arr) {
return arr.flat(Infinity);
}
flatter(
[1, 2, [3, 4, [5]], 6, [[7, [8]], 9]],
);
function flatter2(arr) {
const arrCopy = arr.slice();
@MeyCry
MeyCry / ReturnedValueFromObjectMethodByKey.ts
Last active March 31, 2020 14:11
TypeScript Returned Value From Object Method By Key
/**
* example:
* const aObj = {
* b: () => 42,
* c: () => "hello"
* }
* type ObjExample = ReturnedValueFromObjectMethodByKey<typeof aObj>;
* ObjExample type will be: {b: number; c: string}
*/
@MeyCry
MeyCry / index.js
Created April 15, 2020 21:27
validate brackets and merge sorted arrays.
(function() {
// 1
function smartJoin(arrA, arrB) {
let aIndex = 0;
let bIndex = 0;
const result = [];
while (aIndex < arrA.length || bIndex < arrB.length) {
const itemA = arrA[aIndex];
@MeyCry
MeyCry / index.ts
Created May 1, 2020 17:56
throttle function
/**
* Call fn not more often but and not less then ms
* @param {function} fn - function what need to call not often but and not less then ms
* @param {number} ms - time in milliseconds
* @return (any[]) => void
*/
export const throttle = (fn: (...args: any[]) => void, ms: number = 0) => {
let timeoutId = null;
let lastCall = 0;
@MeyCry
MeyCry / index.tsx
Created May 6, 2020 18:54
reject react router v5 navigate
import { Location } from "history";
import React, { useEffect, useState } from "react";
import { Prompt } from "react-router-dom";
import { SubmitDialogComponent } from "./Modal";
interface Props {
when?: boolean | undefined; // just use true
navigate: (path: string) => void; // (path) => history.push(path)
shouldBlockNavigation: (location: Location) => boolean;
}