Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Basic CoffeeScript + AngularJS Pig-latin filter + unit tests. Only moves first letter at the moment. AngularJS filters are my favorite TDD.
'use strict';
angular.module('pig-latin', [])
.filter 'pig-latin', () ->
# input is handed into the filter
(input) ->
# equals itself or ''
input ||= ''
if input.length > 0
# splits string based on char
firstChar = input.slice(0, 1)
theRest = input.slice(1)
input = theRest + firstChar + "a"
# returns input
'use strict'
describe 'Pig Latin Filter', () ->
# initialize a new instance of the filter before each test
filter = {}
# load the filter's module
beforeEach module('pig-latin')
# give our local object the magical injection
beforeEach inject ($filter) ->
filter = $filter 'pig-latin'
# pretty existential, huh?
it 'should have a filter', () ->
# base case, handles no input
it 'should return an empty string when given null', () ->
output = filter()
# simple test
it 'should set "bozo" to "ozoba"', ->
output = filter("bozo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment