Skip to content

Instantly share code, notes, and snippets.

View milksense's full-sized avatar
🙂

milksense

🙂
  • Decentralized
View GitHub Profile
@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.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.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 / canUseDOM.ts
Created August 3, 2021 03:18
Проверяет, можно использовать браузерное API в текущем окружении
/**
* Проверяет, можно использовать браузерное API в текущем окружении.
*
* @example
* if (canUseDOM()) {
* document.querySelector('...')
* }
*/
export const canUseDOM = (): boolean => {
return (
@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 / 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 / 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.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.js
Last active June 12, 2022 01:08
Improved Time Zone resolver
/**
* Improved Time Zone resolver
*
* @see {@link https://402.ecma-international.org/1.0/#sec-12.1} reference
* @see {@link https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat} reference
* @see {@link https://en.wikipedia.org/wiki/List_of_tz_database_time_zones} reference
*
* @return {String|NULL}
*/
@milksense
milksense / index.sh
Created April 30, 2021 17:50
Updating pip to latest version
/usr/bin/python3 -m pip install --upgrade pip