Skip to content

Instantly share code, notes, and snippets.

function fetchRx(url, options) {
const controller = new AbortController();
const { signal } = controller;
return Observable.create(observer => {
fetch(url, { signal, ...options })
.then(x => observer.next(x))
.catch(x => observer.error(x))
.finally(() => observer.complete())
return () => controller.abort();
});
@qwtel
qwtel / sigV4Client.js
Last active February 20, 2019 17:31
/*
* Copyright (c) 2018 Florian Klampfer <https://qwtel.com/>
*
* This software uses portions of `sigV4Client.js`,
* which is Apache-2.0 licensed with the following copyright:
*
* > Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Changes:
* * Replaced "crypto-js" with Web Cryptography API
@qwtel
qwtel / jsx-create-element.js
Last active December 6, 2017 10:27
Patches the default document.createElement function to follow the JSX/React.createElement function signature.
var createElement = document.createElement.bind(document);
function appendChild(child) {
this.appendChild(
child instanceof Element ? child : document.createTextNode(child)
);
}
document.createElement = function(tagName, attrs, children) {
var el = createElement(tagName);
<!-- 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>
@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; };
@qwtel
qwtel / test.md
Last active September 17, 2017 11:56
@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);
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 / 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/),
@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
});