Skip to content

Instantly share code, notes, and snippets.

View anechunaev's full-sized avatar

Artem Nechunaev anechunaev

View GitHub Profile
@anechunaev
anechunaev / webpack.config.js
Created August 29, 2023 17:36
Webpack configuration for a fast build
const path = require('path');
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
target: 'node14',
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
libraryTarget: 'commonjs2',
@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);
@anechunaev
anechunaev / index.js
Created September 9, 2020 08:30
Custom input-like event in React
const root = document.querySelector('#root');
const customEvent = new CustomEvent(
"myEvent",
{
bubbles: false,
cancelable: false,
detail: null,
},
);
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 / server.mjs
Last active June 20, 2018 09:24
Node.js simple static server
import { createServer } from 'http';
import { readFile } from 'fs';
import { extname } from 'path';
createServer(function (request, response) {
const filePath = request.url === '/' ? './static/index.html' : './static' + request.url;
const extension = extname(filePath);
let contentType = 'text/html';
switch (extension) {
@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'