Skip to content

Instantly share code, notes, and snippets.

View jochemstoel's full-sized avatar
💭
Dematerializing

Jochem Stoel jochemstoel

💭
Dematerializing
View GitHub Profile
@jochemstoel
jochemstoel / binaryDecisions.js
Created January 18, 2021 21:56 — forked from SethVandebrooke/binaryDecisions.js
Code for making decisions off of binary values
function bd(binaryString) {
return parseInt(binaryString, 2);
}
function db(num) {
return num.toString(2);
}
function rl(binaryString,l) {
var zeros = "";
for (var i = 0; i < l - binaryString.length; i++) {
zeros += "0";
@jochemstoel
jochemstoel / RollingCypher.js
Created January 18, 2021 21:54 — forked from SethVandebrooke/RollingCypher.js
Encrypt and decrypt text with a password and this simple yet effective cypher
function RollingCypher() {
var chars = " qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+-=[]\\{}|;':\",./<>?`~\t\r\n";
chars = chars.concat(chars.concat(chars));
function encrypt(s, p) {
var o = "";
s = s.split('');
p = p.split('');
for (var i = 0; i < s.length; i++) {
o += chars[chars.indexOf(s[i]) + chars.indexOf(p[i % p.length])];
function random(a, b) {
if(Array.isArray(a)){
return a[random(a.length)];
}
if (typeof a === "object") {
var key = random(Object.keys(a));
return b === "key" ? key : b === "both" ? {key:key,value:a[key]} : a[key];
}
if (typeof a === "string" && !!a) {
return (a.toLowerCase() === "bool") ? (Math.floor(Math.random()*2) == 1) : random(a.split(''));
@jochemstoel
jochemstoel / mLearn.js
Created January 18, 2021 21:37 — forked from SethVandebrooke/mLearn.js
Machine learning algorithm that learns underlying patterns in sequences and replicates them using frequency analysis
/**
* Machine learning algorithm that learns underlying patterns in sequences and replicates them using frequency analysis.
* AKA a markov chain XD
* @function
* @param {Array|Number} a A sequence or the length of a sequence to return
* @example
* // feed it a sequence to learn from
* m([1,1,2,3,3,2,1]);
* // have it generate a sequence 7 items long
* m(7);
@jochemstoel
jochemstoel / buildMapWithPath.js
Created January 18, 2021 21:29 — forked from SethVandebrooke/buildMapWithPath.js
Generate a grid with a random path
function buildMapWithPath(H, W, L) {
let A = [], p = [0,0];
for (let Y = 0; Y < H; Y++) {
A[Y] = [];
for (let X = 0; X < W; X++) {
A[Y].push(0);
}
}
function mark(p) {
A[p[0]][p[1]] = 1;
@jochemstoel
jochemstoel / PromptGenerator.js
Created January 18, 2021 21:28 — forked from SethVandebrooke/PromptGenerator.js
Generate a writing prompt
function PromptGenerator() {
var self = this;
function pos(a) {
if (Array.isArray(a)) {
if (!('d' in pos)) pos.d = {};
for (var i = 0, p = null; i < a.length; i++) {
(pos.d[p] ? pos.d[p] : pos.d[p] = []).push(p = a[i]);
}
} else if ("number" == typeof a) {
@jochemstoel
jochemstoel / JSModel.js
Created January 18, 2021 21:25 — forked from SethVandebrooke/JSModel.js
Create models with enforced data types that can reference other models as data types themselves
function Model(name, OBJ) {
function model(obj) {
var PUBLIC = this;
var PRIVATE = {};
PUBLIC.MODEL_TYPE = name;
for (let key in obj) {
let value = obj[key];
// Ensure property is set correctly
@jochemstoel
jochemstoel / UCB64URL.js
Created January 18, 2021 21:23 — forked from SethVandebrooke/UCB64URL.js
Upload and file and convert it into a base64 data url
function uploadAndConvertToBase64DataURL(callback) {
if (!document.getElementById("B64DURL_UL")) {
var input = document.createElement("input");
input.id = "B64DURL_UL";
input.style.display = "none";
input.type = "file";
document.body.appendChild(input);
}
var input = document.getElementById("B64DURL_UL");
input.addEventListener("change", function (e){
@jochemstoel
jochemstoel / randomRythem.js
Created January 18, 2021 21:22 — forked from SethVandebrooke/randomRythem.js
Random Rythem Generator
var generateRandomRythem = (weight) => {
let hit = "x", rythem = "--------".split("");
for (let i = 0; i < ( weight || 4 ); i++) {
rythem[Math.floor(Math.random()*rythem.length-1)] = "x";
}
return rythem.concat(rythem).join("");
}
console.log(generateRandomRythem(6));
@jochemstoel
jochemstoel / gist:eabaa173e18e7c23f27b79ba69b3f428
Created December 21, 2020 13:54 — forked from jfromaniello/gist:8418116
Example of authenticating websockets with JWTs.
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: 8080});
var jwt = require('jsonwebtoken');
/**
The way I like to work with 'ws' is to convert everything to an event if possible.
**/
function toEvent (message) {
try {