Skip to content

Instantly share code, notes, and snippets.

@MeyCry
MeyCry / do-sha-hashing.ts
Created June 6, 2024 12:40
Sha hashing in browser
export async function doShaHashing(message: string, algorithm: 'SHA-256' | 'SHA-384' | 'SHA-512') {
// Check if the specified algorithm is supported
const supportedAlgorithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'];
if (!supportedAlgorithms.includes(algorithm.toLowerCase())) {
throw new Error(`Unsupported algorithm: ${algorithm}`);
}
// Encode the message as a Uint8Array
const encoder = new TextEncoder();
const data = encoder.encode(message);
@MeyCry
MeyCry / indexeddb-helper.ts
Last active May 18, 2023 13:31
indexeddb-helper.ts
// indexeddb-helper.ts
class IndexedDBHelper {
private readonly dbName: string;
private readonly storeName: string;
constructor(dbName: string, storeName: string) {
this.dbName = dbName;
this.storeName = storeName;
}
@MeyCry
MeyCry / index.js
Created May 30, 2020 19:05
Get from event submit name of input as key and value of input as value of object.
const data = new FormData(ev.target);
const dataObj = Object.fromEntries(data.entries());
@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;
}
@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.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 / 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 / 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 / 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);
function getBoundingClientRect(el) {
return new Promise((res, rej) => {
requestAnimationFrame(() => {
res(el.getBoundingClientRect());
});
});
}