Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Spuffynism / sequence.js
Created July 1, 2018 04:06
Recursive sequence emulating functional languages' "freeze" functions
function* sequence(x) {
yield x;
yield * sequence(x + 1);
}
@Spuffynism
Spuffynism / squeeze.js
Last active July 8, 2018 00:05
Squeeze utf-8 characters in half their space
let squeeze = s => s.replace(/../g,c=>String.fromCharCode(c[a='charCodeAt']()|c[a](1)<<7));
let unsqueeze = s => s.replace(/./g,c=>String.fromCharCode((c=c.charCodeAt())&127,c>>7));
console.log(unsqueeze("㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ") ===
"for(c.width|=i=9;i--;)x.fillRect(400+i*100+S(t)*300,400,50,200);");
console.log(squeeze("for(c.width|=i=9;i--;)x.fillRect(400+i*100+S(t)*300,400,50,200);") ===
"㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ");
console.log("㟦ᑲᝣ㓷㩤㹨㒽Ჽ㒻ᚭᒻ᝸㓦㙬㋒㩣ᨨᠰ㒫ᢪᠰ⦫㨨ᔩᠳᘰᠴᘰᠵ᤬ᠰᶩ".length === 32);
/*
Variations of https://medium.com/@d0nut/why-building-a-sandbox-in-pure-javascript-is-a-fools-errand-d425b77b2899
jailbreaking example.
*/
var flag = "I'm the flag!";
function jail(code) {
// quick string escape for inner strings
code = code.replace(/["'`\\]/g, function(v){ return `\\${v}`});
@Spuffynism
Spuffynism / sort_spotify_tracks_by_listening_count.js
Last active August 24, 2022 06:10
Takes a streaming history spotify file and sorts the tracks by listening count
let fs = require('fs');
let path = process.cwd();
console.log('looking for StreamingHistory.json file in ' + path + '\\data\\');
let buffer = {};
try {
buffer = fs.readFileSync(path + "\\data\\StreamingHistory.json");
} catch (e) {
@Spuffynism
Spuffynism / delete_vhost.sh
Created May 21, 2018 22:52
Deletes a virtual host
if [[ $# -eq 0 ]] ; then
echo 'You must specify a valid virtual host name'
exit 0
fi
vhost=$1
export vhost
sudo rm -rf /var/www/"$vhost"
sudo a2dissite "$vhost".conf
@Spuffynism
Spuffynism / create_vhost.sh
Created May 21, 2018 22:48
Creates a virtual host
if [[ $# -eq 0 ]] ; then
echo 'You must specify a valid virtual host name'
exit 0
fi
vhost=$1
export vhost
sudo mkdir -p /var/www/"$vhost"/public_html
sudo chmod -R 777 /var/www/"$vhost"
@Spuffynism
Spuffynism / vector.js
Created April 21, 2018 16:51
Simple vector implementation in js to help with amortized analysis
function Vector() {
this.capacity = 1;
this.elements = new Array(this.capacity);
this.size = 0;
this.add = (element) => {
if (this.size > this.capacity)
console.err('this.size > this.capacity');
if (this.size === this.capacity) {
@Spuffynism
Spuffynism / dummy-web-server.py
Last active July 4, 2018 22:31 — forked from bradmontgomery/dummy-web-server.py
A minimal http server in python. Responds to GET and POST requests
#!/usr/bin/env python
"""
Very simple HTTP server.
Upon a GET or POST request, returns a json object with the route of the request
and the data of the request body.
"""
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
@Spuffynism
Spuffynism / heap.js
Created February 19, 2018 16:39
heap
let parent = (t, i) => t[((i + 1)>>1) - 1];
let kids = (t, i) => [t[2*i + 1],t[(i+1)*2]]
@Spuffynism
Spuffynism / binary-tree-roadtrip.ml
Last active February 9, 2018 03:39
binary tree visiting algorithms
type 'a binary_tree = Leaf | Node of 'a * 'a binary_tree * 'a binary_tree;;
let visit_binary_tree tree (visiting_function:'a binary_tree -> 'a list) :'a list = match tree with
| Leaf -> []
| Node (_, _, _) -> visiting_function tree;;
(*
1
/ \
2 3