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 / cleanup-branches.sh
Created February 27, 2026 21:57
Bulk delete GitHub branches with whitelist protection
#!/usr/bin/env bash
# =============================================================================
# cleanup-branches.sh — Bulk delete GitHub branches with whitelist protection
# =============================================================================
#
# Usage:
# ./cleanup-branches.sh --repo owner/repo --whitelist whitelist.txt [OPTIONS]
#
# Options:
# --repo OWNER/REPO Target repository (required)
@petergi
petergi / JavaScript - date - is weekday.js
Created February 25, 2026 19:19
Checks if the given date is a weekday.
Checks if the given date is a weekday.
- Use `Date.prototype.getDay()` to check weekday by using a modulo operator (`%`).
- Omit the argument, `d`, to use the current date as default.
const isWeekday = (d = new Date()) => d.getDay() % 6 !== 0;
isWeekday(); // true (if current date is 2019-07-19)
@petergi
petergi / JavaScript - date - is weekend.js
Created February 25, 2026 19:18
Checks if the given date is a weekend. - Use `Date.prototype.getDay()` to check weekend by using a modulo operator (`%`). - Omit the argument, `d`, to use the current date as default.
// Checks if the given date is a weekend.
// - Use `Date.prototype.getDay()` to check weekend by using a modulo operator (`%`).
// - Omit the argument, `d`, to use the current date as default.
const isWeekend = (d = new Date()) => d.getDay() % 6 === 0;
isWeekend(); // 2018-10-19 (if current date is 2018-10-18)
@petergi
petergi / JavaScript - filter matching and unspecified values.js
Created February 25, 2026 19:18
Filters an array of objects based on a condition while also filtering out unspecified keys. - Use `Array.prototype.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value. - On the filtered array, use `Array.prototype.map()` to return the new object. - Use `Array.pr…
// Filters an array of objects based on a condition while also filtering out unspecified keys.
//
// - Use `Array.prototype.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value.
// - On the filtered array, use `Array.prototype.map()` to return the new object.
// - Use `Array.prototype.reduce()` to filter out the keys which were not supplied as the `keys` argument.
const reducedFilter = (data, keys, fn) =>
data.filter(fn).map(el =>
keys.reduce((acc, key) => {
@petergi
petergi / JavaScript - filter unique array values.js
Created February 25, 2026 19:13
Creates an array with the unique values filtered out. - Use the `Set` constructor and the spread operator (`...`) to create an array of the unique values in `arr`. - Use `Array.prototype.filter()` to create an array containing only the non-unique values.
//
// Creates an array with the unique values filtered out.
//
// - Use the `Set` constructor and the spread operator (`...`) to create an array of the unique values in `arr`.
// - Use `Array.prototype.filter()` to create an array containing only the non-unique values.
const filterUnique = arr =>[...new Set(arr)].filter(i => arr.indexOf(i) !== arr.lastIndexOf(i));
filterUnique([1, 2, 2, 3, 4, 4, 5]); // [2, 4]
@petergi
petergi / JavaScript - find matching keys.js
Created February 25, 2026 19:12
Finds all the keys in the provided object that match the given value. - Use `Object.keys()` to get all the properties of the object. - Use `Array.prototype.filter()` to test each key-value pair and return all keys that are equal to the given value.
// Finds all the keys in the provided object that match the given value.
//
// - Use `Object.keys()` to get all the properties of the object.
// - Use `Array.prototype.filter()` to test each key-value pair and return all keys that are equal to the given value.
const findKeys = (obj, val) => Object.keys(obj).filter((key) => obj[key] === val);
const ages = {
Leo: 20,
Zoey: 21,
Jane: 20,
@petergi
petergi / JavaScript - Int to Bin.js
Created February 25, 2026 19:11
Function converts integer to binary string.
function intToBin(number) {
if (number === 0) {
return '0';
}
let res = '';
while (number > 0) {
res = String(number % 2) + res;
number = parseInt(number / 2, 10);
}
@petergi
petergi / JavaScript - is alpha numeric.js
Created February 25, 2026 19:10
Checks if a string contains only alphanumeric characters. - Use `RegExp.prototype.test()` to check if the input string matches against the alphanumeric regexp pattern.
// Checks if a string contains only alphanumeric characters.
//
// - Use `RegExp.prototype.test()` to check if the input string matches against the alphanumeric regexp pattern.
const isAlphaNumeric = str => /^[a-z0-9]+$/gi.test(str);
isAlphaNumeric('hello123'); // true
isAlphaNumeric('123'); // true
isAlphaNumeric('hello 123'); // false (space character is not alphanumeric)
@petergi
petergi / JavaScript - is anagram.js
Created February 25, 2026 19:10
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters). - Use `String.prototype.toLowerCase()` and `String.prototype.replace()` with an appropriate regular expression to remove unnecessary characters. - Use `String.prototype.split()`, `Array.prototype.sort()` and `Array.prototype…
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
- Use `String.prototype.toLowerCase()` and `String.prototype.replace()` with an appropriate regular expression to remove unnecessary characters.
- Use `String.prototype.split()`, `Array.prototype.sort()` and `Array.prototype.join()` on both strings to normalize them, then check if their normalized forms are equal.
const isAnagram = (str1, str2) => {
const normalize = str =>
str
.toLowerCase()
@petergi
petergi / JavaScript - is date valid.js
Created February 25, 2026 19:09
Checks if a valid date object can be created from the given values. - Use the spread operator (`...`) to pass the array of arguments to the `Date` constructor. - Use `Date.prototype.valueOf()` and `Number.isNaN()` to check if a valid `Date` object can be created from the given values.
Checks if a valid date object can be created from the given values.
- Use the spread operator (`...`) to pass the array of arguments to the `Date` constructor.
- Use `Date.prototype.valueOf()` and `Number.isNaN()` to check if a valid `Date` object can be created from the given values.
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
isDateValid('December 17, 1995 03:24:00'); // true
isDateValid('1995-12-17T03:24:00'); // true