Skip to content

Instantly share code, notes, and snippets.

View tomfa's full-sized avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / JSON-intArray-converter.js
Created May 10, 2015 11:22
JSON to 8-bit-integer parsing (and visa versa)
// JSON to Uint8Array parsing and visa versa
// (Intended Bluetooth communication on Cordova)
var JsonToArray = function(json)
{
var str = JSON.stringify(json, null, 0);
var ret = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
ret[i] = str.charCodeAt(i);
}
@tomfa
tomfa / urls.test.ts
Last active March 6, 2024 17:55
NextJS route vs pathname matcher
describe('matchesPath', () => {
const matches = [
['/cake', '/cake'],
['/cake', '/cake/'],
['/cake', '/cake?frige=warm'],
['/cake', '/cake?frige=warm&freezer=cold'],
['/[id]', '/cake'],
['/[anything-goes]', '/cake'],
['/c/[id]/practitioner/[pid]/[anything-goes]', '/c/1/practitioner/2/3'],
['/[...rest]', '/cake'],
@tomfa
tomfa / example_completed_notification.xml
Last active February 27, 2024 14:15
Docusign webhook xml example
<?xml version="1.0" encoding="utf-8"?>
<DocuSignEnvelopeInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
<Email>tomas@echo.com</Email>
<UserName>Tomas Fagerbekk</UserName>
@tomfa
tomfa / signals.py
Last active November 16, 2023 00:29
Django Elasticsearch with Celery
from celery import shared_task
from django.db import models, transaction
from django.utils import timezone
from django_elasticsearch_dsl.apps import DEDConfig
from django_elasticsearch_dsl.registries import registry
from django_elasticsearch_dsl.signals import BaseSignalProcessor
from .. import utils
@tomfa
tomfa / schema.prisma
Created October 3, 2023 11:33
Lago invoicing modelling
// autogenerated from Lago database model (https://github.com/getlago/lago)
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
@tomfa
tomfa / .meta.json
Last active October 1, 2023 16:46
Autohotkey script examples (found in 2012-archive)
{
"slides": "https://docs.google.com/presentation/d/1WyicsfMJirQDpJYTVOFwRxwiwgfAVq6fRyylFXc6_G4/edit?usp=sharing",
"date": "September 2012"
}
@tomfa
tomfa / imports-add-ext.js
Created September 20, 2023 20:57
Codemod convert commonJS to esm import path style
/*
jscodeshift transform: adds the '.js' extension
to all import declarations with relative specifiers:
From './file' to './file.js', and
from '../file' to '../file.js'.
*/
module.exports = function (fileInfo, api) {
@tomfa
tomfa / file.utils.ts
Last active July 20, 2023 21:21
TypeScript: fs.readDir recursive implementation
/*
* Walks a directory (fs.readdir but recursively)
* Context:
* - fs does not have support for walk/readdir recursively
* - fs-extra moved "walk" functionality to https://github.com/jprichardson/node-klaw
*/
export const listDirectory = async (
currentPath: string,
includeDirectories = false,
@tomfa
tomfa / fields.py
Last active June 9, 2023 11:21
Django compressed json field / compressed binary field
from django.utils.text import compress_string
from django.core.serializers.json import DjangoJSONEncoder
class CompressedBinaryField(models.BinaryField):
compress = compress_string
@staticmethod
def uncompress(s):
zbuf = io.BytesIO(s)
@tomfa
tomfa / handler.py
Last active May 12, 2023 14:44
AWS Lambda: Python Hello World HTTP API
# This file is your Lambda function
import json
def mirror_query_strings(event, context):
body = {
"queryStrings": event['queryStringParameters']
}
return {
"statusCode": 200,