Skip to content

Instantly share code, notes, and snippets.

View milksense's full-sized avatar
🙂

milksense

🙂
  • Decentralized
View GitHub Profile
@milksense
milksense / index.js
Created June 1, 2021 02:40
Is WEBP supported
let webpSupported = undefined;
/* export ES6+ */ async function isWebpSupported() {
if (webpSupported !== undefined) {
return webpSupported;
}
const promise = new Promise(resolve => {
const image = new Image();
image.onload = function () {
@milksense
milksense / index.ts
Created June 4, 2021 05:01
Decimal to Hexadecimal converter
const hex = (e: number): string => '0x' + e .toString(16);
/* Usage: console.log(hex(255)) */
@milksense
milksense / index.ts
Created July 27, 2021 03:51
TypeScript type to return a deep readonly object (recursively)
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
export type Builtin = Primitive | Function | Date | Error | RegExp;
/**
* TypeScript type to return a deep readonly object (recursively).
*/
// prettier-ignore
export type DeepReadonly<T> = T extends Builtin
? T
: T extends Map<infer K, infer V>
@milksense
milksense / windowFocusedHook.ts
Created August 3, 2021 02:50
windowFocusedHook | React
import { useEffect, useState } from 'react';
export const useWindowFocused = () => {
const [windowIsActive, setWindowIsActive] = useState(true);
function handleActivity(forcedFlag) {
if (typeof forcedFlag === 'boolean') {
return forcedFlag ? setWindowIsActive(true) : setWindowIsActive(false);
}
@milksense
milksense / canUseDOM.ts
Created August 3, 2021 03:18
Проверяет, можно использовать браузерное API в текущем окружении
/**
* Проверяет, можно использовать браузерное API в текущем окружении.
*
* @example
* if (canUseDOM()) {
* document.querySelector('...')
* }
*/
export const canUseDOM = (): boolean => {
return (
@milksense
milksense / index.js
Last active October 26, 2021 02:16
Get current FPS via TS
"use strict";
let fps = 1;
let times = [];
const debug = true;
const fpsLoop = (timestamp = 0, debug) => {
while (times.length > 0 && times[0] <= timestamp - 1000) {
times.shift();
}
times.push(timestamp);
fps = times.length;
@milksense
milksense / index.js
Created October 28, 2021 16:43
Display greeting by time via TS
"use strict";
const greeting = () => {
const day = new Date();
const hour = day.getHours();
let result = null;
if (hour >= 5 && hour < 12) {
result = "Good morning";
}
else {
if (hour >= 12 && hour < 18) {
@milksense
milksense / index.ts
Created March 25, 2022 18:37
RU, 2022 | Bank Identification Number (1000+)
// https://github.yandex-team.ru/passport-frontend/core/blob/develop/passport/blocks/react-blocks/morda/new/bank_cards/bin_map.js
export const bins: Record<string, string> = {
'424436': 'akbars',
'424440': 'akbars',
'424438': 'akbars',
'424437': 'akbars',
'424439': 'akbars',
'677088': 'akbars',
'410243': 'alfabank',
@milksense
milksense / index.ts
Created March 29, 2022 23:29
JWT Decoder written in TypeScript [pasted]
const decodeJWT = (token: string): string =>
decodeURIComponent(
atob(token.split(".")[1].replace("-", "+").replace("_", "/"))
.split("")
.map((c) => `%${("00" + c.charCodeAt(0).toString(16)).slice(-2)}`)
.join("")
);
console.log(
decodeJwt(`
@milksense
milksense / index.ts
Created June 3, 2022 07:01
Ukraine, 2022 | Bank Identification Number (5122)
const urkBins: number[] = [446236, 401856, 457733, 446302, 532099, 446237, 535563, 676264, 676264, 676621, 535206, 535206, 535206, 535206, 446234, 441793, 410242, 535838, 413284, 535284, 440535, 516735, 441793, 433423, 476065, 440535, 557815, 557116, 532457, 532457, 676568, 537523, 516871, 410332, 526259, 676634, 676634, 512100, 676645, 535700, 676645, 516783, 554299, 446235, 516911, 533194, 516937, 547304, 547304, 535284, 538852, 516800, 516951, 535285, 535284, 515503, 521441, 527412, 522531, 522531, 413051, 516954, 535466, 545542, 535284, 512306, 557394, 559045, 557110, 557110, 544379, 544379, 559524, 557814, 557109, 512306, 557109, 546040, 524830, 526961, 558260, 526961, 557721, 559521, 558259, 557474, 535117, 535560, 535560, 542463, 544219, 546362, 541940, 543121, 512572, 535561, 535639, 535560, 512557, 535639, 540637, 540637, 540637, 540637, 535639, 535560, 535639, 512468, 512468, 512468, 404877, 535639, 545784, 515470, 544568, 545605, 404879, 559531, 516764, 535560, 557437, 677210, 548883, 522119, 51407