Skip to content

Instantly share code, notes, and snippets.

View fritzy's full-sized avatar
💽
Please Insert Disk III

Nathan Fritz fritzy

💽
Please Insert Disk III
  • Kennewick, WA
  • 15:08 (UTC -07:00)
  • X @fritzy
View GitHub Profile
@fritzy
fritzy / video_to_canvas.js
Created July 11, 2013 16:47
Want to draw a video onto a canvas? Here's how simple it is.
function video2Canvas(video, canvas) {
var ctx = canvas.getContext('2d');
function draw() {
ctx.drawImage(video, 0, 0, video.width, video.height, 0, 0, canvas.width, canvas.height);
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
}
@fritzy
fritzy / pushmaxlist.lua
Last active April 19, 2017 21:07
Appending to a Redis List, deleting entries to the left past a maximum size.
--EVAL "this script" 1 key_name new_item max_size
local key = KEYS[1];
local item, max = unpack(ARGV);
redis.call('RPUSH', key, item);
redis.call('LTRIM', key, -max, -1);
@fritzy
fritzy / levelreduce.js
Created November 5, 2013 17:29
A Map-Reduce implementation for LevelUp. Maps do not necessarily return database key/value pairs, and may return any number of key/value pairs. For example, it could return word-counts or any derived pairs. Or you could simple return a single [{dbkey: value}] for every key that matched a pattern. Maps could also convert formats. Reduce is then c…
/* mapReduce
* arguments (
* lup: levelup instance,
* map: function ({key, value}) { return [{key: mapkey, value: mapvalue}, ...]; },
* reduce: function (mapkey, [mapvalue, ...]) { return reduced; },
* callback: function (err, {mapkey: reduced, ...})
* )
*
* A map is called for each key pair. A map returns an array of key value pairs to be reduced.
* Key value pairs may be derived keys and values, or database keys and values.
@fritzy
fritzy / getmsgpackasjson.lua
Created November 6, 2013 17:35
Redis scripts to set JSON in, store as MSGPack, and get JSON out.
--EVAL 'this script' 1 some-key
local key = KEYS[1];
local value = redis.call('GET', key);
local jvalue = cjson.encode(cmsgpack.unpack(value));
return jvalue;
@fritzy
fritzy / checkpassword.lua
Created November 7, 2013 20:14
Set password as sha1 hash and check password against hash.
--EVAL "this script" 1 key userid password
local key = KEYS[1];
local userid, pass = unpack(ARGV);
local hash = redis.sha1hex('SOME-STATIC-SALT'..string.sub(pass, 1, 2)..userid..string.sub(pass, 3));
--no error, does hash match?
return {false, (redis.call('GET', key) == hash)}
@fritzy
fritzy / collate.lua
Last active April 19, 2017 20:55
Collate a Hash in Redis
--EVAL "this script" 1 some-hash-key
local key = KEYS[1];
local keyvalues = redis.call('HGETALL', key);
local result = {};
--every other value is a key
for idx = 1, #keyvalues, 2 do
result[keyvalues[idx]] = keyvalues[idx + 1]
end
@fritzy
fritzy / updatesendnotice.lua
Last active April 19, 2017 21:10
Update a key, send out a notice, and log the notice.
--2 key changelog update millisecond-epoch
local key, changelog = unpack(KEYS);
local update, epoch = unpack(ARGV);
local result = redis.call('SET', key, update);
local notice = cjson.encode({key = key, value = update, time = epoch});
redis.call('ZADD', changelog, epoch, notice);
redis.call('ZREMRANGEBYSCORE', changelog, '-inf', epoch - 86400000);
#!/usr/bin/env node
// ./generatelookup -l lookupkey -p somekey* -a attribute
var redis = require('redis').createClient();
var async = require('async');
var opt = require('optimist');
opt.usage('$0 -l lookup -p pattern -a attribute');
opt.demand(['l', 'p', 'a']);
var argv = opt.argv;
var iter = '0';
@fritzy
fritzy / updatekeyandlookup.lua
Created November 13, 2013 23:12
Update a key and lookup for a specific root level JSON attribute.
--EVAL "this script" 2 key lookup jsonvalue attribute
local key, lookup = unpack(KEYS);
local jsonvalue, attribute = unpack(ARGV);
local value = cjson.decode(jsonvalue);
local oldvalue = redis.call('GET', key);
--if this key was set before, remove the old lookup
if type(oldvalue) == 'string' then
oldvalue = cjson.decode(oldvalue);
@fritzy
fritzy / spinner.js
Last active July 18, 2022 11:03
Use Mutation Observers to place image placeholders (spinners) on loading images within a target element.
//whenever uncached images are added to the dom tree within target,
//replace them with a spinner gif until they're loaded
function replaceImages(target, spinner_src) {
var spinner, observer;
//detect support
if (window.MutationObserver) {
//preload spinner (probably a gif)
//if it's not ready for first use, oh well
spinner = new Image();