diff --git a/src/exercises/addStringOfNumbers/index.test.ts b/src/exercises/addStringOfNumbers/index.test.ts index 2fcda00..bbfa8e5 100644 --- a/src/exercises/addStringOfNumbers/index.test.ts +++ b/src/exercises/addStringOfNumbers/index.test.ts @@ -19,45 +19,47 @@ describe("addStringOfNumbers", () => { { input: "-2", expected: -2 }, { input: "-2.333", expected: -2.333 }, ])( - "should return n when given the string representation of n", - ({ input, expected }) => { - // arrange - // act - const result = addStringOfNumbers(input); + "should return n when given the string representation of n", + ({ input, expected }) => { + // arrange + // act + const result = addStringOfNumbers(input); - // assert - expect(result).toBe(expected); - } + // assert + expect(result).toBe(expected); + } ); it.each(["--4", "1-23", "3..5"])( - "should throw an error when given a string that is not a number", - (input) => { - // arrange - // act - const act = () => addStringOfNumbers(input); + "should throw an error when given a string that is not a number", + (input) => { + // arrange + // act + const act = () => addStringOfNumbers(input); - // assert - expect(act).toThrow("Not valid number"); - } + // assert + expect(act).toThrow("Not valid number"); + } ); it.each([ { input: "2,3", expected: 5 }, { input: "-2.5,4", expected: 1.5 }, + { input: "1,2,5,9", expected: 17 }, + { input: "1,-3.2,3.7,2", expected: 3.5 }, ])( - "should add two comma separated numbers in a string", - ({ input, expected }) => { - // arrange - // act - const result = addStringOfNumbers(input); + "should add any quantity of comma separated numbers in a string", + ({ input, expected }) => { + // arrange + // act + const result = addStringOfNumbers(input); - // assert - expect(result).toBe(expected); - } + // assert + expect(result).toBe(expected); + } ); - it("should throw an error when given a string with wrong separator", () => { + it("should throw when given a string with wrong separator", () => { // arrange const input = "-2.5;3"; @@ -67,4 +69,47 @@ describe("addStringOfNumbers", () => { // assert expect(act).toThrow("Invalid characters"); }); + + it.each([ + { input: "//;\n1;2", expected: 3 }, + { input: "//:\n-2.5:4", expected: 1.5 }, + { input: "//;\n1;2;5;9", expected: 17 }, + ])( + "should add any quantity of numbers separated by a custom character in a string", + ({ input, expected }) => { + // arrange + // act + const result = addStringOfNumbers(input); + + // assert + expect(result).toBe(expected); + } + ); + + it.each(["//.\n1.2", "//-\n-2.5-4"])( + "should throw when the custom separator is not valid", + (input) => { + // arrange + // act + const act = () => addStringOfNumbers(input); + + // assert + expect(act).toThrow("Invalid custom separator"); + } + ); + + it.each([ + { input: "//;;;\n1;;;2", expected: 3 }, + { input: "//::\n-2.5::4", expected: 1.5 }, + ])( + "should add any quantity of numbers separated by a custom character in a string", + ({ input, expected }) => { + // arrange + // act + const result = addStringOfNumbers(input); + + // assert + expect(result).toBe(expected); + } + ); }); diff --git a/src/exercises/addStringOfNumbers/index.ts b/src/exercises/addStringOfNumbers/index.ts index c69230c..6c1c99f 100644 --- a/src/exercises/addStringOfNumbers/index.ts +++ b/src/exercises/addStringOfNumbers/index.ts @@ -1,17 +1,37 @@ +const defaultSeparator = ","; + export function addStringOfNumbers(input: string) { if (input === "") return 0; - // Regex to catch any character that is not a digit, comma, dot or minus sign - const isContainingBadCharacters = /[^-,.\d]/.test(input); + const customSeparatorMatches = input.match(/^\/\/(.+)\n/); + const customSeparator = customSeparatorMatches + ? customSeparatorMatches[1] + : null; + + const separator = customSeparator || defaultSeparator; + const isContainingBadSeparator = /[-.]/.test(separator); + if (isContainingBadSeparator) throw new Error("Invalid custom separator"); + + const isSeparatorMadeWithSameCharacters = !separator + .split("") + .some((char) => char !== separator[0]); + if (!isSeparatorMadeWithSameCharacters) + throw new Error("Invalid custom separator"); + + // Regex to catch any character that is not a digit, dot, minus sign or separator + const badCharactersRegex = new RegExp(`[^-.\\d${separator}]`); + + const focusedExpression = customSeparator ? input.split("\n")[1] : input; + const isContainingBadCharacters = badCharactersRegex.test(focusedExpression); if (isContainingBadCharacters) throw new Error("Invalid characters"); - if (!input.includes(",")) { - const num = Number(input); + if (!focusedExpression.includes(separator)) { + const num = Number(focusedExpression); if (isNaN(num)) throw new Error("Not valid number"); return num; } - const arr = input.split(","); + const arr = focusedExpression.split(separator); return arr.reduce((acc, str) => { const num = Number(str); if (isNaN(num)) throw new Error("Not valid numbers");