From 8bb6a44a9a33d7719866c2af7436630788bd0169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Wed, 20 Feb 2013 22:29:09 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + README.md | 18 ++++++++++++++++++ calc.coffee | 15 +++++++++++++++ package.json | 33 +++++++++++++++++++++++++++++++++ test/test.coffee | 25 +++++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 calc.coffee create mode 100644 package.json create mode 100644 test/test.coffee diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..90ac2dd --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/calc.coffee b/calc.coffee new file mode 100644 index 0000000..ca11cdc --- /dev/null +++ b/calc.coffee @@ -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 + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..78ce7bd --- /dev/null +++ b/package.json @@ -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" +} diff --git a/test/test.coffee b/test/test.coffee new file mode 100644 index 0000000..6b285a7 --- /dev/null +++ b/test/test.coffee @@ -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 \ No newline at end of file