Skip to content

Instantly share code, notes, and snippets.

View branneman's full-sized avatar

Bran van der Meer branneman

View GitHub Profile
@branneman
branneman / index.html
Created April 13, 2024 13:22
HTML5 Video: MediaSource, SourceBuffer, video segments, etc.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, viewport-fit=cover"
/>
<title>video test</title>
</head>
@branneman
branneman / primitive-reference-types-javascript.md
Last active April 13, 2024 05:10
Primitive Types & Reference Types in JavaScript

Primitive Types & Reference Types in JavaScript

An explanation of JavaScript's pass-by-value, which is unlike pass-by-reference from other languages.

Facts

  • JavaScript has 2 kinds of variable types: primitive and reference.
  • A fixed amount of memory is reserved after creation of every variable.
  • When a variable is copied, it's in-memory value is copied.
  • Passing a variable to a function via a call also creates a copy of that variable.

Primitive Types

@branneman
branneman / better-nodejs-require-paths.md
Last active April 8, 2024 00:22
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@branneman
branneman / six-nations-table.test.ts
Last active March 23, 2024 14:53
Six Nations table calculator
import { describe, expect, it } from 'vitest'
import { Table, Match } from './six-nations-table'
import {
matches2table,
updateTable,
getTable,
orderTable,
PgamesPlayed,
WgamesWon,
LgamesLost,
@branneman
branneman / Struct.php
Created May 2, 2011 09:53
PHP Struct class
<?php
class Struct
{
/**
* Define a new struct object, a blueprint object with only empty properties.
*/
public static function factory()
{
$struct = new self;
foreach (func_get_args() as $value) {
@branneman
branneman / call-apply-bind-proxy.js
Last active February 22, 2024 21:06
JavaScript call() vs apply() vs bind() vs $.proxy()
var fn = function(arg1, arg2) {
var str = '<p>aap ' + this.noot + ' ' + arg1 + ' ' + arg2 + '</p>';
document.body.innerHTML += str;
};
var context = {
'noot': 'noot'
};
var args = ['mies', 'wim'];
// Calls a function with a given 'this' value and arguments provided individually.
@branneman
branneman / components-app.tsx
Last active December 7, 2023 11:11
React useTranslation Hook
import { useState } from 'react'
import { Outlet } from 'react-router-dom' // you don't have to use this necessarily, just an example
import Header from 'components/Header'
import { makeTranslationValue } from 'hooks/translation'
import { TranslationContext } from 'context/translation'
import translations from 'data/translations.json'
export default function App() {
@branneman
branneman / index.js
Created December 6, 2023 15:15
Random person selector
const allPeople = ['Aisha', 'Amina', 'Anya', 'Carlos', 'Chen', 'Liam', 'Nia', 'Oliver', 'Raj', 'Yuki' ]
const peopleAlreadySpeaking = ['Anya', 'Raj']
const numberOfPeople = 2
// Fisher–Yates shuffle
function shuffle(list) {
const xs = list.slice()
let currentIndex = xs.length
let randomIndex
@branneman
branneman / radicals.json
Created June 9, 2020 12:37
JSON list of 214 Simplified Chinese Radicals, data contains radical number, pinyin, english translation, stroke count
[
{
"id": 1,
"radical": "一",
"pinyin": "yī",
"english": "one",
"strokeCount": 1
},
{
"id": 2,