Skip to content

Instantly share code, notes, and snippets.

View paulknulst's full-sized avatar
🎯
Focusing

Paul Knulst paulknulst

🎯
Focusing
View GitHub Profile
@paulknulst
paulknulst / 12-essential-js-functions.js
Created March 30, 2023 06:48
12 Essential JavaScript Functions Every Web Developer Must Know -
//1. Retrieve the First/Last Item In Any JavaScript Array
const head = (arr) => arr[0];
const last = (arr) => arr[arr.length - 1];
head([1, 2, 3, 4, 5, 6, 7, 8]); // 1
last([1, 2, 3, 4, 5, 6, 7, 8]); // 8
//2. Comma operator in JavaScript
console.log([1, 2, 3, 4][1]); // 2
@paulknulst
paulknulst / 12-important-js-functions-to-improve-code-as-a-web-developer.js
Created January 10, 2023 12:16
12 Important JavaScript Functions To Improve Code as a Web Developer on Medium: https://medium.com/@paulknulst
// 1. Valid Json
const isValidJSON = (str) => {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
};
// 1. Max Number
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
maxN([1, 2, 3, 4]); // [4]
maxN([2, 4, 6, 4, 3], 2); // [6, 4]
// 2. Ternary Operator
function temparature(temp) {
return temp > 39 || temp < 35.5
@paulknulst
paulknulst / 12-useful-js-code-snippets.js
Created December 24, 2021 00:18
12 useful JavaScript snippets to solve everyday problems, save time and optimize code as a web developer.
// 1. Destructive Assignment
const data = ["Paul", "too old", "Software Engineer"]
const [name, age, job_title] = data
console.log(name, age, job_title) // Paul too old Software Engineer
// 2. Find an object in Array
const employess = [
{name: "Paul", job_title: "Software Engineer"},
// 1. Shorten the console log
const log = console.log.bind(document)
log("does it work?")
log("yes")
log(5)
// 2. Merge two arrays into one
const array1 = ["One", "Two", "Three"]
const array2 = ["Four", "Five", "Six"]
@paulknulst
paulknulst / full-db-zipping-and-uploading-to-gcp.sh
Created November 3, 2021 09:33
zip/encrypt (with pass from env) db backups within folder and upload resulting file to gcp using stored credentials on system
#!/bin/bash
TIME=$(date +%Y-%m-%d)
HOST=$(hostname)
BACKUPSAVEPATH=/root/backups_encrypted/
BACKEDUPFILE=$BACKUPSAVEPATH/db-backups-$HOST-$TIME.enc
TARGET=gs://f1nalboss-db-backups/
cd /root/backups/db || exit 1
if [ -z "$(ls -A .)" ]; then
exit 1
@paulknulst
paulknulst / update.sh
Last active October 27, 2021 05:46
this update script is used to deploy several docker files.
#!/bin/bash
filepath=`pwd`
if [[ $filepath == *"test-suite/live-deploy" ]]; then
read -p "choose environment 'prod' or 'demo': " updateEnv
else
echo "it seems that you are in the wrong folder, pls got to test-suite/live-deploy"
exit 1
fi
@paulknulst
paulknulst / full-db-backup.sh
Last active November 3, 2021 09:29
this simple script searches all docker container and do a full db backup of all databases
#!/bin/bash
mysqlContainer=$(docker ps | grep 'mysql\|maria' | awk {'print $NF'}) #gets every container with a name containing mysql/mariadb
postgreContainer=$(docker ps | grep -Ei 'postgre'| grep -Eiv 'gitlab' | awk {'print $NF'}) #gets every container with a name containing postgre
gitlabPotsgreContainer=$(docker ps | grep -Ei 'postgre'| grep -Ei 'gitlab' | awk {'print $NF'}) #special gitlab because it use other env variables
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
for container in $mysqlContainer; do
containerStringParts=$(echo $container | tr "." "\n")
for single in $containerStringParts; do
simpleName=$single