Skip to content

Instantly share code, notes, and snippets.

View Git-I985's full-sized avatar
Focusing

Edward Konovalov Git-I985

Focusing
View GitHub Profile
@Git-I985
Git-I985 / railFenceCipher.js
Created November 20, 2023 11:21
Rail fence cipher
function getSteps(currentRail, numberRails) {
let step1 = (currentRail === numberRails) || (currentRail === 1) ? ((numberRails - 1) * 2) : ((numberRails - currentRail) * 2);
let step2 = (currentRail === numberRails) || (currentRail === 1) ? (step1) : ((currentRail - 1) * 2);
return [step1, step2]
}
function encodeRailFenceCipher(string, numberRails) {
let res = ''
for (let currentRail = 1; currentRail <= numberRails; currentRail++) {
function maxSumOfTwoRangesIn(arr) {
const ranges = [[]];
for (let i = 0; i < arr.length; i++) {
const last = ranges.length - 1;
for (let j = 0; j <= last; j++) {
// ranges.push(ranges[j].concat(i))
ranges.push([...ranges[j], i]);
}
}
#!/bin/bash
if [[ -z $TNF_API_KEY ]]
then
echo 'Error: TNF_API_KEY variable not found, please get api key from https://tinypng.com/developers and paste it in your ~/.zshrc or ~/.bashrc'
exit 1
fi
for file in $(git status -s | grep -E '^[^D]{2}' | cut -c4- | grep -E "(.png|.jpg|.webp|.jpeg)")
do
# step 1. find all uncommitted/changed images in sources dir and copy them to the buffer dir
for file in $(git status -s | cut -c4- | grep .png)
do
echo $file;
mkdir -p "./to_optimize/$(dirname $file)";
cp $file "./to_optimize/$file";
done
# step 2. optimize files in buffer dir by some third party service
in="./e1"
out="./export"
rm -rf ${out}
mkdir -p ${out}
for file in ${in}/*; do
dirname=$(basename $file '.svg' | tr '+' '_');
dest=$out/$dirname/default.svg
echo $dest
@Git-I985
Git-I985 / bubble-sort.js
Last active July 16, 2023 16:33
bubble-sort-js
const stdout = {
template: (val) => `\x1b[${val}m`,
reset: () => stdout.template(0),
cyanBright: (value) => stdout.template(96) + value + stdout.template(39) + stdout.reset(),
yellowBright: (value) => stdout.template(93) + value + stdout.template(39) + stdout.reset(),
redBright: (value) => stdout.template(91) + value + stdout.template(39) + stdout.reset(),
greenBright: (value) => stdout.template(92) + value + stdout.template(39) + stdout.reset(),
gray: (value) => stdout.template(30) + value + stdout.template(89) + stdout.reset(),
bold: (value) => stdout.template(1) + value + stdout.template(22) + stdout.reset(),
dim: (value) => stdout.template(2) + value + stdout.template(22) + stdout.reset(),
const getQueryParams = (str) => Object.fromEntries(Array.from(str.matchAll(/(?:([^?=&]+)=([^=&]+))/gm)).map(([_, param, value]) => [decodeURIComponent(param), decodeURIComponent(value)]))
@Git-I985
Git-I985 / phonebook-binary-search.js
Created July 7, 2023 19:16
phonebook binary search js
// binary search by key
const binarySearch = (phonebook, searchName) => {
if(phonebook.length === 0) {
return 'Phonebook is empty!'
}
let left = 0
let right = phonebook.length - 1
while(left <= right) {
@Git-I985
Git-I985 / compare-file-sizes.js
Last active May 20, 2023 21:48
compare-file-sizes-node
const getFileSize = async (file) => (await fs.stat(file)).size
const compareFileSizes = async (filepath1, filepath2, cb) => {
try {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency
const [fsize1, fsize2] = await Promise.all([
getFileSize(filepath1),
getFileSize(filepath2)
])
cb(null, Math.sign(fsize1 - fsize2))
@Git-I985
Git-I985 / finite-state-machine.js
Created May 20, 2023 15:01
finite state machine
const States = {
NEWLINE: 'NEWLINE',
WRITING: 'WRITING',
LINE_COMPLETE: 'LINE_COMPLETE',
}
const NEWLINE_CHAR = '\n'
const SPACE_CHAR = ' '
const lexer = (str) => {