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 / jquery.attributeMatch.js
Created June 12, 2014 11:19
Sometimes Sizzle + CSS level 3's attribute selector logic isn't enough. Sometime you want to be able to do a RegExp search of attribute names, other times you don't know what the attribute name is but have some inkling as to the value. Not recommended for production (this is a performance-intensive anti-pattern) — this was written to help debug …
// Accessible from $ (whole document scope) or on $.fn (search with jQuery scope)
// Usage:
// attributeMatch( name, value, returnAttributes )
//
// Where @name and @value are RegExps or RegExps-as-strings to match against attributes names and values.
// @returnAttributes returns the attributes themselves rather than the owner element - useful to avoid repeating yourself
// eg: $.attributeMatch( /^lang$/, null, true )[ 0 ].value;
//
// To return all elements with ARIA attributes:
// $.attributeMatch( '^aria-\w+' );
@barneycarroll
barneycarroll / select2.accessibility.js
Created June 18, 2014 12:50
Hack some of <select>'s native accessibility behaviour back into select2. Work in progress: can't seem to implement silently without breaking select2 init.
void function accessibilityForSelect2(){
if( ! $.fn.select2 ){
throw new Error( 'You must include Select2 before including its accessibility extensions.' );
return;
}
var oldSelect2 = $.fn.select2;
$.fn.select2 = function select2accessibilityWrapper(){
@barneycarroll
barneycarroll / SassMeister-input.scss
Created June 19, 2014 17:15
Generated by SassMeister.com.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.19)
// ----
@function noop(){}
noop();
@barneycarroll
barneycarroll / grid-items.mixin.scss
Created June 19, 2014 20:47
SASS mixin for creating absolutely positioned, fractional width items. It fulfils the common flexbox use-cases but uses a simpler syntax.
// gridItems is a mixin for creating absolutely positioned,
// fractional width items. It fulfils the common flexbox use-cases
// but uses a simpler syntax.
//
// The only requirement is that the parent be relatively positioned.
// Multiple invocation methods are possible, all of which are
// exemplified at the bottom.
// Occupy full dimensions of parent container.
// The gridItems mixin adds each passed selector to this rule.
@barneycarroll
barneycarroll / Multiline-ellipsis.markdown
Created June 21, 2014 10:34
A Pen by Barney Carroll.

Multiline ellipsis

Using one wrapping element, create multiline ellipses such that a '...' appears when text is overflown.

This particular example shows how it can be used in conjunction with complex effects like zebra-striping by using fixed background images and strict vertical rhythm.

It also implements a system of hidden checkboxes mapped to ellipsis text as labels to enable a 'click to expand' mechanism.

A Pen by Barney Carroll on CodePen.

@barneycarroll
barneycarroll / yieldBack.js
Created July 18, 2014 15:13
Use generators as asynchronous flow controllers (that look synchronous). Pass in a generator. It will be immediately invoked with a `callback` argument that can be inserted into asynchronous yield operations to send the response value back to the yield and resume generator execution.
function yieldBack( generator ){
return generator( function( response ){
generator.send( response.value );
} );
}
module && module.exports = yieldBack;
@barneycarroll
barneycarroll / requireFolder.js
Created August 4, 2014 13:09
Supply a path to return an object with all that path's JS modules exports as properties / methods of the returned object.
var fs = require( 'fs' );
var path = require( 'path' );
module.exports = function( folder ){
var output = {};
fs.readdirSync( folder )
.filter( function isJavascript( filename ){
return '.js' === path.extname( filename );
} )
@barneycarroll
barneycarroll / composer.js
Created August 17, 2014 21:00
Just rediscovered function composition.
function composer(){
var transforms = [].slice.call( arguments );
return function functionWrapper( handler ){
return function transform(){
var ctxt = this;
var args = [].slice.call( arguments );
transforms.forEach( function applyTransform( transform ){
transform.apply( ctxt, args );
@barneycarroll
barneycarroll / eventHandlerTransformer.js
Created August 17, 2014 21:10
I thought this was the most useful thing ever devised for DOM event handling. Then I realised this is just generic a generic function composition tool geared to an over-specified use case.
var _ = require( 'lodash' );
var m = require( 'mithril' );
_.extend( m, {
// Takes any number of transforms, returns a function wrapper:
// When executed, function will be passed through each transform
// sequentially before its own body is executed.
//
// As a DOM event handler:
// onclick: m.transform( m.prevent, m.stop )( x )
@barneycarroll
barneycarroll / mithril.capturingEventHandler.js
Created August 22, 2014 14:37
Event capturing in Mithril.js
// Event capturing for Mithril.js
// Demo here: http://jsfiddle.net/barney/vsw8r3Lh/
m.capture = function capturingEventHandler( eventName, handler ){
function bindCapturingHandler( element ){
element.addEventListener( eventName, handler, true );
}
return function config( element, init ){
if( !init ) bindCapturingHandler( element );
};