Skip to content

Instantly share code, notes, and snippets.

View mrsarm's full-sized avatar
🏠
Working from home

Mariano Ruiz mrsarm

🏠
Working from home
View GitHub Profile
@mrsarm
mrsarm / isDefined.ts
Created February 13, 2025 14:17
isDefined: Return true only if the element is not null or undefined
/**
* Return true only if the element is not null or undefined,
* use it with `Array.filter` to not only filter nullable values
* but also to guard the type.
*/
export const isDefined = <T>(argument: T | undefined | null): argument is T => {
return argument !== undefined && argument !== null;
};
@mrsarm
mrsarm / re-samples.js
Created January 27, 2025 21:32
re-samples.js: JavaScript Regex examples
let personList = `First_Name: John, Last_Name: Doe
First_Name: Jane, Last_Name: Smith`;
// Adding a ? at the end of the named capturing group makes it optional, e.g. '... (?<lastname>\w+)?'
let regexpNames = /First_Name: (?<firstname>\w+), Last_Name: (?<lastname>\w+)/mg;
let match = regexpNames.exec(personList);
do {
console.log(`Hello ${match.groups.firstname} ${match.groups.lastname}`);
} while((match = regexpNames.exec(personList)) !== null);
@mrsarm
mrsarm / console-github-get-pr-commits-msgs.js
Last active November 29, 2024 13:40
Script to run in the developer console from a GitHub Pull Request page to get the list of all commits titles and descriptions
// Script to run in the developer console from a GitHub Pull Request page to get the list of all commits titles and descriptions
"\n- " + $x('//code/a[@class="Link--secondary markdown-title"]').map(e => {
let msg = e.title;
const pars = msg.split("\n\n");
if (pars.length > 1) {
msg = pars[0] + (pars[0].endsWith(":") || pars[0].endsWith(".") ? "" : ":"); // commit title
msg += "\n ";
msg += pars[1].split('\n').join('.\n '); // commit description
}
@mrsarm
mrsarm / github-file.sh
Last active April 19, 2024 22:25
github-file.sh: GitHub Action script to get a file from a repo/branch, otherwise fallback to another branch.
#!/usr/bin/env sh
# Get a file from a GitHub repository, trying first to download it
# from a branch passed by argument, if the file doesn't exist,
# fallback to another branch version.
#
# Script to be called from Github Actions as following:
#
# - name: Get GitHub downloader
# run: wget https://gist.github.com/mrsarm/95279381f3d8bf4269499fb437888e2c/raw/github-file.sh
@mrsarm
mrsarm / main.rs
Last active March 1, 2024 22:58
vector_threads
/// Multi-thread example in Rust of iterating a vector as input data in chunks,
/// using threads in a safe way, using the threads and the channels APIs, and
/// the `Arc` type.
///
/// ## Details
///
/// Spawn MAX_NTHREADS number of threads, then from each thread borrow a slice
/// of the vector data, and from each string inside
/// the slice, calculate a new string and publish it into a channel, that
/// can then be listened by a receiver from the main thread, while the
@mrsarm
mrsarm / promise-concurrency-limit.ts
Created June 16, 2023 01:46
promise-concurrency-limit.ts: Execute concurrently the asynchronous functions passed, with a limit in concurrency
/**
* Execute concurrently the asynchronous functions `fns` passed, but
* not more than `maxConcurrency` at the same time. All functions
* return the same type T, an a promise with T[] inside is returned.
*/
const execConcurrent = async<T> (
fns: (() => Promise<T>)[],
maxConcurrency: number,
): Promise<T[]> => {
const res: T[] = [];
@mrsarm
mrsarm / drop-db-connections-opened.sql
Last active June 15, 2023 15:26
drop-db-connections-opened.sql: Delete Postgres Database with connections opened
-- Delete Database with connections opened
-- Postgres older than 13 don't support FORCE option, in this case pg_terminate_backend() is used:
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'dbname';
DROP DATABASE dbname;
@mrsarm
mrsarm / video2mp4.sh
Created March 12, 2023 14:08
Convert what ever video file to a compatible MP4 file that works on most devices.
#!/usr/bin/env bash
if [ "$1" == "-h" -o "$1" == "--help" -o "$#" == "0" ]
then
echo "Convert what ever video file to a compatible MP4 file"
echo "that works on most devices."
echo
echo "Usage: video2mp4.sh [FILE]"
exit
fi

Install Python from sources

On Ubuntu or compatible:

sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev libffi-dev lzma liblzma-dev

Download Python source and install as normal user with (replace prefix path, e.g. /opt/py/python3.11):

@mrsarm
mrsarm / utils-db-postgresql.sql
Created November 22, 2022 15:38
Postgres DB utils SQL scripts
-- DB size in bytes
SELECT pg_size_pretty( pg_database_size('dbname') );
-- Table size in bytes
SELECT pg_size_pretty( pg_total_relation_size('tablename') );
-- All table sizes sorted by size desc
SELECT table_name, pg_size_pretty( pg_relation_size(quote_ident(table_name)) )
FROM information_schema.tables