{"id":26854206,"url":"https://github.com/sanjibroy360/string-calculator-tdd","last_synced_at":"2026-04-30T00:04:51.613Z","repository":{"id":285033628,"uuid":"955829188","full_name":"sanjibroy360/String-Calculator-TDD","owner":"sanjibroy360","description":"A simple string calculator that can add numbers from a given string input with customizable delimiters and validation options.","archived":false,"fork":false,"pushed_at":"2025-03-29T02:40:28.000Z","size":59,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T03:26:19.816Z","etag":null,"topics":["jest","object-oriented-programming","oop-typescript","string-calculator","string-calculator-kata","tdd-typescript","test-driven-development","typescript"],"latest_commit_sha":null,"homepage":"https://osherove.com/tdd-kata-1/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sanjibroy360.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-27T09:08:27.000Z","updated_at":"2025-03-29T02:40:31.000Z","dependencies_parsed_at":"2025-03-29T03:36:33.558Z","dependency_job_id":null,"html_url":"https://github.com/sanjibroy360/String-Calculator-TDD","commit_stats":null,"previous_names":["sanjibroy360/string-calculator-tdd"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjibroy360%2FString-Calculator-TDD","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjibroy360%2FString-Calculator-TDD/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjibroy360%2FString-Calculator-TDD/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjibroy360%2FString-Calculator-TDD/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanjibroy360","download_url":"https://codeload.github.com/sanjibroy360/String-Calculator-TDD/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246391104,"owners_count":20769517,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jest","object-oriented-programming","oop-typescript","string-calculator","string-calculator-kata","tdd-typescript","test-driven-development","typescript"],"created_at":"2025-03-30T23:18:55.012Z","updated_at":"2026-04-30T00:04:51.574Z","avatar_url":"https://github.com/sanjibroy360.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eString Calculator\u003c/h1\u003e\n\u003cbr/\u003e\n\u003cp\u003eA simple string calculator that can add numbers from a given string input with customizable delimiters and validation options.\u003c/p\u003e\n\n## Implementation Approach\n\nThe calculator is implemented using two classes:\n\n### 1. `Calculator`\n\n- Handles basic addition logic.\n- Ensures numbers larger than the allowed maximum are ignored.\n- Throws errors for negative numbers if required.\n\n### 2. `StringCalculator` (Extends `Calculator`)\n\n- Parses input strings and extracts numbers.\n- Supports custom delimiters, multi-character delimiters, and multiple delimiters.\n- Converts input into a format suitable for `Calculator` to process.\n\n### Test-Driven Development (TDD) Approach  \nI followed the TDD methodology by first writing failing test cases before implementing the functionality. Each feature is built incrementally, ensuring all tests passed before moving to the next. I have completed all the steps of [TDD Kata 1](https://osherove.com/tdd-kata-1/).\n\n\u003cimg width=\"418\" alt=\"Screenshot 2025-03-29 at 8 06 27 AM\" src=\"https://github.com/user-attachments/assets/b3ca52ad-3577-4435-aaae-1e78846b556d\" /\u003e\n\n\n\n## Usage\n\n```typescript\nconst calculator = new StringCalculator();\nconst result = calculator.add(\"1,2,3\"); // Returns 6\nconsole.log(result); // 6\n```\n\n## Features\n\n### 1. Supports adding numbers from a comma-separated string\n\n```typescript\ncalculator.add(\"1,2\"); // Returns 3\n```\n\n### 2. Handles empty string and single-digit string\n\n```typescript\ncalculator.add(\"\"); // Returns 0\ncalculator.add(\"5\"); // Returns 5\n```\n\n### 3. Handles an unknown amount of numbers\n\n```typescript\ncalculator.add(\"1,2,3,4,5\"); // Returns 15\n```\n\n### 4. Supports newline (`\\n`) as a delimiter\n\n```typescript\ncalculator.add(\"1\\n2,3\"); // Returns 6\n```\n\n### 5. Allows custom delimiters\n\nYou can define a custom delimiter by specifying it at the beginning of the string using the pattern:\n`//[delimiter]\\n[numbers…]`\n\n```typescript\ncalculator.add(\"//;\\n1;2\"); // Returns 3\n```\n\n### 6. Throws an exception for negative numbers\n\n```typescript\ncalculator.add(\"1,-2,3\"); // Throws \"negative numbers not allowed -2\"\ncalculator.add(\"-1,-2,-3\"); // Throws \"negative numbers not allowed -1,-2,-3\"\n```\n\n### 7. Ignores numbers greater than a specified limit (default: 1000)\n\n```typescript\ncalculator.add(\"2,1001\"); // Returns 2 (1001 is ignored)\n```\n\n### 8. Supports multiple custom delimiters, including delimiters of any length\n\n```typescript\ncalculator.add(\"//[***]\\n1***2***3\"); // Returns 6\ncalculator.add(\"//[*][%]\\n1*2%3\"); // Returns 6\ncalculator.add(\"//[***][%%]\\n1***2%%3\"); // Returns 6\n```\n\n## Customizable Options\n\n### 1. Allow Only Positive Numbers\n\nBy default, negative numbers are not allowed. If a negative number is found, an error will be thrown.\n\n```typescript\ncalculator.add(\"-9\"); // Throws \"negative numbers not allowed -9\"\ncalculator.add(\"1,2,-3\"); // Throws \"negative numbers not allowed -3\"\n```\n\nTo allow negative numbers, set allowOnlyPositiveNumbers to false:\n\n```typescript\ncalculator.add(\"1,2,-3\", false); // Returns 0 (default sum behavior)\n```\n\n### 2. Ignore Numbers Larger than a Certain Limit\n\nBy default, numbers larger than 1000 are ignored.\n\n```typescript\ncalculator.add(\"1,1001,2\"); // Returns 3 (1001 is ignored)\ncalculator.add(\"1001\"); // Returns 0\n```\n\nYou can change this limit by setting maxAllowedNumber:\n\n```typescript\ncalculator.add(\"1,2000,2\", true, 2000); // Returns 2003 (2000 is now included)\n```\n\n### 3. Custom Delimiters\n\nYou can define a custom delimiter by specifying it at the beginning of the string in the format: `\"//[delimiter]\\n[numbers…]\"`\n\n```typescript\ncalculator.add(\"//;\\n1;2\"); // Returns 3 (using `;` as a delimiter)\n```\n\n### 4. Multi-character Delimiters\n\nYou can use a delimiter of any length.\n\n```typescript\ncalculator.add(\"//[***]\\n1***2***3\"); // Returns 6\n```\n\n### 5. Multiple Delimiters\n\nYou can define multiple custom delimiters of any length.\n\n```typescript\ncalculator.add(\"//[*][%]\\n1*2%3\"); // Returns 6\ncalculator.add(\"//[***][%%]\\n1***2%%3\"); // Returns 6\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanjibroy360%2Fstring-calculator-tdd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanjibroy360%2Fstring-calculator-tdd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanjibroy360%2Fstring-calculator-tdd/lists"}