Skip to content

Instantly share code, notes, and snippets.

View barneycarroll's full-sized avatar
🛠️
Working on Mithril type stuff

Barney Carroll barneycarroll

🛠️
Working on Mithril type stuff
View GitHub Profile
@barneycarroll
barneycarroll / Later.js
Last active December 16, 2015 08:18
Still beating around the bush of Promises, a simple wrapper for `setTimeout(fn, 0)` (a technique used in DOM scripting to defer execution to the end of the stack) that returns `status` and `cancel` methods. When I thought a bit more about [later.js](https://gist.github.com/barneycarroll/5403872) I realised a bit of extension was almost natural. …
function Later( fn ){
// 0: pending; 1: resolved; 2: cancelled;
var status = 0;
var resolve = setTimeout( function resolve(){
fn();
status = 1;
}, 0);
return {
cancel : function cancel(){
@barneycarroll
barneycarroll / ie.css
Last active December 16, 2015 19:00
Old IE CSS per-property hacks — a reminder
.ie {
display: none;
}
.ie9 {
display: block\9;
display: none/;
}
.ie8 {
@barneycarroll
barneycarroll / jquery.lipo.js
Last active March 23, 2019 23:38
Get rid of redundant whitespace & empty structural elements, in that order
void function semanticContentScope(context, jQuery){
var $ = context.jQuery || jQuery || false;
if(!$){
return false;
}
// Strips elements of duplicate, leading and trailing whitespace and removes empty non-functional elements recursively
function semanticContent(markup){
// Conversion
@barneycarroll
barneycarroll / jquery.reverse.js
Last active December 17, 2015 10:09
$(selection).reverse(fn) applies the passed function to each element in each element in the selection recursively, starting with the bottom of the tree. Useful for cleaning out empty elements recursively (such that an empty div will be destroyed, then the parent it is only child of will be destroyed too, etc).
$.fn.reverse = function(fn){
return this.each(function(el, i){
var $all = $('<x>').append(el).find('*');
fn.call([].reverse.call($all));
});
}
@barneycarroll
barneycarroll / loadAllPosts.bookmarklet.js
Created May 23, 2013 16:49
Read all of HackneyHipsterHate.tumblr.com inline (without having to click through for the posts). Probably works for other tumblrs too.
void function(){[].forEach.call(document.querySelectorAll('.read_more'),function(a){var x=new XMLHttpRequest();x.onload=function(){a.outerHTML=this.responseText.split('<!-- more -->')[1].split('<div class="footer')[0]};x.open('get',a.href,true);x.send()})}();
@barneycarroll
barneycarroll / jquery.blockUI.blockRest.js
Created May 28, 2013 14:38
API extension for Block UI (https://github.com/malsup/blockui) allowing to specify a given element and block every other element. Ideal for modal interactions etc where the rest of the UI is a clearly defined entity. $('#loginForm').blockRest() /* Only #loginForm is interactive */ .on('submit', function(){ $(this).unblockRest(); /* Releases the …
void function blockRest($){
function getRest(target){
var andParent = target.add(target.parents());
return andParent.siblings();
}
$.fn.blockRest = function(){
getRest(this).block();
@barneycarroll
barneycarroll / jquery.fss.js
Last active December 17, 2015 20:59
FFS stands for the Filter / Find Solution. You know how you get stumped when your jQuery find is returning nothing, and yet you know you've got a match, like, right there? Or vice versa? Perform first filter, then find, on each member of the collection. Wrote because of repeatedly stubbing my toes on jQuery Ajax responses.
$.fn.FFS = function FilterFindSolution(){
var a = ['filter','find'];
var p = [].slice.call(arguments);
var y = $();
this.each(function(){
var e = $(this);
var i = -1;
while(i++ < 1) y = y.add(e[a[i]].apply(e, p));
@barneycarroll
barneycarroll / uncomment.demo.js
Last active June 18, 2016 01:10
A refined version of the hack discovered by @tjanczuk: multi-line strings in JS, by way of a hack that relies on JS engines' interpreters to read the multi-line comments stored in noop functions: http://tomasz.janczuk.org/2013/05/multi-line-strings-in-javascript-and.html NB: This code relies on non-spec functionality and as such is only reliable…
var countries = uncomment(function(){
/*
Afghanistan
Albania
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
Armenia
@barneycarroll
barneycarroll / jquery.target.js
Last active December 19, 2015 07:09
NB: This code has all sorts of edge cases, and any code that attempts to leverage it on a holistic framework level is likely to run into bugs fast. Do not use. Creates a custom `target` event, matching the CSS `:target` pseudo-selector, which can be used to determine when an element in the page becomes the current target of the URI's fragment id…
/* Provides a jQuery 'target' event that fires in all conditions that would
* result in an element becoming the target of the URI fragment identifier or
* hash as it is often called. It aims to provide a behavioural hook to emulate
* CSS3's :target selector [1] (more here [2] and here [3]: good demos include
* this proof of concept [4] and Wikipedia's styling of targeted footnotes and
* citations [5]).
*
* [1] https://developer.mozilla.org/en-US/docs/Web/CSS/:target
* [2] http://css-tricks.com/on-target/
* [3] http://blog.teamtreehouse.com/stay-on-target
@barneycarroll
barneycarroll / contains.js
Created July 23, 2013 11:39
Does the string (argument 1) contain a given substring (argument 2)?
function contains(string, substring){
return string.indexOf(substring) > -1;
}