Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
friendlyanon / xhr-pool.js
Created January 20, 2019 12:16
XHR pool
class Xhr {
constructor(opts) {
this.options = opts;
}
then(resolve, reject) {
this.xhrPool.add([this.options, resolve, reject]);
}
}
Object.defineProperty(Xhr.prototype, "xhrPool", {
@friendlyanon
friendlyanon / Test.cs
Created December 31, 2018 21:09
Blazor enum reflection bug
using System;
using System.Reflection;
using System.ComponentModel;
namespace EnumTest
{
[Flags]
public enum Longs : long
{
[Description("1 bit")]
class ReusablePromise {
constructor() {
this.executor = this._executor.bind(this);
this.resolve =
this.reject =
this.root =
this.args =
this.method = null;
}
setup(root, method, ...args) {
"use strict";
const { abs, atan2, round, sqrt, cos, sin } = Math;
const vectorRe = /(?<x>\d*\.?\d*)\s*,?\s*(?<y>\d*\.?\d*)?\s*,?\s*(?<z>\d*\.?\d*)?/;
function num(n, d) {
return typeof n !== "undefined" ? parseFloat(n) : d;
}
function parseArgs(x, y, z) {
const { length } = arguments;
"use strict";
class Optional {
constructor(value, isError) {
this._value = value;
this._ok = !isError;
}
[Symbol.toPrimitive](hint) {
switch (hint) {
case "number": return this._ok ? 1 : 0;
const fs = require("fs");
const readline = require("readline");
// https://gist.github.com/friendlyanon/afd5a18811c540bc8bcf3f40560d4c74
const Queue = require("./queue");
class _Readline {
constructor(stream) {
this.clear();
(stream
.on("line", l => this.feed(l))
"use strict";
const fs = require("fs");
// https://gist.github.com/friendlyanon/afd5a18811c540bc8bcf3f40560d4c74
const Queue = require("./queue");
function nodeCb(error, result) {
const { resolve, reject } = this;
this.data =
this.resolve =
this.reject = null;
const luhnLookup = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
// prerequisite: str is a numeric string
function luhn(str) {
let sum = 0, i, j = (i = str.length + 1) - 1;
while (i > 1) sum += str.charCodeAt(i -= 2) - 48;
while (j > 1) sum += luhnLookup[str.charCodeAt(j -= 2) - 48];
return !(sum % 10);
}
@friendlyanon
friendlyanon / array-from-proper-polyfill.js
Created October 31, 2018 16:49
Proper Array.from polyfill
if (!(Array.hasOwnProperty("from") && typeof Array.from === "function")) {
Array.from = function(arrayLike) {
var result, i, len, mapFn, thisArg;
if (arrayLike == null) {
throw new TypeError("Array.from requires an array-like object - not null or undefined");
}
if (arguments.length > 1) {
if (typeof (mapFn = arguments[1]) !== "function") {
throw new TypeError("Array.from: when provided, the second argument must be a function");
}
@friendlyanon
friendlyanon / pipe.js
Last active April 3, 2019 17:56
Node IPC on Windows
import net from "net";
import EventEmitter from "events";
const PipeEmitter = class PipeEmitter extends EventEmitter {
constructor(pipeName, listen) {
super();
this.pipeAddress = "\\\\.\\pipe\\" + pipeName;
this.listen = listen;
this.server =