Skip to content

Instantly share code, notes, and snippets.

View petergi's full-sized avatar
💭
Just Busy Living On The Side Of A Square

Peter Giannopoulos petergi

💭
Just Busy Living On The Side Of A Square
View GitHub Profile
@petergi
petergi / Covert to PNG.sh
Created May 22, 2026 02:11
Convert JPG, JPEG, WEBP, and AVIF images to PNG.
#!/usr/bin/env bash
# =========================================================
# to-png
#
# Convert JPG, JPEG, WEBP, and AVIF images to PNG.
#
# Requirements:
# brew install imagemagick
# brew install webp
python3 -m json.tool /path/to/file/my_json_file.json > /dev/null && echo OK
pwd
fnm current
ls .nvmrc .node-version 2>/dev/null
# walk upward looking for a version file:
d=$PWD; while [ "$d" != "/" ]; do ls "$d"/.nvmrc "$d"/.node-version 2>/dev/null; d=$(dirname "$d"); done
@petergi
petergi / Reverse a file in Neovim.txt
Created April 8, 2026 15:53
Reverse entire file. The first pass of :g marks every line matching {pattern}, while the second pass (again starting at the file's beginning and proceeding to the end) performs the [cmd]. Note: if :g processed lines in any order other than from top to bottom, this command would not work.
:g/^/m0
local function system(command)
local file = assert(io.popen(command, "r"))
local output = file:read("*all"):gsub("%s+", "")
file:close()
return output
end
@petergi
petergi / Regex to Match U.S. ZIP and Canadian Postal Codes.sh
Created April 8, 2026 15:51
Matches US ZIP, ZIP + 4, and Canadian Postal Codes
/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/
// Checks if a date is before another date.
// - Use the less than operator (`<`) to check if the first date comes before the second one.
const isBeforeDate = (dateA, dateB) => dateA < dateB;
isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true
// Finds the index of a given element in a sorted array using the binary search algorithm.
//
// - Declare the left and right search boundaries, `l` and `r`, initialized to `0` and the `length` of the array respectively.
// - Use a `while` loop to repeatedly narrow down the search subarray, using `Math.floor()` to cut it in half.
// - Return the index of the element if found, otherwise return `-1`.
// - **Note:** Does not account for duplicate values in the array.
const binarySearch = (arr, item) => {
// Checks if a date is after another date.
// Use the greater than operator (`>`) to check if the first date comes after the second one.
const isAfterDate = (dateA, dateB) => dateA > dateB;
isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true
// Returns the elements that exist in both arrays, filtering duplicate values.
// Create a `Set` from `b`, then use `Array.prototype.filter()` on `a` to only keep values contained in `b`.
const intersection = (a, b) => {
const s = new Set(b);
return [...new Set(a)].filter(x => s.has(x));
};