Skip to content

Instantly share code, notes, and snippets.

View jdelight's full-sized avatar

James Da Costa jdelight

View GitHub Profile
@jdelight
jdelight / findImage.js
Created February 24, 2016 00:38
Given an array of image URLs, returns a promise resolving with the first URL to load successfully
var loadImage = (src) => {
return new Promise(function(resolve, reject) {
var proxyImage = new Image();
proxyImage.onload = function(){
console.log('resolving', src);
resolve(src);
};
proxyImage.onerror = function(){
@jdelight
jdelight / fake-observable.js
Last active September 30, 2015 11:55
Fake observable based on rxJS API.
/*
Takes an array of items with each "," representing 100ms.
Subscribers can map or filter over the Observable.
Only when forEach is called do they start
getting data (with the transformations applied) over time.
Usage:
var subscriber = FakeObservable([10, , , 3, , , 8, , , , , 2, 18, 9]).subscribe();
subscriber.filter(function (item) {
@jdelight
jdelight / get_date_range.js
Created September 15, 2015 09:22
ES6 Destructuring assignments with Objects & Arrays
// { days } creates the variable days from the default object {days: 7}
function get_date_range({ days } = {days: 7}) {
let end = Date.now();
let period = days * 24 * 60 * 60 * 1000;
let start = end - period;
let start_date = new Date(start).toISOString(); // returns the date in ISO8601 format in UTC 0
let end_date = new Date(end).toISOString();
console.log(start_date, '-', end_date);
return [start_date, end_date];
@jdelight
jdelight / object_class.js
Last active September 8, 2015 13:27
// Determining object [[Class]] with toString.call([value])
var o = {length: 12};
var a = [];
var f = function(){};
var b = true;
var r = /^abc/;
var n = 0;
var u = void 0;
var l = null;
console.log(toString.call(o)); // [object Object]
@jdelight
jdelight / gist:f151d570d58051d22519
Last active August 29, 2015 14:24
Prefix your main function with another one
function prefix(initFn, mainFn) {
return function(){
var func = initFn;
initFn = mainFn;
return func.apply(this, arguments);
}
}
function add(a, b){
return a + b;
@jdelight
jdelight / url.js
Last active September 8, 2015 13:09
Construct a URL path, replacing ":placeholder" with values
define([
'src/urls'
], function(
urls
) {
'use strict';
var slice = Array.prototype.slice;
return function url(name) {
@jdelight
jdelight / collate.js
Created January 5, 2015 17:59
Simple themes with sass 3.3 (works with Fabricator 0.7 but requires changing collate.js to include current index)
/**
* Block iteration
* @description Repeat a block a given amount of times.
* @example
* {{#iterate 20}}
* <li>List Item {{@index}}</li>
* {{/iterate}}
*/
Handlebars.registerHelper('iterate', function (n, block) {
var accum = '', data;
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
.window {
height: 105px;
overflow-y: scroll;
@jdelight
jdelight / _.md
Created January 16, 2014 21:50
abpp