Skip to content

Instantly share code, notes, and snippets.

View alex-alekseichuk's full-sized avatar
🏠
Working from home

Alex Alekseichuk alex-alekseichuk

🏠
Working from home
View GitHub Profile
@alex-alekseichuk
alex-alekseichuk / enum.js
Last active October 9, 2021 17:15
Enum in JavaScript
function createEnum(labels, startIndex = 0) {
return Object.freeze(labels.reduce((enu, label, i) => {enu[label] = i + startIndex; return enu;}, {}));
}
const ERRORS = util.createEnum(['OK', 'UNKNOWN_ERROR', 'SERVER_ERROR']);
console.log(ERRORS.OK);
@alex-alekseichuk
alex-alekseichuk / console.logging.js
Last active October 9, 2021 17:14
A bit better console.log() calls
const var1 = "string1";
const var2 = 7;
console.log({var1, var2}); // is much better than
console.log(var1, var2);
// because it outputs both the name of variables and their values.
@alex-alekseichuk
alex-alekseichuk / axios-upload-unlimited.js
Last active October 9, 2021 17:14
Avoid default limitation for upload content length in axios lib.
// There is a limit for uploading content in axios library by default.
// To avoid such a "feature", and be able to upload large files add to options:
options.maxBodyLength = Infinity;
options.maxContentLength = Infinity;
@alex-alekseichuk
alex-alekseichuk / key-press-exit.js
Last active October 9, 2021 17:13
Non-blocking node.js exit on key press
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
@alex-alekseichuk
alex-alekseichuk / range-const.go
Last active October 9, 2021 17:12
Custom range of const in Golang
package main
import "fmt"
func main() {
const (
C8 = iota * 2 + 8
C10
C12
C14
@alex-alekseichuk
alex-alekseichuk / man-titles.sh
Last active October 9, 2021 17:26
Generate brief descriptions for the list of tools from their man pages
#!/bin/bash
cmds=(cat tac head tail tee more less echo wc grep find sort comm tr cut split sed awk seq)
for cmd in $cmds; do
man $cmd|sed -n '4p'|sed -E 's/^\s+//;s/ - /\n /'
done
@alex-alekseichuk
alex-alekseichuk / zebra-puzzle.go
Last active October 17, 2021 20:38
Brute-force solution of the Zebra Puzzle https://code.energy/solving-zebra-puzzle/
// Brute-force solution of the Zebra Puzzle
// https://code.energy/solving-zebra-puzzle/
//
// Result:
// Norwegian from 1 house drinks water.
// Japanese from 5 house owns zebra.
package main
import (
@alex-alekseichuk
alex-alekseichuk / isBalanced.js
Created February 16, 2022 16:40
Check if the brackets string is balanced
// Test task: check if brackets string balanced
const bracketsMatch = {
'{': '}',
'[': ']',
'(': ')',
}
function isBalanced(brackets) {
const stack = []
@alex-alekseichuk
alex-alekseichuk / fizz-buzz.js
Last active August 1, 2022 12:41
FizzBuzz problem implementation. CLI tool, JavaScript, generator.
#!/usr/bin/env node
function createFizzBuzzGenerator() {
const values = Array(16).fill(null).map((_, i) => {
const x = i + 1
if (x % 3 !== 0 && x % 5 !== 0)
return x
let result = ''
if (x % 3 === 0)
result += 'Fizz'
@alex-alekseichuk
alex-alekseichuk / syntax-sugar.js
Created September 1, 2022 14:34
JavaScript Examples: syntax sugar
// Nullish coalescing
// Нулевое слияние
// if data is undefined
const getUserName = (user) => {
return user?.name ?? "Anonymous";
};
// will display `fallbackData`
// if data is false-ish (e.g., null, undefined, '', 0, etc.)
console.log(data || fallbackData);