Skip to content

Instantly share code, notes, and snippets.

View gabssnake's full-sized avatar

gabssnake gabssnake

  • France
View GitHub Profile
@gabssnake
gabssnake / Taskfile.sh
Created June 2, 2024 19:10
Nifty unobstrusive log and requirements functions for tiny Taskfile scripts
#!/bin/bash
function main {
log 'you said:' "$1"
}
# Use stderr to avoid polluting stdout
function log {
printf "%s\n" "$*" >&2;
}
@gabssnake
gabssnake / safe-fetch.mjs
Last active June 2, 2024 10:26
Wrapper on `fetch` to get happy path or just `null` and stderr logs, instead of throwing errors
import assert from "node:assert/strict";
const { error: debug } = console;
/**
* Silly fetch wrapper for lazyness and convenience to get happy path JSON
* or just `null` and stderr logs, instead of fiddling with res or throwing.
* $ node --env-file=.env script.mjs
* $ TOKEN=xxx node script.mjs
*/
export async function safeFetch(url, { TOKEN } = process.env) {
@gabssnake
gabssnake / line-stream.mjs
Last active June 2, 2024 10:15
Read lines from filename passed as argument or from stdin in Node.js
import fs from "node:fs/promises";
import readline from "node:readline/promises";
const { error: debug } = console;
/**
* Use as async iterator: `for await (const line of lineStream())`
* Works for calling with filename argument or piping stdin
* $ node script.mjs filename.txt
* $ node script.mjs < cat filename.txt
* $ cat filename.txt | node script.mjs
@gabssnake
gabssnake / ipc.example.js
Created October 19, 2021 13:59 — forked from Xaekai/ipc.example.js
Example of Interprocess communication in Node.js through a UNIX domain socket
/*
**
** Example of Interprocess communication in Node.js through a UNIX domain socket
**
** Usage:
** server> MODE=server node ipc.example.js
** client> MODE=client node ipc.example.js
**
*/
@gabssnake
gabssnake / README.md
Created December 22, 2020 09:11 — forked from developius/README.md
Setup SSH keys for use with GitHub/GitLab/BitBucket etc
@gabssnake
gabssnake / Good taste.md
Created December 15, 2020 10:22 — forked from santisbon/Good taste.md
What makes good taste?

On Good Taste

Linus Torvalds in an interview talked about the idea of good taste in code or what I like to call elegance. As one might expect from two slides meant to make a point during a talk, he omits a lot of details to keep it short and simple. This post digs into the specifics of his example (deleting an element from a list) and adds another example (inserting an element in a list) including working code.

Example from Linus

This is an example of removing an element from a singly-linked list. It's one of the first data structures you learn about when you start learning about computer science and programming. The reason it doesn't show particularly good taste is because we have that condition at the end where we take a different action depending on whether the element we want to remove is at the beginning of the list or somewhere in the middle.

![Bad taste](http://

@gabssnake
gabssnake / naur.md
Created October 24, 2020 19:16 — forked from dpritchett/naur.md
Programming as Theory Building

Programming as Theory Building

Peter Naur, 1985

(copied from http://alistair.cockburn.us/ASD+book+extract%3A+%22Naur,+Ehn,+Musashi%22)

Introduction

The present discussion is a contribution to the understanding of what programming is. It suggests that programming properly should be regarded as an activity by which the programmers form or achieve a certain kind of insight, a theory, of the matters at hand. This suggestion is in contrast to what appears to be a more common notion, that programming should be regarded as a production of a program and certain other texts.

@gabssnake
gabssnake / hooks.js
Created April 29, 2020 12:47
Using callbacks as iterators à la React Hooks
// Not my credit, this comes from @jedschmidt on Twitter
// https://twitter.com/jedschmidt
// Example usage:
//
// void async function() {
// let [clicks, onclick] = iterator()
// document.querySelector('button').addEventListener('click', onclick)
// for await (let click of clicks) console.log(click)
// }()
@gabssnake
gabssnake / tracker_blocking.rb
Created April 24, 2020 23:10 — forked from dhh/tracker_blocking.rb
Current list of spy pixels named'n'shamed in HEY, as of April 23, 2020
module Entry::TrackerBlocking
extend ActiveSupport::Concern
included do
has_many :blocked_trackers
end
email_service_blockers = {
"ActiveCampaign" => /lt\.php(.*)?l\=open/,
"AWeber" => "openrate.aweber.com",
@gabssnake
gabssnake / .colors
Created December 15, 2019 00:01 — forked from daytonn/.colors
Bash Color functions
# Colors
end="\033[0m"
black="\033[0;30m"
blackb="\033[1;30m"
white="\033[0;37m"
whiteb="\033[1;37m"
red="\033[0;31m"
redb="\033[1;31m"
green="\033[0;32m"
greenb="\033[1;32m"