A Node.js implementation of the Calculator KATA. This project demonstrates a simple string calculator that takes a string input, extracts numbers from it, and performs basic arithmetic operations. The project is built using Node.js and TypeScript and follows a TDD (Test-Driven Development) approach.
- Add numbers in a comma-separated string.
- Supports custom delimiters.
- Ignores numbers greater than or equal to 1000.
- Throws an error for negative numbers and lists them.
- Supports multi-character delimiters.
- Follows TDD principles with unit tests for each feature.
- Node.js (v12.x or higher)
- npm (v6.x or higher)
Follow the steps below to run the project on your local machine.
git clone https://github.com/mrpcodecraft/calculator-kata-nodejs
cd calculator-kata-nodejs
Once you have cloned the repository, install the necessary dependencies by running:
npm install
You can run the calculator by running the following command:
npm start
The program will prompt you to enter a string of numbers, separated by commas or custom delimiters, and then display the result.
This project follows a TDD approach, with tests written using Mocha and Chai. To run the test suite, use the following command:
npm test
You should see output showing the test results for all defined cases.
-
Input:
"1,2,3"
-
Output:
Result: 6
-
Input:
"//;\n1;2;5"
-
Output:
Result: 8
-
Input:
"//[***]\n1,2***5"
-
Output:
Result: 8
-
Input:
"//[***]\n1,1000***1001***5"
-
Output:
Result: 6
-
Input:
"//;\n1;-2;5"
-
Output: Throws an error:
"Negative numbers not allowed: -2"
The project consists of the following key files:
The core logic of the calculator, including functions for parsing the input string, handling custom delimiters, and calculating the sum of numbers.
The main entry point of the application that interacts with the user via the command line and calls the calculator logic.
Unit tests for the calculator functions, written using Mocha and Chai.
Contains metadata about the project, scripts for running the application and tests, and a list of dependencies.
- Node.js: JavaScript runtime for building the application.
- TypeScript: Strongly-typed language built on JavaScript.
- Mocha: Test framework for JavaScript.
- Chai: Assertion library for testing.
- String Parsing: The calculator takes a string input of numbers separated by delimiters (default is comma, but can be customized).
- Sum Calculation: It sums up all numbers from the string.
- Error Handling: It throws an error if there are negative numbers in the input, listing all such numbers.
- Custom Delimiters: Allows using custom single or multi-character delimiters by defining them at the start of the string.
For easier development, you can use nodemon
to automatically restart the application when you make changes to the source code.
npm run dev
-
Test empty string:
- Input:
""
- Expected Output:
0
- Input:
-
Test single value:
- Input:
"1"
- Expected Output:
1
- Input:
-
Test multiple values:
- Input:
"1,2,3"
- Expected Output:
6
- Input:
-
Test for custom delimiter:
- Input:
"//;\n1;2;3"
- Expected Output:
6
- Input:
-
Test for negative numbers:
- Input:
"//;\n1;-2;3"
- Expected Output: Throws an error:
Negative numbers not allowed: -2
- Input:
If you wish to contribute to this project, feel free to submit a pull request. Contributions in the form of bug fixes, features, or documentation are always welcome!
This project is licensed under the MIT License. See the LICENSE file for details.
nodejs
typescript
TDD
calculator
kata
mocha
chai
testing
Let me know if you need any more modifications!