Skip to content

Instantly share code, notes, and snippets.

@chrisdavies
chrisdavies / stripe-currencies.js
Created November 7, 2016 14:53
A handy list of currency codes and descriptions for use with Stripe.
'use strict';
// The STRIPE-supported currencies, sorted by code
export const currencies = [
{
'code':'AED',
'description':'United Arab Emirates Dirham'
},
{
'code':'AFN',
@chrisdavies
chrisdavies / gist:735daddd50510ed9d5b0
Created July 19, 2014 08:55
C# underscore string extension
// Convert a string to Underscore
public static class StringEx
{
public static string Underscored(this string s)
{
var builder = new StringBuilder();
for (var i = 0; i < s.Length; ++i)
{
if (ShouldUnderscore(i, s))
@chrisdavies
chrisdavies / gist:9d9c0aff6229ef1d3eef
Created October 28, 2014 14:08
Rust - lifetime constraint and generic constraint on a struct
use std::io::Writer;
// This specifies lifetime constraint of 'a
// Type W must implement the Writer trait
// Type W has the lifetime 'a (the same as the related struct instance)
pub struct Foo<'a, W: 'a + Writer> {
writer: &'a mut W
}
@chrisdavies
chrisdavies / toggle-checkbox.html
Created December 3, 2014 02:25
A CSS-only (no JS) checkbox toggle control. Might make the label text (yes/no) markkup rather than CSS injected content.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Toggle Example</title>
<style>
body {
font-family: sans-serif;
}
@chrisdavies
chrisdavies / autosize.md
Last active April 6, 2018 15:17
Example of how to get an autosized textarea working in Vue.js

Autosize Textarea

I seem to implement this feature in just about every project, and I never seem to be able to find my old implementations easily enough. So, here's a gist. Should help to get things started.

JS

Using Vue:

// Autosize textarea directive

Migrations

Plugins

  • Expose their mappings so that they are programmatically consumable and easily associated w/ the right plugin
  • Expose a list of migrations, which are one of:
    • Transform: Migrate a document from one version to the next
    • Seed: Create a new document
  • Old data which is imported will be run through the migrations before being persisted
@chrisdavies
chrisdavies / open_struct_perf_test.rb
Created June 23, 2016 13:41
A perf test of Ruby's OpenStruct vs a few other options
# OpenStruct is slow. But if you need/want similar functionality,
# you can achieve it with really good perf results. See the impl
# of NotificationEvent below.
#
# Benchmark results from my Macbook (2.6 GHz Intel Core i5)
#
# Rehearsal -----------------------------------------------------
# Literal 1.060000 0.020000 1.080000 ( 1.080056)
# NotificationEvent 1.350000 0.000000 1.350000 ( 1.367066)
# OpenStruct 11.500000 0.110000 11.610000 ( 11.646464)
Look for `// CHRIS:` comments to get some inline thoughts.
Well, I learned something new. I've never seen "text/babel" as a script type. How are you transpiling your JSX?
I think if I were interviewing you, I'd ask why you didn't use ES6 modules or TypeScript, and something like Webpack to bundle them. It's something you should familiarize yourself with, since every production stack you will work with will be using some equivalent. (TypeScript + VS Code is a great combo, by the way.)
You'll also benefit from using a linter on your JavaScript. It catches bugs, and ensures you format things nicely (which helps when someone's interviewing / reviewing your code!)
So here's what I'd recommend after a quick scan:
@chrisdavies
chrisdavies / sorted-router-example.js
Last active December 16, 2016 13:41
A naive wrapper around Backbone router to make it understand route specificity (so route order doesn't matter)...
// Example usage of SortedRouter
var SortedRouter = require('./sortedrouter');
var router = new SortedRouter();
// Supports multiple URLs
router.route('', 'books', show('<h1>Books</h1>'));
router.route('books/new', show('<h1>New Book</h1>'));
router.route('books/:id', show('<h1>Show Book</h1>'));
@chrisdavies
chrisdavies / qunit-zombie.js
Last active April 12, 2016 18:20
Gulp task for running qUnit with zombiejs
// This is what your gulp task should look like
gulp.task('qunit', function(done) {
var files = glob.sync('./test/**/*.html');
runAllQunits(files);
});
// Runs through each qunit file (one at a time, though this could be relatively easily parallelized)
function runAllQunits(testFiles) {
var browser = new Zombie();