Skip to content

Instantly share code, notes, and snippets.

Avatar
🤖
boop. boop. boop beep boop.

Aurélien Delogu pyrsmk

🤖
boop. boop. boop beep boop.
View GitHub Profile
@pyrsmk
pyrsmk / limiter.ts
Created Jul 8, 2022
A simple rate limiter that waits for N milliseconds before running the next callback.
View limiter.ts
/*
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 Jul 1, 2022
A simple function that marks a callback as busy to avoid calling it multiple times before it has finished its task.
View busy.ts
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 Jul 8, 2022
Read a directory recursively
View recursive-readdir-sync.ts
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 Jan 19, 2022
Convert XML files to JS objects.
View xml2obj.js
// 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 Jul 8, 2022
A queue that handles async functions in order with a wait time between each callback.
View async_queue.ts
/*
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>) => {
View svelte-lint.js
// 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 Mar 15, 2022
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 🦄
View event_testing.html
<!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 Nov 26, 2020
Current directory example in Makefile
View Makefile
current_dir := $(dir $(abspath $(firstword $(MAKEFILE_LIST))))
.PHONY=install
install:
ln -sf $(current_dir)config ~/.test/config
@pyrsmk
pyrsmk / fancy_loader.rb
Created Jul 2, 2020
A fancy dots loader
View fancy_loader.rb
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 Jul 2, 2020
Example of a pipe implementation in a service, from the functional programming world 🤖
View service.rb
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
)