Skip to content

Instantly share code, notes, and snippets.

View AaronHarris's full-sized avatar

Aaron Harris AaronHarris

  • University of Southern California
  • Seattle, WA
View GitHub Profile
@AaronHarris
AaronHarris / gist:9677388
Created March 21, 2014 00:58
Compare two directories
#!/bin/bash
shopt -s dotglob
for file in "$1"/*; do [[ -f "$file" ]] && d1+=( "$(md5sum < "$file")" ); done
for file in "$1"/*; do [[ -f "$file" ]] && d1+=( "$(md5sum < "$file")" ); done
[[ "$(sort <<< "${d1[*]}")" == "$(sort <<< "${d2[*]}")" ]] && echo "Same" || echo "Different"
@AaronHarris
AaronHarris / Fullscreen-drag-slider-with-parallax.markdown
Created March 21, 2015 07:58
Fullscreen drag-slider with parallax

Fullscreen drag-slider with parallax

Sort of responsive. Tested in Chrome/FF/last IE, everywhere looks fine.

Source of inspiration - https://stupid-studio.com/ (their js is minified, so all code is mine, except some parts of svg, like viewBox/minHeight).

A Pen by Nikolay Talanov on CodePen.

License.

@AaronHarris
AaronHarris / props.js
Last active May 3, 2017 19:42
Gets all the properties you can call on an arbitrary JavaScript object and its prototype chain
function logAllProperties(obj) {
if (obj == null) return; // recursive approach
console.log(Object.getOwnPropertyNames(obj));
logAllProperties(Object.getPrototypeOf(obj));
}
// logAllProperties(my_object);
// Using this, you can also write a function that returns you an array of all the property names:
function props(obj) {
var p = [];
@AaronHarris
AaronHarris / puzzle-solve.js
Last active August 23, 2017 01:27
Includes scripts I've written to help solve puzzles during puzzle competitions
// This was an around-the-world-in-80-days type problem where we were given places and had to filli n the blanks based on clues
var cities = ["Allahabad", "Battleship", "Brindisi", "Cadet", "Calcutta", "Charcoal", "Dove",
"Gunmetal", "Lampblack", "Omaha", "Paris", "Paynes", "Pewter",
"Slate", "Spanish", "Suez", "Whitewash", "Yokohama"].map(s => s.toLowerCase());
@AaronHarris
AaronHarris / promise-retry.js
Created August 23, 2017 01:14
Make a promise retryable a certain number of times without balooning memory
function retry(gen /*: Function<Promise> */, try = 2) {
if (try < 1) return gen(); // will not retry
return gen().catch(retry.bind(retry, gen, i - 1))
}
// or
function retry(gen/*: Function<Promise> */, try = 2) {
function again(i) {
if (i < 1) return gen();
@AaronHarris
AaronHarris / TimeCache.js
Created August 23, 2017 18:22
A TimeCache is a subclass of Map where keys become invalidated after a certain amount of time
DEFAULT_CACHE_TIME_MS = 262144; // ~5 minutes (closest 1-bin value)
export default class TimeCache extends Map {
constructor(cacheTime, seed) {
super(seed);
this.cacheTime = cacheTime || DEFAULT_CACHE_TIME_MS;
}
set(key, value, cacheTime) {
super.set(key, {
@AaronHarris
AaronHarris / intersperse.js
Last active August 23, 2017 20:37
Testing different possible implementations for lodash to add an intersperse method in https://github.com/lodash/lodash/issues/2339
```
_.intersperse(["a", "b", "c"], "+");
// ["a", "+", "b", "+", "c"]
intersperse = function intersperse(arr, sep) {
var i = arr.length,
separr;
@AaronHarris
AaronHarris / dummy.js
Last active September 27, 2017 09:54
JavaScript Stump - the Ultimate Singleton: This describes a function object that no matter whether you call it, instantiate it, or access or modify a property, it always returns itself.
var proxy, handler;
handler = {
has(/*target, prop*/) {
return false;
},
get(target, prop, receiver) {
if (prop in target) return target[prop];
return receiver;
},
set(/*target, prop, value, receiver*/) {
@AaronHarris
AaronHarris / truncate-json.js
Last active January 7, 2019 23:21
A module that shortens JSON to a number of bytes while keeping syntactically valid JSON. Useful for when apis can only accept a certain byte length for input. Only works for NODE but can be ported to browsers. WIP.
function getBytes(string) {
return Buffer.byteLength(String(string), 'utf8');
}
function trimToBytes(string, diff) {
return Buffer.alloc(diff, string, 'utf8').toString();
}
// This is only intended atm for flat objects with string values
// Does not handle edge cases
@AaronHarris
AaronHarris / bootstrap-4-starter-template-using-cdn.markdown
Created January 24, 2018 21:46
Bootstrap 4: Starter template (using CDN)