Skip to content

Instantly share code, notes, and snippets.

@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);
@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
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();
});

Keybase proof

I hereby claim:

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

To claim this, I am signing this object:

import md5 from "js-md5";
import { base64ToUint8Array, splitUint8Array, concatUint8Arrays } from "./utils";
const HEAD_SIZE_DWORD = 2;
const SALT_SIZE_DWORD = 2;
export async function decryptCryptoJSCipherBase64(
cryptoJSCipherBase64,
password,
@qwtel
qwtel / promise-race-truthy.js
Last active October 13, 2021 02:11
Returns the first promise that resolves with a truthy value, or undefined if all promises resolve with a nullish value.
const NEVER = new Promise(() => {});
async function raceTruthy(iterable) {
const ps = [...iterable].map(_ => Promise.resolve(_));
let { length } = ps;
const continueWhenNullish = value => value != null
? value
: --length > 0
? NEVER
: undefined;
@qwtel
qwtel / README.md
Last active August 12, 2020 04:41
Subtypes of the browser's own Request and Response types adopted for JSON objects.

Main difference to regular Response and Request types is that no manual JSON.stringify and headers.set('Content-Type', ...) is required.

In my opinion, JSONRequest is the most lightweight way of fixing the biggest issue of the Fetch API when dealing with JSON APIs, without resorting to full alternatives such as superagent or axios.

Example:

const response = await fetch(new JSONRequest('/comments', {
  method: 'POST',
 body: { text: 'Usage example: ...' },
export const urlWithParams = (url, params) => {
const u = new URL(url, global.location.origin)
if (params) {
u.search = new URLSearchParams([...u.searchParams, ...Object.entries(params)])
}
return u.href
}
@qwtel
qwtel / ParamsURL.js
Last active November 20, 2019 20:35
export class ParamsURL extends URL {
constructor(href, params, origin = globalThis.location) {
super(href, origin);
if (params) {
this.search = new URLSearchParams([...this.searchParams, ...Object.entries(params)]);
}
}
}
@qwtel
qwtel / 10.js
Created December 10, 2019 13:13
const nearestAsteroidsByAngle = pipe(
asteroids.filter(mkNe(laserPos)),
groupBy(mkCalcAngle(laserPos)),
mapValues(ps => ps.sort((p1, p2) => distToLaser(p1) - distToLaser(p2))),
intoMap(),
);
pipe(
cycle([...nearestAsteroidsByAngle.keys()].sort((a, b) => a - b)),
skipWhile(a => a < -Math.PI/2),