Initial commit

This commit is contained in:
Nils Norman Haukås 2013-02-20 22:29:09 +01:00
commit 8bb6a44a9a
5 changed files with 92 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

18
README.md Normal file
View File

@ -0,0 +1,18 @@
#Coding kata
An attempt at implementing a simple string calculator in Coffee-script
#Installation
1. First install node.js, thereby including npm.
2. Git clone this folder
3. Do a 'npm install' inside folder
4. Run 'npm test' to see tests
#License
Copyright (C) 2013 Nils Norman Haukås
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

15
calc.coffee Normal file
View File

@ -0,0 +1,15 @@
exports.adder = (input) ->
if input is '' then return '0'
else if input isnt '' and input.length is 1
return parseInt(input[0])
else if input.length > 1
if /^\/\//.test(input) is true
endIndex = input.search(/\n/)
customDelimiter = input.slice 2, endIndex
input = input.slice endIndex
input = input.split /\D/
result = 0
for num in input
if num isnt '' then result = result + parseInt(num)
return result

33
package.json Normal file
View File

@ -0,0 +1,33 @@
{
"name": "coding-cata-calc",
"version": "0.0.1",
"description": "Implementation of a simple string calculator in Coffee-script",
"main": "calc.coffee",
"directories": {
"test": "test"
},
"dependencies": {
"chai": "~1.5.0"
},
"devDependencies": {
"coffee-script": "~1.4.0"
},
"scripts": {
"test": "mocha -w -R nyan --compilers coffee:coffee-script"
},
"repository": {
"type": "git",
"url": "git://github.com/nilsnh/coding-cata-calc.git"
},
"keywords": [
"coffee-script",
"coding",
"kata",
"Bergen",
"coding",
"dojo"
],
"author": "Nils Norman Haukås",
"license": "mit",
"readmeFilename": "readme.md"
}

25
test/test.coffee Normal file
View File

@ -0,0 +1,25 @@
assert = require 'assert'
should = require('chai').should()
expect = require('chai').expect
calc = require '../calc'
describe 'calculator', () ->
it 'should exist an calc obj', () ->
expect(calc).to.be.a 'object'
it 'should have adder function', () ->
expect(calc.adder).to.be.a 'function'
it 'should add input together', () ->
calc.adder('').should.equal '0'
it 'should accept a number and return it', () ->
calc.adder('1').should.equal 1
calc.adder('2').should.equal 2
it 'handles two numbers by putting them together', () ->
calc.adder('1,2').should.equal 3
it 'handles any number of numbers by putting them together', () ->
calc.adder('1,20,3').should.equal 24
calc.adder('3,2,1').should.equal 6
calc.adder('1,2,3,4').should.equal 10
it 'handles new lines between commands', () ->
calc.adder('1\n2,3').should.equal 6
it 'should support specifying custom delimiter', () ->
calc.adder('//;\n1;2').should.equal 3