Skip to content

Instantly share code, notes, and snippets.

View Floofies's full-sized avatar
🧩
Looking for a job

Dani Glore Floofies

🧩
Looking for a job
View GitHub Profile
@Floofies
Floofies / customApply.js
Last active September 10, 2018 23:08
Unrolled Argument Applier
function apply(functor, thisArg = null, argsArray = null) {
if (argsArray !== null && argsArray.length > 8) {
// More than 8 arguments, use apply
return functor.apply(thisArg, argsArray);
}
if (thisArg === null) {
// thisArg is not present:
// 0 arguments, call directly
if (argsArray === null || argsArray.length === 0) return functor();
// 8 or less arguments, call with directApplier
@Floofies
Floofies / dispatcher2.js
Created August 20, 2018 23:01
Coroutine Dispatcher Prototype 2
function Coroutine(functor, thisArg = null, args = null) {
this.type = "coroutine";
this.image = functor;
this.instance = null;
this.args = args;
this.thisArg = thisArg;
}
Coroutine.prototype.call = function (nextValue) {
if (this.instance === null) {
if (this.thisArg !== null) {
@Floofies
Floofies / binaryExpansionGenerator.js
Last active December 5, 2018 20:42
Generates binary expansion integers in order
function* binaryExpansionGenerator(int = 1, roof = 2048) {
while (roof > int) {
int = int * 2;
yield int;
}
};
console.log(Array.from(binaryExpansionGenerator()));
@Floofies
Floofies / authSlowdown.js
Last active October 22, 2018 19:09
Slows down failed authentication attempts
const tryLimit = 10; // Number of tries allowed in try period
const tryPeriod = 1800000; // 30 Minutes until tries expire
const banPeriod = 1800000; // 30 Minutes until bans expire
const cooldownIndex = new Map(); // Index of authentication attempts and bans
// Takes Express request object to set ban/cooldown for the client
function setCooldown(req) {
const time = Date.now();
const ip = getIP(req);
var cooldown = cooldownIndex.get(ip);
if ((typeof cooldown) === "undefined") {
@Floofies
Floofies / coroutineExperiment2.js
Last active July 31, 2018 23:19
Showing possible semantics of transforming regular subroutines into coroutines.
// Original source code supplied by userland
function userFunc() {
var one = 1;
var two = 2;
var three = one + two;
return three;
}
// Same source code after compiling with HertzScript
@Floofies
Floofies / coroutineExperiment.js
Last active July 31, 2018 23:20
An example of coroutines and a simple coroutine scheduler.
function Dispatcher() {
this.coroutine = null;
this.loc = -1;
this.stack = [];
}
Dispatcher.prototype.run = function* () {
var returnValue = null;
while (this.loc !== -1) {
this.coroutine = stack[loc];
const state = this.coroutine.next(returnValue !== null ? returnValue : undefined);
@Floofies
Floofies / switchGenerator.js
Last active November 12, 2018 20:14
Generator/iterator
function generator() {
var loc = 0;
const state = { done: false, value: undefined };
const iterator = {
next: function() {
switch(loc) {
case 0:
console.log("Hello World");
loc++;
break;
@Floofies
Floofies / Array-Prototype-on-NodeList.html
Last active July 31, 2018 23:31
Testing which Array prototype methods work on NodeList
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<h1>See Console for test results.</h1>
<div id="testNode">
<i></i>
<i></i>
<i></i>
<i></i>
@Floofies
Floofies / tinyMVVM.js
Last active June 11, 2019 16:06
Tiny baby MVVM with nestable Views
// Ultra basic DataStore with publisher/subscriber model
function DataStore(state = null) {
this.state = {};
if (state !== null) this.state = state;
this.subscribers = [];
}
DataStore.prototype.stateChanged = function () {
for (const callback of this.subscribers) callback(() => this.getState());
};
DataStore.prototype.setState = function (newState) {
@Floofies
Floofies / promiseFilter.js
Last active September 9, 2022 11:28
Returns a Promise which resolves with a new Array, containing only values which passed the test.
/**
* promiseFilter - Returns a Promise which resolves with a new Array, containing only values which passed the test.
* @param {Array} array An Array from which to filter values into a new Array.
* @param {callback} test A callback function which must return a Promise, which must resolve with a Boolean.
* @callback test
* @param {any} value The current value from `array`.
* @param {Number} index The current index being used to access `array`.
* @param {Array} array The Array being iterated through, `array`.
* @returns {Promise} A Promise which resolves with a new Array containing values which passed the test.
*/