Skip to content

Instantly share code, notes, and snippets.

View danielearwicker's full-sized avatar
💭
Learning, yearning, occasionally gurning

Daniel Earwicker danielearwicker

💭
Learning, yearning, occasionally gurning
View GitHub Profile
@danielearwicker
danielearwicker / hash-base64.ts
Created October 26, 2022 16:16
Getting base64 string of SHA-256 from some string input, natively in modern browsers
const message = "Hello, this is the message";
const hash = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(message)));
const base64 = btoa(String.fromCharCode(...hash)));
@danielearwicker
danielearwicker / usePartialCall.ts
Created May 15, 2021 10:30
usePartialCall - binding more args to end of a callback, avoid re-rendering a pure react component
import { useEffect, useRef, useMemo } from "react";
type Args = readonly unknown[];
function usePartialCall<A extends Args, M extends Args, R>(
f: (...args: [...A, ...M]) => R,
...moreArgs: M
) {
const ref = useRef(moreArgs);
@danielearwicker
danielearwicker / CudoEvent.cs
Created January 21, 2017 21:52
Cudo (Create, Update, Delete, Observe) - subscribers get Create events to recreate current contents, followed by subsequent updates
namespace Cudo
{
public class CudoEvent<TRecord>
{
public CudoEvent(CudoEventType type, TRecord record)
{
Type = type;
Record = record;
}
@danielearwicker
danielearwicker / twoWay.js
Created July 8, 2017 08:22
Very minimal two-way binding example
import React from 'react';
function twoWay(Comp) {
return props => {
const { value, children, ...others } = props;
return (
<Comp {...others}
@danielearwicker
danielearwicker / example.ts
Last active February 19, 2017 09:31
Remox (Redux in MobX)
import { autorun } from "mobx";
import { createStore, Action, Reducer } from "./remox"
interface Person { firstName: string; lastName: string; }
interface SetFirstName { type: "SET_FIRST_NAME", newFirstName: string; }
interface SetLastName { type: "SET_LAST_NAME", newLastName: string; }
function personReducer(state:Person, action: SetFirstName | SetLastName) {
interface Person {
readonly name?: string;
readonly age?: number;
}
const x = record<Person>({ name: "Bart" }).set({ age: 10 });
const y = x.set({ age: 11 });
@danielearwicker
danielearwicker / MediaWikiBackup.cs
Created February 19, 2016 13:30
Simple minimal backup of mediawiki
using System;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace WikiBackup
{
class Program
@danielearwicker
danielearwicker / node-recursion-async
Created July 22, 2013 08:46
A pure library approach to async/await in standard JavaScript #2
var fs = require('fs');
var path = require('path');
var Q = require('q');
var interrupt = require('./interrupt.js');
var readdir = interrupt.bind(Q.nfbind(fs.readdir));
var stat = interrupt.bind(Q.nfbind(fs.stat));
var consoleLog = interrupt.bind(console.log);
@danielearwicker
danielearwicker / node-recursion-sync
Created July 22, 2013 08:44
A pure library approach to async/await in standard JavaScript - #1
var fs = require('fs');
var path = require('path');
var recurseDir = function(dir) {
fs.readdirSync(dir).forEach(function(child) {
if (child[0] != '.') {
var childPath = path.join(dir, child);
if (fs.statSync(childPath).isDirectory()) {
recurseDir(childPath);
} else {
@danielearwicker
danielearwicker / person.js
Created February 16, 2013 12:20
Object instances without using this/new/prototype
var person = function(firstName, lastName) {
return {
getFullName: function() {
return firstName + ' ' + lastName;
},
setFirstName: function(newName) {
firstName = newName;
},
setLastName: function(newName) {