Skip to content

Latest commit

Β 

History

History
73 lines (50 loc) Β· 2.63 KB

File metadata and controls

73 lines (50 loc) Β· 2.63 KB

πŸ§ͺ TDD Kata 1: String Calculator

A TypeScript implementation of the classic String Calculator Kata, built using a Test-Driven Development (TDD) approach with Jest.


πŸ“‹ Problem Statement

Create a class StringCalculator with a method add(numbers: string): number that can accept a string of numbers and return their sum, following a series of progressively complex rules.


βœ… Features Implemented

Step Feature
1 Returns 0 for an empty string ("" β†’ 0)
2 Returns the number itself for a single value ("1" β†’ 1)
3 Returns the sum of two comma-separated numbers ("1,2" β†’ 3)
4 Handles an unknown amount of comma-separated numbers
5 Supports newlines as valid delimiters ("1\n2,3" β†’ 6)
6 Supports custom delimiters ("//;\n1;2" β†’ 3)
7 Throws an exception for negative numbers ("negatives not allowed: -1")
8 Shows all negative numbers in the exception ("negatives not allowed: -1, -4")
9 Tracks method invocations via getCalledCount()
10 Ignores numbers greater than 1000 ("2,1001" β†’ 2)
11 Supports custom delimiters of any length ("//[***]\n1***2***3" β†’ 6)
12 Supports multiple delimiters ("//[*][%]\n1*2%3" β†’ 6)
13 Supports multiple delimiters with length > 1 ("//[**][%%]\n1**2%%3" β†’ 6)

πŸ›  Getting Started

πŸ“ Clone the Repository

git clone https://github.com/SSBarik/string-calculator-tdd-kata.git
cd string-calculator-kata

πŸ“¦ Install Dependencies

npm install

πŸš€ Run Tests

npm test

πŸ§ͺ Example Usage

import { StringCalculator } from './StringCalculator';

const calculator = new StringCalculator();

console.log(calculator.add(""));              // Output: 0
console.log(calculator.add("1"));             // Output: 1
console.log(calculator.add("1,2"));           // Output: 3
console.log(calculator.add("1\n2,3"));        // Output: 6
console.log(calculator.add("//;\n1;2"));      // Output: 3

console.log(calculator.getCalledCount());     // Output: 5


## πŸ“š References

- Original Kata: [TDD Kata by Roy Osherove](https://osherove.com/tdd-kata-1)