Skip to content

Instantly share code, notes, and snippets.

@cjkoepke
cjkoepke / functions.php
Last active February 22, 2016 05:05
This is what functional code looks like that still equals crap.
<?php
add_action('wp_enqueue_scripts','genesis_sample_google_fonts' );
function genesis_sample_google_fonts() {
wp_enqueue_style( 'google-fonts','//fonts.googleapis.com/css?family=Lato:300,400,700', array(), CHILD_THEME_VERSION ); }
add_theme_support( 'html5', array(
'search-form', //search
'comment-form', 'comment-list' //comments
)
@cjkoepke
cjkoepke / fetch.js
Last active July 26, 2017 03:50
A simple way to add headers to a fetch call.
const myHeaders = new Headers();
myHeaders.append('Content-Type', 'document');
fetch('url-here', {
method: 'GET',
headers: myHeaders
}).then(res => {
// Do something.
});
@cjkoepke
cjkoepke / example.md
Created August 10, 2017 17:17
Terse is Better Than Verbose

Terse is Better Than Verbose

Below are some examples on how the best practice of writing "readable" code is not always synonymous with writing it verbosely. For example, in scenarios where your code is complicated, being verbose might actually be harder to scan and quickly see the purpose without a bunch of decorative wrappers.

See below for more explanation.

@cjkoepke
cjkoepke / logging_errors.js
Created August 10, 2017 18:20
Debugging tips and tricks.
// Great for getting values, outputting context strings, and so on.
console.log(variable)
// Same as console.log(), but prints in yellow as a warning. Useful to show a non-breaking error in a dev env.
console.warn('It is a good idea to use .map instead of a for-loop.')
// Same as console.log(), but prints in red as an error. Useful to show a breaking error in a dev env.
console.error(`The value must be an integer. You provided a ${typeof variable}.`)
// Group console data together for better viewing. Nice to group console logs together.
@cjkoepke
cjkoepke / loadDependencies.js
Created January 11, 2018 17:52
Load script/style dependencies asynchronously using Promises.
// Use the function like so:
_loadDependecies([
{tag: 'script', url: 'example.com/asset.js'},
{tag: 'script', url: 'example.com/asset2.js'},
{tag: 'link', url: 'example.com/asset.css'}
]).then(() => {
// Do something.
});
// Function to return a promise catch-all.
shouldShow = groups => {
const field = this.props.field
let show = true
if ( field.conditional_logic ) {
field.conditional_logic.forEach( conditions => {
conditions.forEach( condition => {
const { field } = fieldLookup( groups, condition.field )
if ( field ) {
if ( eval( `"${field.value}" !${condition.operator} "${condition.value}"` ) ) {
show = false