Skip to content

Instantly share code, notes, and snippets.

View rolaveric's full-sized avatar

Jason Stone rolaveric

View GitHub Profile
@rolaveric
rolaveric / providerTest.js
Last active August 29, 2015 13:56
Shows how to get a reference to a provider, before "provider.$get()" is called, for testing.
describe('myServiceProvider', function () {
var p;
// Uses the "module()" method to get a reference to the provider during the "config" phase
beforeEach(module('myModule', function (myServiceProvider) {
p = myServiceProvider;
}));
// Runs "inject()" so that the module gets instantiated.
// Because "myService" isn't injected, the "myServiceProvider" should still be pristine
@rolaveric
rolaveric / simpleDirectiveTest.js
Created February 4, 2014 22:33
An ng-hide directive test taken from the AngularJS source to illustrate a simple directive test.
describe('ngHide', function() {
it('should hide an element', inject(function($rootScope, $compile) {
var element = angular.element('<div ng-hide="exp"></div>');
element = $compile(element)($rootScope);
expect(element).toBeShown();
$rootScope.exp = true;
$rootScope.$digest();
expect(element).toBeHidden();
}));
});
@rolaveric
rolaveric / ngHideClone.js
Created February 4, 2014 22:27
A simplified implementation of the ng-hide directive, to illustrate directive testing.
angular.directive('ngHide', [function () {
return function (scope, element, attr) {
scope.$watch(attr.ngShow, function ngHideWatchAction(value){
// Call "addClass()" or "removeClass()" based on the attribute value
// AngularJS already declares the CSS for the "ng-hide" class
element[value ? 'removeClass' : 'addClass']('ng-hide');
});
};
}]);
@rolaveric
rolaveric / classicMartini.go
Last active August 29, 2015 13:56
A classic martini web server that demonstrates using middleware and route handlers to keep authentication and authorization code DRY.
package main
import (
"github.com/go-martini/martini"
)
func main() {
// Create a new Martini server
m := martini.New()
@rolaveric
rolaveric / typesEmptyInterfaceSlice.go
Created March 2, 2014 07:34
Example of using an empty interface slice to hold pointers to another slice in Go
cols, err := rows.Cols()
// rows.Scan wants '[]interface{}' as an argument, but we want the values
// loaded into a '[]string'. So we must copy the references into such a slice
// See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details
scanArgs := make([]interface{}, len(cols))
values := make([]string, len(cols))
for i := range values {
scanArgs[i] = &values[i]
}
@rolaveric
rolaveric / typesEmptyInterface.go
Created March 2, 2014 07:22
Example of using an empty interface to work around the type system in Go
// X needs anything
func X(anything interface{}) {
// Nothing comes for free - need to see what I got
switch value := anything.(type) {
case string:
fmt.Printf("I got a string!: %s", value)
default:
fmt.Print("I got... something!")
}
}
@rolaveric
rolaveric / typesSimple.go
Created March 2, 2014 06:53
A simple example of strict typing in Go
func Triple(input int) int {
// This line would be really awkward if we were allowed to accept a string
return input * 3
}
@rolaveric
rolaveric / typesFunctions.go
Created March 2, 2014 06:57
An example of a function type in Go
func DoSomething(callback func(message string) error) {
// Good thing I know this function will take a message string and return a single error response
err := callback("Did something good!")
if err != nil {
panic(err)
}
}
@rolaveric
rolaveric / typesInterfaces.go
Created March 2, 2014 07:07
Example of implicit interface implementations in Go
// A Doer is anything that will Do something
type Doer interface {
Do()
}
// X needs a Doer
func X(doer Doer) {
doer.Do()
}
@rolaveric
rolaveric / gopherjsResult.js
Created March 7, 2014 08:19
The result from building gopherjsIntroMain.go with GopherJS
"use strict";
(function() {
Error.stackTraceLimit = -1;
var go$reservedKeywords = ["abstract", "arguments", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"];
var go$global;
if (typeof window !== "undefined") {
go$global = window;