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
function IntervalTree() {
this.intervals = [];
this.intervals[0] = [];
this.nodes = [];
}
IntervalTree.prototype.addInterval = function (unit, id) {
if (id > this.intervals.length - 1) {
this.intervals[id] = [];
}
this.intervals[id][this.intervals[id].length] = unit;
@Floofies
Floofies / asserts.js
Last active March 22, 2018 06:23
Basic JavaScript Asserts for Type-Checking
/**
* assert - Logs or throws an Error if `boolean` is false,
* If `boolean` is `true`, nothing happens.
* If `errorType` is set, throws a new Error of type `errorType` instead of logging to console.
* @param {Boolean} boolean The activation Boolean.
* @param {String} message The message to log, or include in the Error.
* @param {Error} errorType = null If not `null`, throws a new error of `errorType`.
*/
function assert(boolean, message, errorType = null) {
if (boolean) return;
@Floofies
Floofies / tinyKernel.js
Last active December 14, 2017 07:13
An asynchronous cooperative multitasking kernel.
const BG_QUANTUM = 50;
const FG_QUANTUM = 300;
function Kernel() {
this.queue = new this.stdLib.CircularDoubleLinkedList();
this.backgroundQueue = new this.stdLib.CircularDoubleLinkedList();
this.queue.tail.next = this.backgroundQueue.head;
this.queue.head.prev = this.backgroundQueue.tail;
this.backgroundQueue.tail.next = this.queue.head;
this.backgroundQueue.head.prev = this.queue.tail;
this.procs = new Map();
@Floofies
Floofies / LexScan.js
Last active January 15, 2018 10:19
Basic Lexical Scanner
const SEMICOLON = [[59, 59]];
const SPACE = [[32, 32]];
const NUMERIC = [[48, 57]];
const ALPHA = [[65, 90], [97, 122]];
const ALPHA_SPACE = [...SPACE, ...ALPHA];
const ALPHA_NUMERIC = [...NUMERIC, ...ALPHA];
const ALPHA_NUMERIC_SPACE = [...SPACE, ...ALPHA_NUMERIC];
// Returns the first contiguous substring in `string`, starting at `start`, which precedes symbols found in `nt`.
function scanToken(string, start = 0, nt = [" "]) {
var subString = "";
@Floofies
Floofies / csvParser.js
Created January 18, 2018 19:08
Basic CSV Parser
// Parses a CSV string into a Row-first array.
function parseCsv(csvString) {
const rows = [[]];
var curRow = rows[0];
var code;
for (var loc = 0; loc < csvString.length; loc++) {
code = csvString.charCodeAt(loc);
if (code === 10 && loc !== csvString.length - 1) {
// Add a Row
curRow = [];
@Floofies
Floofies / taquito.js
Last active December 10, 2018 03:35
A tiny XMLHttpRequest wrapper.
function XHR(url = "/", verb = "GET", data = null) {
this.requestor = null;
this.url = url;
this.verb = verb;
this.data = data;
}
XHR.prototype.executor = function (resolve, reject) {
this.requestor.onreadystatechange = () => this.stateChange(resolve, reject);
this.requestor.ontimeout = () => this.timeout(resolve, reject);
this.requestor.onerror = reject;
function isFloat(int) {
return int % 1 !== 0;
}
function padBinary(string, pad = 7) {
while(string.length < pad) string = "0".concat(string);
return string;
}
if ((typeof window) === "undefined" || !("btoa" in window)) var btoa = v => Buffer.from(v).toString("base64");
if ((typeof window) === "undefined" || !("atob" in window)) var atob = v => Buffer.from(v, "base64").toString();
const toBase64 = btoa;
@Floofies
Floofies / bits.js
Last active January 18, 2023 17:30
A collection of small utility functions I have written over the years. Will receive updates.
/* Table Of Contents:
GeneratorFunction
The GeneratorFunction constructor, exposed.
isSetIterator
Returns true if the object is a bultin Set Iterator.
isMapIterator
Returns true if the object is a builtin Map Iterator.
iterableObject
Make a regular Object an iterable.
get
@Floofies
Floofies / headingScroll.js
Last active March 9, 2018 08:26
Listen for when the URL hash value is changed, and scroll to the first matching Heading element.
// Returns the value of the URL hash parameter, or `null` if the value is empty.
function getHash() {
if (window.location.hash.length < 2) return null;
return window.location.hash.slice(1, window.location.hash.length)
}
const headingSelector = "h1, h2, h3, h4, h5, h6";
// Scrolls to the first Heading element which matches `title`.
function scrollToHeading(title) {
const headings = document.querySelectorAll(headingSelector);
if (headings === null) return;
@Floofies
Floofies / httpd.js
Created March 22, 2018 05:06
Used for clientside testing with Express & NodeJS
const express = require('express');
const app = express();
app.use(express.static("./"));
const server = app.listen(8080, () => console.log("Listening for HTTP on port 8080\n"));