Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / README.md
Created January 24, 2023 11:08
test stylesheet font face definitions

This is supposed to be served next to a fonts.css file defining one or more font-families, ex:

@font-face {
  font-family: "American Captain";
  src: url("./American-Captain.woff") format("woff");
}
@JosePedroDias
JosePedroDias / hack.ts
Last active January 5, 2023 14:30
filter JS complex tree with cycles
const ignoreKeys = new Set([
'parent',
'transform',
]);
const numberKeyRgx = /^[0-9]+$/;
const simpleKeyRgx = /^[a-zA-Z_\$][a-zA-Z0-9_\$]*$/;
const getExpr = function (key: string) {
if (numberKeyRgx.test(key)) return `[${key}]`;
return (simpleKeyRgx.test(key)) ? `.${key}` : `['${key}']`;
@JosePedroDias
JosePedroDias / configuration.nix
Created January 3, 2023 23:58
run nixos on M1 with UTM
{ config, pkgs, ... }:
{
imports =
[
./hardware-configuration.nix
];
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
@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 / JSON_number_limits.md
Created July 20, 2019 11:58
JSON number limits

JSON number limits

So we we're using an API which returns a JSON response. One of its attributes is a numeric key. Due to historical reasons we're now being served longer number (longs) so the server, which is not based on JavaScript, started returning long integers.

I had heard about issues like this but hadn't cross against a real use case before.

So what started happening on our JavaScript clients (browser and React Native alike) is that the primitive value we get back once we get the fetch json promise resolved is an overflown number.

JavaScript engines commonly have the symbol Number.MAX_SAFE_INTEGER so one can retrieve the number above which problems start to appear (it is 9007199254740991).

@JosePedroDias
JosePedroDias / debug_cookie_set.js
Created July 13, 2020 18:02
tampermonkey userscripts for cookies and localStorage
@JosePedroDias
JosePedroDias / saveSvgFile.js
Created February 15, 2016 01:04
to save svg files generated from snapsvg or other svg lib
function saveSvgFile(svgEl, linkLabel) {
svgEl.setAttribute('version', '1.1');
svgEl.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
var markup = svgEl.outerHTML;
var b64 = btoa(markup);
var aEl = document.createElement('a');
aEl.setAttribute('download', linkLabel + '.svg');
aEl.href = 'data:image/svg+xml;base64,\n' + b64;
document.body.appendChild(aEl);
aEl.click();
@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) {