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 / 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 / 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 / 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 / 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 / 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 / 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 / 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"