Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / ArrayUtils.test.ts
Last active December 29, 2022 12:14
DIY browser tests without mocking
import { expect } from 'chai';
import { ArrayUtils } from './ArrayUtils';
function sortByNumericAttribute() {
const arr = [
{ a: 'a', n: 3 },
{ a: 'b', n: 2 },
{ a: 'c', n: 6 },
];
expect(ArrayUtils.sortByNumericAttribute(arr, 'n', false)).to.deep.eq([
@JosePedroDias
JosePedroDias / fetchForJest.js
Created June 23, 2022 09:14
fetch for jest
// to capture actual responses
let i = 0;
global.fetch = jest.fn(async (url, options) => {
console.log(`#${i++}: ${options.method || 'GET'} ${url}`);
const resp = await originalFetch(url, options);
const body = await resp.json();
console.log(` status: ${resp.status}, body: ${JSON.stringify(body, null, 2)}`);
return body;
});
@JosePedroDias
JosePedroDias / connect.sh
Created May 10, 2022 14:56
mysql 5.7 docker-composed
mysql -h localhost -u USER -P 3306 -p --protocol=TCP
@JosePedroDias
JosePedroDias / misc.js
Created February 23, 2022 11:22
misc.js
function clamp(v, m, M) {
return v < m ? m : v > M ? M : v;
}
function average(arr) {
return arr.reduce((prev, curr) => prev + curr, 0) / arr.length;
}
// triangle m0 x l of area 1 (linear degradation)
function averageWithDegradation(arr) {
@JosePedroDias
JosePedroDias / README.md
Last active October 28, 2021 16:38
simple interceptor of webgl context calls (for when spector.js is an overkill)

the code should be called right after we obtain the GL context. Don't forget to reset/edit ignoreCommands if you intend to capture some of those.

example usages:

fakeGl.captureOperations(40)
...
fakeGl.ignoreCommands = []
@JosePedroDias
JosePedroDias / map.geojson
Last active October 16, 2021 09:23
Maratona de Lisboa 2021-10-17
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JosePedroDias
JosePedroDias / index.md
Last active February 7, 2021 16:30
build ODE

Build ODE in Linux

simple approach (can't choose revelant things):

sudo apt add libode-dev

otherwise use premake as explained below

Build ODE in Windows

@JosePedroDias
JosePedroDias / README.md
Last active December 27, 2020 19:20
Fix Strava GPX

Your GPS device has messed up and the recorded route has a faulty segment?

This code iterates over recorded points, finding biggest distance discrepancy and allowing you to remove it and save an altered XML version leaving out those measurements.

@JosePedroDias
JosePedroDias / fetchFireAndForget.ts
Created December 3, 2020 16:24
fetch fire and forget in nodejs (I suppose it works in the browser as well)
import http from 'http';
import https from 'https';
import fetch, { Response } from 'node-fetch';
import { AbortController } from 'abort-controller';
const DEFAULT_TIMEOUT = 10000;
const httpAgent = new http.Agent({
keepAlive: true,
@JosePedroDias
JosePedroDias / homemadeGet.js
Created August 6, 2020 16:19
to eventually use in node instead of request
const http = require('http');
const https = require('https');
const HEADERS = {};
function get(url, body, moreHeaders) {
const prot = url.indexOf('https:') === 0 ? https : http;
return new Promise((resolve, reject) => {
const r = prot.request(
url,