Skip to content

Instantly share code, notes, and snippets.

View josdejong's full-sized avatar

Jos de Jong josdejong

View GitHub Profile
@josdejong
josdejong / detectFont.ts
Created October 9, 2025 12:05
A JavaScript function to test whether a font is supported or not
/**
* Usage:
*
* detectFont('arial') // true
*/
function detectFont(fontName: string) : boolean {
const canvas = window.document.createElement('canvas')
const context = canvas.getContext('2d')
const text = 'i' // this text has a different width for monospace and sans-serif
@josdejong
josdejong / npm.js
Last active July 15, 2025 15:19
Helper function to load ESM packages from NPM in your browser dev tools console
/**
* Load an ESM package from npm in your JavaScript environment.
*
* Loads packages from https://www.npmjs.com/ via the CDN https://www.jsdelivr.com/
*
* Usage:
*
* const lodash = await npm('lodash-es')
* const lodash = await npm('lodash-es@4')
* const lodash = await npm('[email protected]')
@josdejong
josdejong / merge.kt
Last active July 30, 2025 05:53
Merge two data classes in Kotlin
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.primaryConstructor
/**
* Merge two data classes
*
* The resulting data class will contain:
* - all fields of `other` which are non null
* - the fields of `this` for the fields which are null in `other`
*
@josdejong
josdejong / package.json
Last active August 29, 2015 14:22
Test ES7 async/await
{
"name": "es7_async",
"version": "0.0.1",
"description": "",
"license": "ISC",
"dependencies": {
"asyncawait": "^0.7.4",
"babel-core": "^5.4.4",
"babel-plugin-typecheck": "0.0.3"
}
@josdejong
josdejong / gist:4537647
Last active September 27, 2022 22:55
String interpolation method for underscore.js.
/*
String interpolation method for underscore.js
Usage:
var props = {
first: 'Jos',
last: 'de Jong'
};
var message = _.interpolate('Hello $first $last, welcome!', props);