Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
marcogrcr / localstack-ddb-table.sh
Created November 7, 2024 13:35
Create a DynamoDB table using localstack
#!/bin/sh
set -eu
echo 'Initializing test DynamoDB table...'
# https://docs.localstack.cloud/getting-started/installation/#docker
IMAGE=$(docker run \
--rm -d \
-p 127.0.0.1:4566:4566 \
@marcogrcr
marcogrcr / string-builder.ts
Last active October 31, 2024 14:48
TypeScript StringBuilder
/**
* A memory efficient string concatenation builder.
* @example
* const builder = new StringBuilder();
* builder.append("Hello");
* builder.append(", world!");
*
* // if you need the string
* const str = builder.toString();
*
@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 October 25, 2024 01:01
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