Skip to content

Instantly share code, notes, and snippets.

View aduth's full-sized avatar

Andrew Duthie aduth

View GitHub Profile
@aduth
aduth / objectkeys.txt
Last active December 12, 2015 03:39
Object.keys shim (non-spec-compliant): JavaScript vs CoffeeScript
// JavaScript
if (Object.keys == null) {
Object.keys = function(obj) {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
# Before
get: (fileName, complete) ->
throw new Error("File name `#{fileName}` has not been added yet") unless fileName of @ipsums
if @cached[fileName].length
# Pull from cache if available
complete null, @cached[fileName].shift()
else
# Else, generate fresh
complete null, @getFresh(fileName)
@aduth
aduth / gist:5543247
Created May 8, 2013 20:08
Convert CSS3 transition duration string to milliseconds
var transitionDurationToMilliseconds = function(duration) {
var pieces = duration.match(/^([\d\.]+)(\w+)$/),
time, unit, multiplier;
if (pieces.length <= 1) {
return duration;
}
time = pieces[1];
unit = pieces[2];
@aduth
aduth / gforms_placeholders.php
Created October 25, 2013 20:13
Gravity Forms placeholders using a combination of default value and "gforms-placeholder" class
function enable_gforms_placeholders($content, $field, $value, $lead_id, $form_id) {
if (strpos($field['cssClass'], 'gforms-placeholder') !== false) {
switch ($field['type']) {
case 'text':
case 'phone':
case 'website':
case 'number':
case 'name':
case 'address':
case 'email':
@aduth
aduth / find-warnings-to-upgrade.js
Last active November 1, 2016 15:19
Finds ESLint rules which are declared as warnings but for which there are no issues, to be safely upgraded to error
/* eslint-disable no-console */
/**
* External dependencies
*/
const execSync = require( 'child_process' ).execSync;
const reduce = require( 'lodash/reduce' );
/**
* Internal dependencies
@aduth
aduth / state-effects.js
Created December 15, 2016 14:46
redux effects middleware
// state/effects.js
/**
* Internal dependencies
*/
import post from './post/effects';
export default [
post
];
const parse, { query, text, attr } = wp.blocks.parse;
const quote = parse( '<blockquote><p>...</p><p>...</p><cite>Andrew</cite></blockquote>', {
text: query( 'p', text() ),
cite: text( 'cite' )
} );
// { text: [ "...", "..." ], cite: "Andrew" }
const image = parse( '<figure><img src="img.png" alt="Image"><figcaption>An Image</figcaption></figure>', {
const { parse, registerBlock } = wp.blocks;
const { attr, html } = parse;
registerBlock( 'wp/image', {
schema: {
src: attr( 'img', 'src' ),
alt: attr( 'img', 'alt' ),
caption: html( 'figcaption' )
},

Quote:

<Element name="blockquote">
	<OneOrMore>
		<Element name="p">
			<Text as="value" />
		</Element>
	</OneOrMore>
	<Optional>
// Action creators
function savePost( post ) {
return {
type: 'SAVE_POST',
post
};
}
function receivePost( post ) {