Skip to content

Instantly share code, notes, and snippets.

View brunnerh's full-sized avatar

brunnerh

  • Europe
  • 15:12 (UTC +02:00)
View GitHub Profile
/**
* Function for reading files as promise.
* @param {(reader: FileReader) => void} readCallback
* Execute the read, gets file reader as argument.
* e.g. `r => r.readAsText(file)`
* @returns {Promise<FileReader['result']>}
* Promise that resolves to the file reader result.
*/
export function readFile(readCallback)
{
@brunnerh
brunnerh / entries-from-storage.js
Last active April 30, 2022 12:06
Function for enumerating storage contents.
/**
* Turns a storage into an array of key/value tuples.
* (Same format as the result of {@link Object.entries}.)
* @param {Storage} storage A storage instance (e.g. {@link Window.localStorage}).
* @returns {[string, string][]} Key/value tuple array of entries.
*/
function entriesFromStorage(storage) {
return new Array(storage.length).fill(0)
.map((e, i) => storage.key(i))
.map(key => [key, storage.getItem(key)]);
@brunnerh
brunnerh / dishonored-2-riddle.pl
Created January 15, 2021 17:05
An SWI Prolog solution to the riddle in Dishonored 2.
:- use_module(library(lists)).
:- use_module(library(clpfd)).
:- use_module(library(pairs)).
:- use_module(library(apply)).
solution(P, H, Vs) :-
Table = [Persons, Beaverages, Colors, Heirlooms, Origins],
PersonNames = [winslow, marcolla, contee, natsiou, finch],
HeirloomNames = [diamond, tin, ring, pendant, medal],
Persons = [Winslow, Marcolla, Contee, Natsiou, Finch],
@brunnerh
brunnerh / group-by.js
Last active September 28, 2021 14:59
Groups items in an array by a give selector.
/**
* Groups items in a list by a give selector.
* @template T Type of the items.
* @template K Type of the grouping key.
* @param {Iterable<T>} list The items to group.
* @param {(item: T) => K} by Function for the selector by which to group.
*/
function groupBy(list, by)
{
/** @type {Map<K, T[]>} */
@brunnerh
brunnerh / xpath.js
Created May 3, 2019 18:11
Iterable document.evaluate wrapper.
/**
* Executes an XPath expression returning an iterable node list.
* @param {string} expression XPath expression.
* @param {Node} node Optional context node for query. Default is document element.
* @param {XPathNSResolver | ((prefix: string) => string | null) | null} nsResolver Optional namespace resolver function.
* @returns {Iterator<Node>} Result nodes iterator.
*/
function* xpath(expression, node = document.documentElement, nsResolver = null)
{
const xPathResult = document.evaluate(
@brunnerh
brunnerh / abstract-class.js
Last active December 20, 2018 12:58
Sketch of how to prevent construction of conceptually abstract superclass.
class X
{
constructor()
{
if (Object.getPrototypeOf(Object.getPrototypeOf(this)) != X.prototype)
throw new Error("Cannot construct X directly.");
}
static create(type)
{
@brunnerh
brunnerh / ansi.js
Last active November 3, 2018 15:25
/**
* Creates a basic ANSI escape code string from a number.
* @param {number} x Numeric ANSI escape code.
*/
const ansi = x => `\x1b[${x}m`;
/**
* Adds ANSI escape codes to a string.
* Resets properties at the end of the string.
* @param {string} text Text to colorize.
@brunnerh
brunnerh / _selenium-js-to-net.md
Created October 26, 2018 13:21
Info about Selenium JavaScript to .NET conversions

Selenium JS => .NET

null                            => object <null>
undefined                       => object <null>
42                              => Int64 42
3.1415                          => Double 3.1415
'text'                          => String "text"
{}                              => Dictionary<String, Object> {  }
{ x: 42 }                       => Dictionary<String, Object> { "x": Int64 42 }

{ [0]: 42 } => Dictionary { "0": Int64 42 }

@brunnerh
brunnerh / index.html
Last active September 24, 2018 18:09
coin-hexagon-puzzle
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coin Hexagon Puzzle</title>
<style>
html,