Skip to content

Instantly share code, notes, and snippets.

@spacejack
spacejack / svg-path-parser.ts
Created April 5, 2020 02:39
SVGPath parser/serializer sketch
/** Expected number of values for each command type */
const CMD_LENGTHS = {
a: 7, c: 6, C: 6, h: 1, l: 2, m: 2, M: 2, q: 4, s: 4, S: 4, t: 2, v: 1, z: 0
}
type CmdType = keyof typeof CMD_LENGTHS
export interface PathSeg {
/** Command type string */
cmd: CmdType
@spacejack
spacejack / mobile-web-app-html-tags.md
Last active March 15, 2020 04:01
HTML tags for a simple web app (PWA)

Simple PWA HTML tags.

If you don't need the fancy features of a service worker and just want your SPA to behave nicely when a user adds to homescreen, or creates a desktop Chrome shortcut, these are the bare minimum HTML tags needed (not including the title, charset and other usual tags you would also have.)

Description and Image tags are useful for sharing purposes.

<meta name="viewport" content="initial-scale=1, width=device-width"/>
<meta name="mobile-web-app-capable" content="yes"/>
import * as t from './io-ts'
type FieldErrors<T = Record<string, any>> = Partial<Record<keyof T, string>>
/** Creates an empty FieldErrors object for the specified entity type. */
function FieldErrors<T = Record<string, any>>() {
return {} as FieldErrors<T>
}
// Helpers
@spacejack
spacejack / modal-example.ts
Created April 3, 2018 21:02
Example of Mithril Modal component
import * as m from 'mithril'
// Modal state
let isOpen = false
let options: ModalOptions = {}
export interface ModalButton {
id: string
text: string
}
@spacejack
spacejack / mithril-obserer-component.js
Last active July 19, 2021 11:48
Mithril "Observer" Component
/*
This 'Observer' component expects the attrs described below.
The vdom rendered by `view` will re-render normally within
Mithril's redraw cycle.
The vdom rendered by `render` will only re-render when the
data stream updates. This will not trigger global redraws.
`render` renders into the dom node described by `selector`,
otherwise defaults to the component's root node.
// ts2fable 0.5.2
module rec Mithril
open System
open Fable.Core
open Fable.Import.JS
let [<Import("*","mithril")>] Mithril: Mithril.Static = jsNative
type [<AllowNullLiteral>] IExports =
/// Manually triggers a redraw of mounted components.
function createToken() {
let resolve
const promise = new Promise(r => {resolve = r})
return [promise, resolve]
}
const c = function() {
const timeline = Timeline(async (delay) => {
const state = stmu({
declare namespace Mithril {
// ...
/** A redraw boolean property can be set on all events received by event handlers created by hyperscript */
export interface VEvent {
redraw: boolean
}
/** This represents the attributes available for configuring virtual elements, beyond the applicable DOM attributes. */
export interface Attributes extends Lifecycle<any, any> {

deserialize

A deserialization method to be applied to the xhr.responseText. Defaults to a small wrapper around JSON.parse that returns null for empty responses. If extract is defined, deserialize will be skipped.

extract

A hook to specify how the XMLHttpRequest response should be read. Useful for processing response data, reading headers and cookies. By default this is a function that returns xhr.responseText, which is in turn passed to deserialize. If a custom extract callback is provided, the xhr parameter is the XMLHttpRequest instance used for the request, and options is the object that was passed to the m.request call. Additionally, deserialize will be skipped and the value returned from the extract callback will not automatically be parsed as JSON.

declare namespace Reddit {
interface Item {
domain: string;
banned_by: any;
media_embed: any;
subreddit: string;
selftext_html: string;
selftext: string;
likes: any;