Skip to content

Instantly share code, notes, and snippets.

View pyrsmk's full-sized avatar
🤖
boop. boop. boop beep boop.

Aurélien Delogu pyrsmk

🤖
boop. boop. boop beep boop.
View GitHub Profile
@pyrsmk
pyrsmk / limiter.ts
Created July 8, 2022 09:41
A simple rate limiter that waits for N milliseconds before running the next callback.
/*
How to use:
import limiter from './limiter'
const limit = limiter(<wait_time_in_ms>)
limit(() => console.log('foo'))
limit(async () => console.log('bar'))
*/
export default (interval : number) => {
if (interval <= 0) {
@pyrsmk
pyrsmk / busy.ts
Created July 1, 2022 14:06
A simple function that marks a callback as busy to avoid calling it multiple times before it has finished its task.
const busyCallbacks = new Set()
export async function busy(callback : () => any) : Promise<any> {
if (busyCallbacks.has(callback)) {
return
}
busyCallbacks.add(callback)
const value = await callback()
busyCallbacks.delete(callback)
return value
@pyrsmk
pyrsmk / recursive-readdir-sync.ts
Last active July 8, 2022 08:43
Read a directory recursively
import fs from 'fs'
import path from 'path'
const recursiveReaddirSync = (currentPath : string) => {
let results : Array<string> = []
fs.readdirSync(currentPath).forEach(filename => {
const filePath = path.resolve(currentPath, filename)
const stat = fs.statSync(filePath)
if (stat.isDirectory()) {
@pyrsmk
pyrsmk / xml2obj.js
Created January 19, 2022 17:38
Convert XML files to JS objects.
// It converts simple XML structures to JS objects. The default value of a node is an
// empty string, as it should be (contrary to several libraries I tested).
const { DOMParser } = require('@xmldom/xmldom')
const loadChildNodesRecursive = node => {
const nodes = {}
Array.from(node.childNodes)
.filter(element => element.nodeType === element.ELEMENT_NODE)
.forEach(element => {
@pyrsmk
pyrsmk / async_queue.ts
Last active July 8, 2022 08:53
A queue that handles async functions in order with a wait time between each callback.
/*
Usage:
import async_queue from 'async_queue'
const queue = async_queue(<wait_time_in_ms>)
queue(async () => await someSlowAction1())
queue(async () => await someSlowAction2())
*/
export default (waitTime : number) => {
const queue : Array<() => Promise<void>> = []
return (callback : () => Promise<void>) => {
// Currently, the ESLint plugin used by Svelte is buggy and choke on files with Sass.
// This little implementation takes care of removing styling from Svelte components as
// as well as handle any type of files other than Svelte, with complete respect of rules
// from the base ESLint plugin (that is, `.eslintrc.cjs` rules are used). It has been
// thought to be simple to use and integrate.
//
// https://github.com/sveltejs/eslint-plugin-svelte3/issues/10
//
// Usage example:
//
@pyrsmk
pyrsmk / event_testing.html
Last active March 15, 2022 10:33
Because sometimes we don't understand what's going on with DOM events (even more on mobiles), here's an HTML page to test and understand things 🦄
<!doctype html>
<html>
<head>
<title>Event testing.</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" integrity="sha512-NhSC1YmyruXifcj/KFRWoC561YpHpc5Jtzgvbuzx5VozKpWvQ+4nXhPdFgmx8xqexRcpAglTj9sIBWINXa8x5w==" crossorigin="anonymous" />
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=" crossorigin="anonymous"></script>
<style>
* {
@pyrsmk
pyrsmk / Makefile
Created November 26, 2020 16:30
Current directory example in Makefile
current_dir := $(dir $(abspath $(firstword $(MAKEFILE_LIST))))
.PHONY=install
install:
ln -sf $(current_dir)config ~/.test/config
@pyrsmk
pyrsmk / fancy_loader.rb
Created July 2, 2020 08:36
A fancy dots loader
def fancy_loader(current, total)
glyph = "⠹⠼⠶⠧⠏⠛"[(Time.now.to_f * 10).to_i % 6]
print "\r#{glyph} #{current}/#{total}"
end
@pyrsmk
pyrsmk / service.rb
Last active July 2, 2020 20:01
Example of a pipe implementation in a service, from the functional programming world 🤖
class SomeService
def call(some_value, bypass_some_method4 = false)
pipe(
some_value,
:some_method1,
:some_method2,
-> (value) { value * 2 },
bypass_some_method4 ? :noop : :some_method4,
:some_method5
)