Skip to content

Instantly share code, notes, and snippets.

View nikolaskhodov's full-sized avatar
🎯
Focusing

Nikolas Khodov nikolaskhodov

🎯
Focusing
  • Prague, Czech Republic
View GitHub Profile
@nikolaskhodov
nikolaskhodov / instrumentPreCallHook.js
Last active October 16, 2025 23:09
Instrument a pre-call function hook in JS
export function instrumentPreObjectFunctionCallHook(obj, methodName, hookFn) {
const originalMethod = obj[methodName];
obj[methodName] = (...args) => {
const result = hookFn({ args, obj, methodName });
return result instanceof Promise
? result.then(() => originalMethod.call(obj, args))
: originalMethod.call(obj, args);
};
return obj[methodName].__deinstrument = () => {
@nikolaskhodov
nikolaskhodov / useTraceChangedHookDependencies.ts
Created May 23, 2025 20:54
Track changed hook dependencies in React
import { DependencyList, useRef } from 'react';
export function useTrackChangedDependencies(
dependencies: Record<string, any>,
location = 'unknown location'
): DependencyList {
const prevDependencies = useRef<Record<string, any> | undefined>();
if (prevDependencies.current !== undefined) {
const changedDependencies = Array.from(Object.entries(dependencies))
.map<[string, any, any] | undefined>(([key, dependency]) =>
(() => {
const DAY = 3600 * 1000 * 24;
const today = new Date();
const monday = today.getTime() + (1 /* Monday */ - today.getDay()) * DAY;
const weekdays = [
"sun",
"mon",
"tue",
"wed",
@nikolaskhodov
nikolaskhodov / .eslintrc.js
Last active November 6, 2023 12:53
Print AST ESLint Rule
module.exports = {
root: true,
parser: '@typescript-eslint/parser'
plugins: [
'nk'
],
extends: [ ],
settings: { },
rules: {
'nk/print-ast': 'error'
@nikolaskhodov
nikolaskhodov / transform.ts
Created October 18, 2023 16:44
Transform generic objects with type guargs only
import { keys } from 'ts-transformer-keys';
export type ConvertibleToNumber<T> = {
[K in keyof T]: number | string;
};
function isNumeric(value: string): boolean {
return isFinite(parseFloat(value));
}
export function record(type: string, object: any) {
const recordsByType: Record<string, any[]> = global.___recorded || {}
if (!global.___recorded) {
global.___recorded = recordsByType
}
if (!recordsByType[type]) {
recordsByType[type] = []
}
recordsByType[type].push(object)
@nikolaskhodov
nikolaskhodov / runInWebWorker.ts
Created June 11, 2022 19:27
Run an async function in web workers
import { v4 } from 'uuid';
interface IProgressMessage<T = any> {
id: string;
type: 'progress';
progress: T;
}
interface IDoneMessage<T = any> {
id: string;
@nikolaskhodov
nikolaskhodov / encrypt.sh
Last active June 25, 2025 16:05
encrypt.sh
#!/bin/bash
# brew install s3cmd@2.3.0
USERNAME=nikolas
set -e
CreateArchive () {
# generate encryption key
KEY=$(openssl rand -base64 32)
sudo -u ${USERNAME} /bin/bash <<EOT
set -e
@nikolaskhodov
nikolaskhodov / logAllDOMEvents.js
Last active December 8, 2020 13:13
Log all DOM events
logAllEvents(
el,
options = {
excludeEvents: ['mousemove', 'mouseover', 'mouseout'],
onlyEvents: [],
capture: false,
id: ''
}
) {
const watchedEventTypes = [
(function() {
const serializedLS = JSON.stringify(localStorage);
const template = `Object.entries(${serializedLS}).forEach((key, value) => localStorage.setItem(key, value))`;
copy(template);
console.log(`${template.length} bytes copies to the clipboard!`);
})();