Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
quidmonkey / numberMap.js
Created August 9, 2023 12:52
Map a Number from One Value System to Another
Number.prototype.map = (istart, istop, ostart, ostop) => {
return ostart + (ostop - ostart) * ((this - istart) / (istop - istart));
};
const map = (value, istart, istop, ostart, ostop) => {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
};
@quidmonkey
quidmonkey / async-loops.js
Created January 10, 2019 17:00
Async Loop Example
const sleep = async ms => new Promise(
resolve => setTimeout(() => {
console.log('~~~ Slept for', ms, 'ms')
resolve()
}, ms)
)
const of = async range => {
for (const ms of range) {
await sleep(ms)
@quidmonkey
quidmonkey / reduce.js
Last active March 16, 2018 00:43
Various JavaScript Implementations of Reduce
// utils
// unit test - assert 2 values are equal
const assert = (assertion, actual, expected) => {
console.log('~~~', assertion, ':', actual === expected);
};
// clone a value
// objects are only shallow cloned
const clone = (val) => {
@quidmonkey
quidmonkey / watch.sh
Created June 23, 2017 20:02
Sample NPM Run Script File for Watching
#!/bin/bash
function ctrl_c() {
for process in "${PROCESSES[@]}"; do
local pid=$(get_pid $process)
if [ ! -z $pid ]; then
kill $pid
fi
done
@quidmonkey
quidmonkey / fsm.js
Last active June 5, 2017 17:30
Simple FSM Based on Redux.js Style of State Management - Features Timewarping
import { merge, mergeWith } from 'lodash-es';
const history = []; // history of all states
let timePeriod = 0; // index for history, used to timewarp throughout the history
let state = Object.freeze({}); // current state
/**
* Recursively merges all objects, and overwrites
* all other values.
* @param {*} destObj Current state
@quidmonkey
quidmonkey / foo.js
Created February 5, 2016 18:20
How Do I Mock the DOM with Ava?
import jQuery from 'jquery';
export const attachEvents = someStr => {
jQuery(document).ready(e => {
// do something cool with someStr
});
};
@quidmonkey
quidmonkey / anonymous-es5.js
Last active January 12, 2016 21:54
JavaScript Anonymous Function Call Stack Test in ES5 & ES6
// anonymous function call stack test
// tested and works in chrome, ff, safari, node 4.2.2
// does not work in ie9 or ie11
var f = function f() { g(); };
var g = function g() { h(); };
var h = function h() { console.log(x) };
try {
f();
} catch (e) {
@quidmonkey
quidmonkey / uninstall_node.sh
Created January 7, 2016 16:55
Uninstall Node
# First:
lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom | while read f; do sudo rm /usr/local/${f}; done
sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
# To recap, the best way (I've found) to completely uninstall node + npm is to do the following:
#go to /usr/local/lib and delete any node and node_modules
cd /usr/local/lib
sudo rm -rf node*
@quidmonkey
quidmonkey / bower-components.js
Last active August 29, 2015 14:16
Get All Bower Components in Node
'use strict';
var fs = require('fs');
var globule = require('globule');
var path = require('path');
var bowerComponents = globule
.find('bower_components/**/bower.json')
.map(function (fileName) {
var bowerJson = fs.readFileSync(fileName);
@quidmonkey
quidmonkey / index.html
Last active March 22, 2024 19:05
Three.js Mandelbrot Demo
<!DOCTYPE html>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
</head>
<body>
<script id="mandelbrot-vertex" type="x-shader/x-vertex">
precision highp float;
uniform float zoom;
varying vec2 pos;