Skip to content

Instantly share code, notes, and snippets.

View AlexKratky's full-sized avatar
🌴
Working from home

Alex Krátký AlexKratky

🌴
Working from home
View GitHub Profile
@AlexKratky
AlexKratky / fomatNumberInString.js
Last active February 21, 2021 09:36
Format number with spaces in text using regex
let str = 'This string contains two numbers. One is 50000 and the second one is 10000.';
// Edit (\s|\.) if the character after the number is not whitespace or dot
// Edit '$& ' if you want to insert different character from whitespace, for example to format number using ',' replace whitespace with '$&,'
console.log(str.replace(/\d(?=(\d{3})+(\s|\.))/g, '$& '));
// or
console.log(str.replace(/\B(?=(\d{3})+(?!\d))/g, " "));
@AlexKratky
AlexKratky / backup_server.sh
Created February 7, 2021 11:56
backup whole server to .tar.gz file without .node_modules/ and vendor/ directory
cd /
# backup.tar.gz is the file name and we exclude that file from backuping (recursion)
# the ending slash is the directory you going to backup ( '/' is whole filesystem)
tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude="node_modules" --exclude="vendor" --exclude=/swapfile --one-file-system /
# more info at https://help.ubuntu.com/community/BackupYourSystem/TAR
@AlexKratky
AlexKratky / deploy_script.sh
Last active April 7, 2022 03:25
Laravel pipeline - composer, code style, php insights, phpstan, database migration, phpunit tests, integration test using Newman (Postman) + PHP built-in server, simple deploy to stage
#!/bin/bash
set -e
echo "✨ Deploying application..."
echo "👉 Fetching git"
git fetch --all
echo "👉 Pulling git"
@AlexKratky
AlexKratky / sort.js
Created May 16, 2022 13:52
Sort array of object for user (user related data first)
const userId = 28;
const res = [
{name: 'A', id: 1},
{name: 'A1', id: 1},
{name: 'A2', id: 1},
{name: 'A3', id: 1},
{name: 'B', id: 2},
{name: 'B1', id: 2},
{name: 'B2', id: 2},
{name: 'C', id: null},
@AlexKratky
AlexKratky / README.md
Created June 5, 2022 12:39
Get peak (maximum usage) of items at one moment depending on start and end date

This solution is for finding the peak (or you can call it maximum usage at one moment) of items depending on date ranges (which can be for example reservations).

Usage is as I mentioned for instance in the reservations system. Example case: You have a reservation system with items and multiple warehouses, in the first location, you have 25 items and you want to move part of them to another location, however, there are multiple upcoming reservations in the first location, so you just need to check, if in the first location will be enough items for that reservation after you transport a certain amount of items (which will lower the amount in the first location). The first solution that comes to mind is to sum all amounts of upcoming reservations, but that will only work if all reservations overlap each other, otherwise, the sum can be even higher than the total sum of all items. Using this solution you will find max usage at one moment.

Princip is very simple, every peak is at the start of some reservation, s