Skip to content

Instantly share code, notes, and snippets.

View krutoo's full-sized avatar

Dmitry Petrov krutoo

  • Ekaterinburg, Russia
View GitHub Profile
@krutoo
krutoo / get-stats-diff.js
Created March 6, 2024 11:19
Get Bun stats sorted diff
import fs from 'node:fs/promises';
const before = await readJson('./stats_before.json');
const after = await readJson('./stats_after.json');
getDiff(before, after)
.sort((a, b) => b[1] - a[1])
.forEach(([key, value]) => {
console.log(key, `+${value}`);
});
@krutoo
krutoo / README.md
Last active October 12, 2023 12:25
Формирование NPM-пакета с поддержкой ESM/CJS/DTS и React 17/18

Формирование NPM-пакета с поддержкой ESM/CJS/DTS и React 17/18

Предисловие

Для того чтобы наверняка понимать содержание данного документа потребуется:

  • понимание того что такое CommonJS
  • понимание того что такое ECMAScript modules
  • понимание того что такое React
  • понимание того что такое TypeScript
@krutoo
krutoo / babel-plugin-node-module-resolution.ts
Last active October 11, 2023 10:06
Babel plugin for remove Node module resolution imports
import { PluginObj } from '@babel/core';
import fs from 'node:fs';
import path from 'node:path';
export default function nodeModuleResolution(): PluginObj {
return {
visitor: {
ImportOrExportDeclaration: {
enter(nodePath, { file }) {
if (!file.opts.filename) {
@krutoo
krutoo / escape.mjs
Created December 27, 2021 16:27
simple jsesc() in worker
import { Worker, isMainThread, parentPort } from 'worker_threads';
import jsesc from 'jsesc';
const generateId = (count => () => count++)(0);
let escape;
if (isMainThread) {
const worker = new Worker(new URL('./escape.mjs', import.meta.url));
@krutoo
krutoo / heplers.js
Created November 25, 2020 15:38
Simplest async image loading
export const loadImage = path => new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => resolve(image);
image.onerror = () => reject(image);
image.src = path;
});
@krutoo
krutoo / createStore.js
Last active October 23, 2020 11:16
minimal version of redux createStore
export const createStore = (reducer, preloadedState, enhancer) => {
if (enhancer) return enhancer(createStore)(reducer, preloadedState);
const listeners = new Set();
let state = reducer(preloadedState, { type: `INIT:${performance.now()}` });
return {
dispatch: action => void (state = reducer(state, action), listeners.forEach(fn => fn())),
subscribe: listener => (listeners.add(listener), () => listeners.delete(listener)),
getState: () => state,
@krutoo
krutoo / sum.js
Created September 21, 2020 03:54
sum(1, 2)(3)(4, 5) == 15
const add = (a, b) => a + b;
const sumList = numbers => numbers.reduce(add, 0);
const sum = (...startValues) => {
let total = sumList(startValues);
const fn = (...values) => (total += sumList(values), fn);
fn.valueOf = () => total;
@krutoo
krutoo / get-scroll-parent.js
Last active July 8, 2020 04:57 — forked from gre/scrollparent.js
get first parent scrollable container of a DOM element
// more minimal version of https://github.com/olahol/scrollparent.js/blob/master/scrollparent.js
const regex = /(auto|scroll)/;
const isScrollable = element => {
let result = false;
if (element) {
const styles = getComputedStyle(element);
result = regex.test(styles.overflow + styles.overflowX + styles.overflowY);
}
@krutoo
krutoo / takeChain.js
Last active June 25, 2020 06:38
takeChain redux-saga custom effect
export const takeChain (timeout, types, task, ...args) => fork(function * () {
while (true) {
const BREAK_TYPE = `BREAK_${performance.now()}`;
const collectedActions = [yield take(types)];
yield fork(putDelayed, timeout, BREAK_TYPE);
while (true) {
const { action, canceled } = yield race({
action: take(types),
@krutoo
krutoo / parseMultiple.js
Last active January 26, 2022 19:31
date-fns parse multiple formats
import { default as parse } from 'date-fns/parse';
import { default as isValid } from 'date-fns/isValid';
// like parse but second argument must array of strings
// returns date
export const parseMultiple = (
dateString,
formatString,
referenceDate,
options