Skip to content

Instantly share code, notes, and snippets.

View jonaskuske's full-sized avatar
👀
[[IsHTMLDDA]]

Jonas Kuske jonaskuske

👀
[[IsHTMLDDA]]
View GitHub Profile
@jonaskuske
jonaskuske / README.md
Last active February 4, 2018 02:09
Minimal hyperapp router

Minimal hyperapp router

1. Usage

Configure

Specify your routes with paths and corresponding components

const routes = [
@jonaskuske
jonaskuske / README.md
Last active December 6, 2018 01:01
qJerry – convenience methods for DOM manipulation

qJerry

Zissle selector engine and more... 😜

Adds some methods commony known from jQuery to Elements' prototype.
Yes, prototype – better not use this in production... =)

@jonaskuske
jonaskuske / happy.js
Created December 6, 2018 00:29
Spread happyness
// Copy this into the browser console
let _=i=$D=1;Object.defineProperty(this,'mood',{set(){console.log(`Made ${i++} folks happy!`);Math.random()<.1&&(_=0)}})
// Then you can do this and find out how many people you made happy :)
please: for (;_;) mood = $D
@jonaskuske
jonaskuske / keybase.md
Created December 15, 2018 03:13
Keybase authentication

Keybase proof

I hereby claim:

  • I am jonaskuske on github.
  • I am jonaskuske (https://keybase.io/jonaskuske) on keybase.
  • I have a public key ASDmu_cLaafasTpFdOxjEHDjScJaycNtp3EydJ3CtvvNBQo

To claim this, I am signing this object:

@jonaskuske
jonaskuske / iterableNumber.js
Last active September 28, 2020 21:20
How to make Numbers iterable as quick as possible
// Shortest (96 chars):
Number.prototype[Symbol.iterator]=function(){let i=0;return{next:()=>({done:i>this,value:i++})}}
// Only return value if not done yet (112 chars):
Number.prototype[Symbol.iterator]=function(){let i=0;return{next:()=>({done:i>this,...!(i>this)&&{value:i++}})}}
// Readable:
Number.prototype[Symbol.iterator] = function() {
let i = 0;
@jonaskuske
jonaskuske / recursiveAssign.js
Last active February 17, 2019 20:12
Recursive Object.assign / merge
const isPlainObject = object => Object.prototype.toString.call(object) === '[object Object]'
const recursiveAssign = (target, ...sources) => {
sources.forEach(source => {
Object.entries(source).forEach(([key, value]) => {
if (isPlainObject(target[key]) && isPlainObject(value)) recursiveAssign(target[key], value)
else target[key] = value
})
})
return target
}
@jonaskuske
jonaskuske / load-scroll-polyfill.js
Last active February 19, 2019 02:29
Smallest way to load smoothscroll polyfills
// Loading "smoothscroll-polyfill" & "smoothscroll-anchor-polyfill" + setting scroll-behavior to smooth in 247 bytes:
((d,s,u)=>{d.documentElement.setAttribute('style','scroll-behavior:smooth');let i=s=>{let e=d.createElement('script');e.src=s;d.head.append(e)};i(u+s+`-polyfill/dist/${s}.min.js`);i(u+s+'-anchor-polyfill')})(document,'smoothscroll','//unpkg.com/')
// Readable version:
((doc, name, cdn) => {
// Set scroll-behavior, setAttribute so it works in browsers that don't recognize the property
doc.documentElement.setAttribute('style', 'scroll-behavior: smooth;');
// Function to insert scripts...
const insertScript = url => {
const scriptElement = doc.createElement('script');
@jonaskuske
jonaskuske / swagger-to-ts.js
Created September 18, 2019 15:12
Extract a TypeScript type from a Swagger Spec website
/* Used to extract a from a Swagger API spec */
function generateTypeScriptInterface(targetDef, definitions) {
if (typeof targetDef === 'string') {
definitions =
definitions ||
window.__defs__ ||
(window.__defs__ = JSON.parse(document.querySelector('body pre').textContent).definitions);
targetDef = definitions[targetDef];
}
import React, { useState, useEffect, useCallback } from 'react'
export const IDLE = 'IDLE'
export const LOADING = 'LOADING'
export const SUCCESS = 'SUCCESS'
export const ERROR = 'ERROR'
export function useAsyncData(fetchFn, deps) {
const stableFetchFn = useCallback(fetchFn, deps)
const [{ state, data, error }, setState] = useState({ state: IDLE })
@jonaskuske
jonaskuske / promise.md
Last active November 24, 2022 07:20
A+ compliant Promise implementation