This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* binaryExpansionGenerator(int = 1, roof = 2048) { | |
while (roof > int) { | |
int = int * 2; | |
yield int; | |
} | |
}; | |
console.log(Array.from(binaryExpansionGenerator())); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head></head> | |
<body> | |
<h1>See Console for test results.</h1> | |
<div id="testNode"> | |
<i></i> | |
<i></i> | |
<i></i> | |
<i></i> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. | |
*/ |