Skip to content

Instantly share code, notes, and snippets.

View chrisdc's full-sized avatar

Christopher Crouch chrisdc

View GitHub Profile
@chrisdc
chrisdc / search.js
Last active October 11, 2018 10:05
Binary Search
function search(key, data) {
var min = 0;
var max = data.length - 1;
var half;
while (max >= min) {
half = Math.floor((max + min)/2);
if (data[half] === key) {
return half;
@chrisdc
chrisdc / stemmer.js
Last active October 8, 2018 12:35
A compact implementation of the Porter 2 stemming algorithm in Javascript
'use strict';
function handleSuffix(word, patterns) {
var wordLen = word.length;
for (var i = 0, len = patterns.length; i < len; i++) {
if (patterns[i][0].test(word)) {
if (wordLen - patterns[i][3] < patterns[i][2]) {
// The given suffix does not fit in the required region.
return word;
@chrisdc
chrisdc / log-message.js
Created September 24, 2018 10:31
Mocking globals in Jest.
function logMessage(message) {
console.log(message);
}
module.exports = logMessage
@chrisdc
chrisdc / Vagrantfile
Created March 1, 2016 00:06
Vagrant Setup
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
@chrisdc
chrisdc / gist:e99763a1d9da1aaf99a1
Last active August 29, 2015 14:23
Bootstrap Template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Template</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
@chrisdc
chrisdc / .jscsrc
Last active November 2, 2018 19:48
Options file for JSCS
{
"disallowKeywordsOnNewLine": ["else"],
"disallowMixedSpacesAndTabs": "smart",
"disallowNewlineBeforeBlockStatements": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowTrailingWhitespace": true,
"requireBlocksOnNewline": true,
@chrisdc
chrisdc / events.js
Last active August 29, 2015 14:08
Publish/subscribe module
/**
* A simple Publish/subscribe javascript module based on code by David Walsh:
* http://davidwalsh.name/pubsub-javascript
*/
var events = (function(){
// Holds the subscribed topics.
var topics = {};
return {
subscribe: function( topic, callback ) {
@chrisdc
chrisdc / navigation.js
Last active November 2, 2018 19:49
Object Orientated navigation.js
( function() {
function Menu( containerId, options ) {
var container,
menu,
button;
function init() {
container = document.getElementById( containerId );
if ( ! container ) {
@chrisdc
chrisdc / term-meta.php
Last active July 7, 2021 17:57
Add a custom meta box to the new/edit category pages. The meta data is saved to the array term_meta[], which can handle further fields in the future. Based on: https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/
<?php
/**
* Add a custom meta box to the new/edit category pages.
* The meta data is saved to the array term_meta[], which can handle further
* fields in the future.
*
* Based on: https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/
*/
/**
@chrisdc
chrisdc / plugin.php
Created July 31, 2014 12:51
Wordpress Plugin Template
<?php
/**
* Plugin Name: My Plugin
* Plugin URI: https://example.com/plugin/
* Description: An amazing WordPress plugin.
* Version: 1.0
* Author: [Author Name]
* Author URI: https://example.com/author/
* License: GPL2
* Text Domain: myplugin