Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
marcogrcr / recursive-for-each-file.sh
Created July 19, 2024 15:31
A POSIX-compatible recursive iteration over each file
#!/bin/sh
folder_path='...'
file_types='*.json'
# See https://www.shellcheck.net/wiki/SC2044 for why we loop this way
find "$folder_path" ! -name "$(printf "*\n*")" -name "$file_types" | while IFS= read -r file
do
echo "Processing $file..."
# process $file
@marcogrcr
marcogrcr / diff-between-two-dates.js
Last active June 26, 2024 13:21
Gets the difference between two dates and a timeline of a series of dates
export function diffBetweenTwoDates(start, end) {
const startTime = new Date(start).valueOf();
const endTime = new Date(end).valueOf();
if (Number.isNaN(startTime)) throw new Error("Invalid start date");
if (Number.isNaN(endTime)) throw new Error("Invalid end date");
const fmt = (value, len = 2) => value.toString().padStart(len, "0");
const diff = new Date(endTime - startTime);
const diffNoTime = `${fmt(diff.getUTCFullYear(), 4)}-${fmt(diff.getUTCMonth() + 1)}-${fmt(diff.getUTCDate())}T00:00:00Z`;
@marcogrcr
marcogrcr / print-element-attributes.js
Created May 28, 2024 18:25
Print element attributes
((selector) => Array
.from(document.querySelectorAll(selector))
.map(x => Array
.from(Array(x.attributes.length).keys())
.reduce((o, i) => ({ [x.attributes.item(i).name]: x.attributes.item(i).value, ...o }), {})
)
)('meta')
@marcogrcr
marcogrcr / echo-server.js
Created May 22, 2024 20:41
A simple node.js HTTP/1.1 server that echoes back a request as a JSON object
import { createServer } from "http";
const server = createServer((req, res) => {
const { method, url: path, headersDistinct: headers } = req;
let data = [];
req.on("data", (chunk) => data.push(chunk));
req.on("end", (chunk) => {
data.push(chunk);
data = data.join("");
@marcogrcr
marcogrcr / transform-contentful-type-files.md
Last active May 2, 2024 00:14
Transforms files generated by contentful-typescript-codegen

This script transforms files generated by [contentful-typescript-codegen] to ensure the sys property is in sync with the [contentful] package.

For example, the following auto-generated file:

// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Asset, Entry } from 'contentful';
import { Document } from '@contentful/rich-text-types';
@marcogrcr
marcogrcr / AwsSdkV2AndVirtualThreads.java
Last active March 21, 2024 17:35
Java AWS SDK v2 and virtual threads
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@marcogrcr
marcogrcr / extract-jar-files.sh
Created March 13, 2024 00:26
Extract all nested jar files
#!/bin/sh
find . -name '*.jar' | while read -r jar_path; do
jar=$(basename "$jar_path")
folder=$(dirname "$jar_path")
cd "$folder" || exit 1
unzip -o "$jar"
cd - || exit 1
done
echo 'Done!'
@marcogrcr
marcogrcr / recommended-flags.sh
Created March 11, 2024 19:41
Recommended flags for shell scripts
@marcogrcr
marcogrcr / parse-args.sh
Created March 11, 2024 19:23
POSIX compliant argument parsing example
#!/bin/sh
# Example to parse arguments in a POSIX shell file
# Inspired by: https://stackoverflow.com/questions/2875424/correct-way-to-check-for-a-command-line-flag-in-bash
# error codes
invalid_args=-1
# arg defaults
value=""
@marcogrcr
marcogrcr / rtx-in-ubuntu.md
Created December 2, 2023 09:15
rtx-in-ubuntu.md

When using [rtx] in Ubuntu 22.04, take the following into account:

dotnet

Installation works out of the box via the [asdf-dotnet] plugin.

rtx install dotnet@8.0.100