Skip to content

Instantly share code, notes, and snippets.

View drKnoxy's full-sized avatar

Adam Willoughby-Knox drKnoxy

  • Charleston, SC
View GitHub Profile
@drKnoxy
drKnoxy / index.html
Created June 11, 2015 14:37
AngularJS: Data binding between service and controller
<html>
<head>
<link data-require="bootstrap@*" data-semver="3.3.2" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script data-require="bootstrap@*" data-semver="3.3.2" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script data-require="jquery@2.1.3" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<style>
body {
padding: 20px 0;
@drKnoxy
drKnoxy / js.js
Created December 7, 2015 15:51
Simple javascript templating
/**
* Simple template rendering
* + double curly brace syntax {{id}} or {{ id }}
* + only supports rendering, no conditions or expressions
* + no error checking
*
* @param {string} tmpl Your template string
* @param {obj} data The data to inject
* @return {string}
*/
@drKnoxy
drKnoxy / cached-promise.js
Last active May 18, 2016 02:15
cacheing promises in angularJS so requests don't pile up
angular.module('configModule')
.factory('timezoneService', ['configApiService', '$q',
function(configApiService, $q) {
var tzPromise = false;
var service = {
get: getTz,
};
@drKnoxy
drKnoxy / round.go
Created April 21, 2016 18:27
Truncating vs. Rounding in go
// http://play.golang.org/p/KNhgeuU5sT
package main
import (
"fmt"
"math"
)
// truncate a float to two levels of precision
func Truncate(some float64) float64 {
@drKnoxy
drKnoxy / product-options-with-recursion.js
Last active May 17, 2016 23:38
Using recursion to make an array of all product options
var options = [
["red", "blue", "green", "yellow"], // color
["small", "medium", "large"], // size
["heavy", "light"], // thickness
];
scope.inventory = findPermutations(options);
//////////////////////
function findPermutations(options) {
@drKnoxy
drKnoxy / promise-all.js
Created May 18, 2016 02:14
an implementation of allResolved for angularJS
// NOTE: $q.all will reject if one of the promises is bad, so we can't use it here
// and have instead replicateed $Q.allResolved
function allResolved(promises) {
var deferred = $q.defer(), // our aggregate promise
counter = 0, // know when to resolve our deferred
results = []; // where we collect the ordered results of each promise
angular.forEach(promises, function(promise, key) {
counter++;
// might be a value, wrap it up to be safe
@drKnoxy
drKnoxy / clarify-pingdom.js
Created August 25, 2016 16:41
sort pingdom results, expand poor results
console.clear()
sortPerfTable()
expandPoorSuggestions()
showFullText()
function sortPerfTable() {
const tbl = document.querySelector('.table-perfinsights tbody');
// PhotoResponse is thing
type PhotoResponse struct {
Total int `json:"total"`
TotalPages int `json:"total_pages"`
Results []Photo `json:"results"`
}
// Photo is thing
type Photo struct {
ID string `json:"id"`
Images ImageSet `json:"urls"`
import (
"encoding/json"
"fmt"
"net/http"
)
// ...
func main() {
url := baseURL + "/search/photos/?query=hats&client_id=" + applicationID
package main
import (
"io"
"net/http"
"os"
)
const (
apiVersion = "v1"