Skip to content

Instantly share code, notes, and snippets.

@kendru
kendru / keep-in-mind.md
Last active August 29, 2015 14:01
Keep in Mind

Keep in mind...

(Remind myself of the important points to consider when writing)

  1. Be sure you can answer these questions:
    • What problem am I trying to solve?
    • Why should you listen to me?
    • Who cares?
  2. Be as concise as possible. You are competing with 1,000 other things for your reader's time
  3. Remember to give examples whenever applicable
@kendru
kendru / generic_accessors.php
Created June 26, 2012 14:27
Get and set generic properties that are not known until runtime
<?php
class AccessibleClass {
protected $data;
public function __construct() {
$this->data = array();
}
public function __call( $method, $args ) {
if( 0 === ( $start = strpos($method, 'get') ) ) { // If the method is a getter
@kendru
kendru / combineLinearArray.php
Created June 29, 2012 15:46
Combine linear array into associative array in PHP
<?php
function combineLinearArray( $arrayToSmush, $evenItemIsKey = true ) {
if ( ( count($arrayToSmush) % 2 ) !== 0 ) {
throw new Exception( "This array cannot be combined because it has an odd number of values" );
}
$evens = $odds = array();
// Separate even and odd values
for ($i = 0, $c = count($arrayToSmush); $i < $c; $i += 2) {

Keybase proof

I hereby claim:

  • I am kendru on github.
  • I am andrew_meredith (https://keybase.io/andrew_meredith) on keybase.
  • I have a public key whose fingerprint is 8E6E 5FA4 B7D4 52CF FAE6 5242 8A8F 18D4 2D6E 2123

To claim this, I am signing this object:

@kendru
kendru / index-mangling.js
Last active August 17, 2016 14:26
Demonstrate re-indexing already-indexed data
const source = {
data: {
123: {
0: 15,
1: 1
},
456: {
0: 53,
1: 4
}
@kendru
kendru / handle-sighup.js
Created October 10, 2016 22:06
Example of using SUGHUP with Node to reload config
// Example /tmp/srv.conf.json
// {"name":"bill"}
var fs = require('fs');
var config = {
name: ''
};
function loadConfig() {
@kendru
kendru / simpleslider.jquery.js
Created October 12, 2012 06:00
Simple jQuery image slider
// Image slider, complete with lazy loading.
(function ($) {
$.fn.sliderize = function( options ) {
var settings = $.extend({
srcAttrib: "src", // data attribute containing image source
delayTime: 6000,
transitionTime: 1000,
randomize: false, // "randomize" the slides
width: 700,
height: 276
@kendru
kendru / kubernetes-cheatsheet.md
Last active August 8, 2017 05:22
Links and descriptions for tasks that I commonly perform on Kubernetes
@kendru
kendru / findNode.js
Last active October 31, 2017 22:42
Find a node matching a predicate in a generic JS tree
function findNode(node, pred) {
if (pred(node)) {
return node;
}
if (typeof node == 'object' && node.constructor == Object) {
for (var prop in node) {
if (node.hasOwnProperty(prop)) {
let foundNode = findNode(node[prop], pred);
@kendru
kendru / trie.js
Created September 5, 2017 19:27
Javascript Trie for efficient prefix search
class Node {
constructor(char, parent = null, weight = null) {
this.char = char;
this.parent = parent;
this.weight = weight;
this.children = {};
}
get isTerminal() {
return this.weight !== null;