Skip to content

Instantly share code, notes, and snippets.

View anechunaev's full-sized avatar

Artem Nechunaev anechunaev

  • 🇸🇪 Stockholm
  • 09:08 (UTC +02:00)
View GitHub Profile
@anechunaev
anechunaev / prng.ts
Last active January 22, 2025 14:57
Pseudo-random number generator for TypeScript
let storedSeed: number = 1337;
// MurmurHash3
export function seed(str: string): void {
for(let i = 0, h = 1779033703 ^ str.length; i < str.length; i++) {
h = Math.imul(h ^ str.charCodeAt(i), 3432918353),
h = h << 13 | h >>> 19;
if (i === str.length - 1) {
storedSeed = h;
@anechunaev
anechunaev / webpack.config.js
Last active January 29, 2025 15:43
Webpack configuration for a fast build of Node.js application. Does not bundle node_modules. Uses SWC to transform and minimize code.
const path = require("node:path");
const webpack = require("webpack");
const { SwcMinifyWebpackPlugin } = require('swc-minify-webpack-plugin');
module.exports = {
mode: "production",
target: "node22",
context: path.resolve(__dirname),
entry: {
server: path.resolve(__dirname, "../src/server.ts"),
@anechunaev
anechunaev / RequestQueue.ts
Created April 18, 2023 11:20
This class helps to work with queue of async tasks (e g network requests) with some restrictions on how many tasks could be running at the same time. By default it will store new tasks to internal queue and run no more than 5 tasks with 1 retry.
export type TFuture<T> = () => Promise<T>
export type TQueueOptions<T> = {
maxActiveRequests?: number
dataHandler?: (data: T) => void
errorHandler?: (error: Error) => void
retries?: number
doneCallback?: (error?: Error, data?: Array<Error | T | undefined>) => void
failOnError?: boolean
}
@anechunaev
anechunaev / index.js
Last active November 4, 2020 13:45
Custom require and resolve functions for Node.js (v.14)
// Run Node.js process with --require flag:
// $ node index.js -r register.js
// ...or with NODE_OPTIONS env variable:
// $ NODE_OPTIONS="-r register.js" node index.js
const style1 = require('./src/styles.less');
const style2 = require('./src/styles.less?raw');
console.log(style1, style2);
function linearToLogarithmic(linearValue: number, minValue: number, maxValue: number) {
const range = maxValue - minValue;
let value = Math.round(Math.pow(range + 1, linearValue) + minValue - 1);
if (value < minValue) {
value = minValue;
} else if (value > maxValue) {
value = maxValue;
}
@anechunaev
anechunaev / index.ts
Created September 25, 2018 19:13
Get element path in DOM (including correct shadow DOM path resolving).
/**
* Return string that represent element position in DOM (usually valid CSS selector). Position is relative to document.body.
*
* ATTENTION!
* If you use shadow DOM on the page, this function will not return valid CSS selector.
*/
export function getPath(element: Element) {
const stack = [];
let el = element;
while (!!el.parentNode) {
@anechunaev
anechunaev / date-parse.ts
Created August 28, 2018 13:23
Super simple function to parse date string (date.toLocaleDateString()). It's not bullet-proof, so for more stable parsing use something like moment.js
/**
* Super simple function to parse date string (date.toLocaleDateString())
* It's not bullet-proof, so for more stable parsing use something like moment.js
*
* This function may return invalid date object if the string was not parsed correctly.
* Check for validity:
*
* isNaN(date.valueOf()) // Invalid date if true
*/
private parseLocaleDateString = (localeDate?: string): Date => {
@anechunaev
anechunaev / .bash_profile
Last active May 10, 2023 18:18
.bash_profile
#################
# Custom prompt #
#################
txtrst='\e[0m'
fgW='\e[38;5;7m'
bgDG1='\e[48;5;235m'
fgDG1='\e[38;5;235m'
bgDG2='\e[48;5;237m'
fgDG2='\e[38;5;237m'
bgDG3='\e[48;5;239m'