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 / 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));
};
// Finds all unique values of an array, based on a provided comparator function, starting from the right.
// - Use `Array.prototype.reduceRight()` and `Array.prototype.some()` to create an array containing only the last unique occurrence of each value, based on the comparator function, `fn`.
// - The comparator function takes two arguments: the values of the two elements being compared.
const uniqueElementsByRight = (arr, fn) =>
arr.reduceRight((acc, v) => {
// Checks if all the elements in `values` are included in `arr`.
// - Use `Array.prototype.every()` and `Array.prototype.includes()` to check if all elements of `values` are included in `arr`.
const includesAll = (arr, values) => values.every(v => arr.includes(v));
// includesAll([1, 2, 3, 4], [1, 4]); // true
// includesAll([1, 2, 3, 4], [1, 5]); // false
// Interval: [start, end].
// Merges two overlapping intervals into one.
function intervalsMerge(a, b) {
return [Math.min(a[0], b[0]), Math.max(a[1], b[1])];
}
const deepEqual = require('./deepEqual');
console.log(deepEqual(intervalsMerge([1, 2], [1, 4]), [1, 4]));
console.log(deepEqual(intervalsMerge([1, 2], [0, 4]), [0, 4]));