Skip to content

Instantly share code, notes, and snippets.

@qwtel
qwtel / jsonml.js
Created September 17, 2016 05:10
The problem with HTML? Not enough JSON!
function render() {
return (
['aside', { id: 'sidebar' },
['div', { className: 'sidebar' },
['div', { className: ['container', 'sidebar-sticky'] },
['div', { className: 'sidebar-about' },
['h1', { className: 'font-accent' },
['a', { tabindex: '1', href: site.baseurl },
site.title
],
import styled from 'styled-components';
const Button = styled.button`
background: white;
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
@qwtel
qwtel / retry.js
Created March 18, 2017 15:36
Solid retry logic with rxjs5
// emits a value after `init` ms, then after `init * exp` ms, then `init * exp * exp` ms, etc.
function expInterval(init, exp) {
return Observable.create((observer) => {
let n = init;
let id;
function next() {
observer.next(n);
n *= exp;
id = setTimeout(next, n);
@qwtel
qwtel / min-dur.js
Last active March 26, 2017 19:45
Make an observable take at least a certain time
const MIN_DURATION = 100;
ajaxRequest(href)
.zip(Observable.timer(MIN_DURATION), x => x))
.subscribe(response => {
// will execute after at least 100ms or longer depending on how long the request takes
});
@qwtel
qwtel / createTween.js
Last active November 6, 2018 18:18
rxjs5 tweening helper function. Creates a tween observable that emits samples from an easing function via requestAnimationFrame.
import { Observable } from 'rxjs/Observable';
/**
* Creates an observable that emits samples from an easing function on every animation frame
* for a duration `d` ms.
*
* The first emitted value will be a sample form the easing function at `t = 0`,
* and the last emitted value is guaranteed to be the easing function at `t = d`.
*
* It can be used with any of [Robert Penner's easing functions](http://robertpenner.com/easing/),
function filterWithAll(p$, ...others) {
if (process.env.DEBUG && !p$) throw Error();
else if (others.length === 0) {
return this::withLatestFrom(p$)::filter(([, p]) => p)::map(([x]) => x);
} else {
return this::withLatestFrom(p$, ...others)
::filter(([, ...ps]) => ps.every(p => p))
::map(([x]) => x);
}
}
@qwtel
qwtel / getFiles.js
Last active September 9, 2023 13:43
[node.js 8+] Recursively get all files in a directory
// Node 8+
// --------------------------------------------------------------
// No external dependencies
const { promisify } = require('util');
const { resolve } = require('path');
const fs = require('fs');
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
@qwtel
qwtel / test.md
Last active September 17, 2017 11:56
@qwtel
qwtel / quick-and-dirty-set.js
Created October 13, 2017 14:39
Quick-and-Dirty `Set` implementation
// Quick-and-Dirty `Set` implementation.
/* eslint-disable */
export const Set = global.Set || function (a = []) {
a = a.filter((x, i) => i === a.indexOf(x));
a.size = a.length;
a.has = x => a.indexOf(x) > -1;
a.add = x => { if (!a.has(x)) { a.size++; a.push(x); } return a; };
a.delete = x => { let t; if (t = a.has(x)) { a.size--; delete a[a.indexOf(x)]; } return t; };
a.keys = a.values = () => a[Symbol.iterator]();
a.clear = () => { while (a.pop()) {} a.size = 0; };
<!-- This is an exemplary mailchimp subscription form -->
<div id="mc_embed_signup">
<form action="{{ site.mailchimp.action }}" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<h2>Subscribe to our mailing list</h2>
<div class="mc-field-group form-group">
<label for="mce-EMAIL">Email Address <span class="asterisk">*</span></label>
<input type="email" value="" name="EMAIL" class="form-control required email" id="mce-EMAIL">
<small class="indicates-required form-text text-muted"><span class="asterisk">*</span> indicates required</small>
</div>