Skip to content

Instantly share code, notes, and snippets.

View dana-ross's full-sized avatar

Dana Ross dana-ross

View GitHub Profile
function getFontSize(n) {
return parseInt(window.getComputedStyle(n, null).getPropertyValue('font-size'))
}
function renderNode(n) {
let output = [n.tagName]
n.id && output.push(`id="${n.id}"`)
n.className && output.push(`class="${n.className}"`)
return output.join(' ')
}
@dana-ross
dana-ross / walkthedom.js
Created May 18, 2020 14:06
Find nodes with > 60 children
function x(n) {
if(n.children.length > 60) {
console.log(n)
}
Array.from(n.children).forEach(x)
}
x(document.getElementsByTagName('html')[0])
@dana-ross
dana-ross / www.conf
Last active May 31, 2018 18:00
Modified www.conf for wp-local-docker fixing Windows 10 random "not found" issue. See https://github.com/docker/for-win/issues/2005
; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]
; Per pool prefix
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
@dana-ross
dana-ross / dmrflatten.js
Last active March 23, 2018 15:02
The world needed another JavasScript array flattening function
/**
* Flatten a JavaScript array
* @param {Array} arr Array to flatten
* @param {number} depth Levels deep to flatten
* @return Array
*/
export default function flatten(arr, depth = 1) {
if(depth === 0) return arr
let newArr = []
arr.forEach((x) => {
@dana-ross
dana-ross / disable-rest-endpoints.php
Created February 27, 2018 16:58 — forked from chuckreynolds/disable-rest-endpoints.php
WordPress: Disable WP REST API JSON endpoints if user not logged in
<?php
/*
* Disable WP REST API JSON endpoints if user not logged in
*/
function chuck_disable_rest_endpoints( $access ) {
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'disable-json-api' ), array( 'status' => rest_authorization_required_code() ) );
}
return $access;
}
@dana-ross
dana-ross / functions.php
Created July 13, 2017 21:29
Global-less WordPress Templating
<?php
/**
* WordPress template renderer that avoids using global variables
* @param string Name of a template to load
* @param array $args Template variables
* @return string|WP_Error rendered template or an error
* NOTE: Untested
*/
function render_template( $template_name, array $args ) {
@dana-ross
dana-ross / reading_time.php
Created June 14, 2016 19:18
Reading time calculation from one of my WordPress plugins. MIT licensed. Use as you see fit.
<?php
/**
* Calculate the reading time for a piece of content
*
* @param string $content
* @param integer $wpm Words Per Minute reading speed
*
* @return array minutes => integer, seconds => integer
*/
<?php
use DaveRoss\FunctionalProgrammingUtils\Left as Left;
use DaveRoss\FunctionalProgrammingUtils\Right as Right;
/**
* @param string $json A JSON string
* @return Either
*/
function parse_json_returning_either( $json ) {
<?php
use DaveRoss\FunctionalProgrammingUtils\Maybe as Maybe;
$good = Maybe::of( json_decode( '{ "label": "This is valid JSON", "value": 5 }' ) );
$bad = Maybe::of( json_decode( '{ "label": This is invalid JSON, "value": 5 }' ) );
$good->map( function( $obj ) { echo $obj->value . "\n"; } );
$bad->map( function( $obj ) { echo $obj->value . "\n"; } );
<?php
$good = json_decode('{ "label": "This is valid JSON", "value": 5 }' );
$bad = json_decode('{ "label": This is invalid JSON, "value": 5 }' );
if( is_null( $good ) ) {
echo "Couldn't parse good JSON\n";
}
else {
echo $good->value . "\n";