Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / instrument.js
Last active April 13, 2025 21:43
ad hoc instrument a js file for coverage and sequencial call analysis
import { parse } from 'acorn';
import { simple } from 'acorn-walk';
import { generate } from 'escodegen';
import fs from 'fs';
// https://astexplorer.net/
function addCounterToNode(node) {
if (node.loc) {
const li = node.loc.start.line;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JosePedroDias
JosePedroDias / weightedRandom.mjs
Created December 2, 2023 19:08
returns random outcomes based on a configuration of their weights
// array of [outcome, weight]
export function weightedRandom(arr) {
const outcomeCuts = [];
let accum = 0;
for (const [out, weight] of arr) {
accum += weight;
outcomeCuts.push([out, accum]);
}
const r = accum * Math.random();
let best;
@JosePedroDias
JosePedroDias / AesCbc.ts
Last active October 31, 2023 11:55
AES CBC friendly impl using subtle crypto for both nodejs and the browser
// import { webcrypto as crypto } from 'node:crypto'; // NODE
// eslint-disable-next-line no-undef
const crypto = globalThis.crypto; // BROWSER
const subtle = crypto.subtle;
const ALGO = 'AES-CBC';
// HELPER FUNCTIONS
@JosePedroDias
JosePedroDias / produce_consume.ts
Created October 20, 2023 09:22
multiple consumers of a promise
function defer<T>(): {
promise: Promise<T>;
resolve: (resp: T) => void;
reject: (err: any) => void;
} {
const answer: any = {};
answer.promise = new Promise((resolve, reject) => {
answer.resolve = resolve;
answer.reject = reject;
});
@JosePedroDias
JosePedroDias / stacklang.mjs
Last active October 14, 2023 00:51
simplest stack calculator
import { test } from 'node:test';
import { strictEqual, deepEqual } from 'node:assert';
/*
inspired by https://www.youtube.com/watch?v=umSuLpjFUf8
3 3 * 4 4 * + sqrt
equivalent to
Math.sqrt(3*3 + 4*4)
*/
@JosePedroDias
JosePedroDias / streamLines.mjs
Created September 15, 2023 17:12
streams a non-binary file avoiding loading it all to memory
import { createReadStream } from 'fs';
export function streamSplitBy(file, onMatch, splitBy='\n') {
return new Promise((resolve) => {
const stream = createReadStream(file);
let ongoing = '';
stream.on('data', buff => {
const chunk = buff.toString();
@JosePedroDias
JosePedroDias / spy.ts
Last active September 27, 2023 15:36
spies of method calls and optionally returns prepared results instead
export function spy(fn: Function, prot = fn.prototype) {
function proxyFn() {
const args = Array.prototype.slice.call(arguments);
const call = { args, result: undefined, error: undefined, real: true };
proxyFn.calls.push(call);
let responded = false;
// check if preparedCalls can answer
type SortableType = number | string | boolean;
type SortFn<T> = (el: T) => SortableType;
interface SortHelperApi<T> {
sortBy(fn: SortFn<T>, decrescent?: boolean): SortHelperApi<T>;
execute(): T[];
}
/**