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 / select-optgroup.html
Created May 19, 2023 09:09
HTML SELECT: Split whole options set into sections
<select id="selectOptgroup" defaultValue="">
<option disabled="disabled" value="">Select option...</option>
<optgroup label="First section">
<option value="1.1">1.1</option>
<option value="1.2">1.2</option>
</optgroup>
<optgroup label="Second section">
<option value="2.1">2.1</option>
<option value="2.2">2.2</option>
</optgroup>
@alex-alekseichuk
alex-alekseichuk / vic
Last active December 2, 2023 14:27
Edit encrypted by password text file
#!/usr/bin/bash
echo -n 'Password: '
read -s password
plain=$(mktemp)
openssl enc -aes-256-cbc -pbkdf2 -iter 100000 -pass pass:$password -d <$1 >$plain 2>/dev/null
prev=$(mktemp)
cp $plain $prev
vi $plain
if ! (cmp -s $prev $plain); then
openssl enc -aes-256-cbc -pbkdf2 -iter 100000 -pass pass:$password <$plain >$1 2>/dev/null
@alex-alekseichuk
alex-alekseichuk / mysql.js
Last active April 19, 2023 18:40
node.js mysql client
/**
npm i --save mysql
const conn = await pool.getAsyncConnection();
await conn.beginTransaction();
try {
await conn.query(sql1);
await conn.query(sql2);
await conn.commit();
@alex-alekseichuk
alex-alekseichuk / sha256.js
Created April 15, 2023 12:15
Pure JavaScript SHA256
/*
SHA256 pure JavaScript implementation.
const hexContent = '278f0c9f473ddca628240ac41bfab9fec90612a929ad2d7b';
const aContent = hexToBytes(hexContent);
const hash = createHash('sha256');
hash.update(aContent);
const aHash = hash.digest();
@alex-alekseichuk
alex-alekseichuk / isTokenExpired.js
Created April 13, 2023 12:37
Check JWT token is expired. Pure JavaScript.
const isTokenExpired = token => Date.now() >= (JSON.parse(atob(token.split('.')[1]))).exp * 1000;
@alex-alekseichuk
alex-alekseichuk / mem-limit-test.js
Created March 20, 2023 17:36
Check node.js RAM usage limit
// node ./mem-limit-test.js
// node --max-old-space-size=4096 ./mem-limit-test.js
// NODE_OPTIONS=--max-old-space-size=4096 node ./mem-limit-test.js
const array = [];
while (true) {
// increase array
array.push(new Array(10000000));
const memory = process.memoryUsage();
console.log((memory.heapUsed / 1024 / 1024 / 1024).toFixed(4), 'GB');
@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);
@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 / 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 / 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 (