Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View johan's full-sized avatar

Johan Sundström johan

View GitHub Profile
@johan
johan / selectArg.ts
Created August 1, 2023 03:11
Some ts utilities handy for well-typed reselect createSelector input functions.
/** Constructs a new function type from `F`, instead returning `R` */
export type Returns<F, R> = F extends (...args: any[]) => infer _
? (...args: Parameters<F>) => R
: never;
/**
* Given a (zero-indexed) positional argument number `n`
* and a function call signature generic type `F`,
* produce a well-typed function that selects that `n`th arg.
* @example
@johan
johan / index.html
Created July 20, 2012 05:02
HTML5 GPS tracker
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5 GPS</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" href="style.css">
</head>
@johan
johan / dec-2022.html
Last active December 19, 2022 15:08
Vacation Day Planner
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<base href="https://www.timeanddate.com/date/workdays.html?d1=01&amp;m1=05&amp;y1=2018&amp;d2=9&amp;m2=7&amp;y2=2018&amp;">
<title>Vacation Day Planner</title>
<script>
const sumUp = (td) => { const t = td.parentNode.parentNode, r = t.rows[t.rows.length-1], c = r.cells[r.cells.length-1]; c.textContent = Array.from(t.querySelectorAll('td')).filter(c => c.classList.contains('hl')).length; c.style.color = '#000'; }
document.onclick = (e) => { if (e.target.nodeName === 'TD') { const t = e.target, cl = t.classList; cl.contains('hl-b') || cl.contains('hl') ? cl.remove('hl-b', 'hl') : cl.add('hl'); sumUp(t); } };
const pad = (n) => (n < 10 ? '0' : '') + n;
const date = (t) => t.getFullYear() + '-' + pad(t.getMonth() + 1) + '-' + pad(t.getDate());
const ONE_DAY = 864e5;
const ghostThePast = () => {
@johan
johan / readme.md
Last active October 17, 2022 23:14 — forked from johan/autofork.js
(Re)fork any gist, including your own

Want to fork your own gists? No fork button? No problem! Install this user script by clicking refork.user.js' "raw" link down below: ⇓

@johan
johan / jquery.nodoubletapzoom.js
Created March 15, 2012 22:59
A jQuery plugin to selectively disable the iOS double-tap-to-zoom action on specific page elements (and have that generate two click events instead).
// jQuery no-double-tap-zoom plugin
// Triple-licensed: Public Domain, MIT and WTFPL license - share and enjoy!
(function($) {
var IS_IOS = /iphone|ipad/i.test(navigator.userAgent);
$.fn.nodoubletapzoom = function() {
if (IS_IOS)
$(this).bind('touchstart', function preventZoom(e) {
var t2 = e.timeStamp
@johan
johan / octocat.svg
Created June 4, 2011 11:16
Github octocat avatar, SVG format
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@johan
johan / Makefile
Created January 24, 2017 06:15
Makefile of the steps in Mike Bostock's command-line cartography tutorial, parts 1-4 https://medium.com/@mbostock/command-line-cartography-part-1-897aa8f8ca2c
# request one at http://api.census.gov/data/key_signup.html and paste it below
CENSUS_API_KEY=YOUR_CODE_HERE
# a factor 1609.34 squared
SQ_M_TO_SQ_MI=2589975.2356
#prereqs:
# npm install -g shapefile # 0.6.1
# npm install -g d3-geo-projection # 1.2.1
# npm install -g ndjson-cli # 0.3.0
@johan
johan / README.md
Last active November 6, 2021 14:09 — forked from NV/Readme.md
JS debug breakpoint / log snippets

stopBefore.js

2min screencast

These tools inject a breakpoint, console.log or console.count in any function you want to spy on via stopBefore('Element.prototype.removeChild') or ditto stopAfter, logBefore / logAfter / logAround / logCount.

Works in Chrome DevTools and Safari Inspector; Firefox dev tools reportedly less so.

const units = [["ms", 1000], ["s", 60], ["m", 60], ["h", 24], ["d", 7], ["w", 52.142857142858986], ["y", Infinity]];
const duration = (ms) => units.reduce(
({ left, s }, [u, o]) => {
const n = left % o;
left -= n;
n && s.unshift(`${~~n}${u}`);
return { left: ~~(left / o), s };
},
{ left: ms, s: [] }
@johan
johan / LICENSE
Created January 23, 2017 22:25
A small ES6 credit card number validation and formatting library based on https://github.com/omarshammas/jquery.formance/blob/master/src/zfields/credit_card_number.coffee
Copyright (c) 2013 Omar Shammas (omar@shammaslabs.com)
and Stripe (alex@stripe.com)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions: