1/2j
This commit is contained in:
parent
e092272bd4
commit
5573b6d6a5
@ -19,45 +19,47 @@ describe("addStringOfNumbers", () => {
|
|||||||
{ input: "-2", expected: -2 },
|
{ input: "-2", expected: -2 },
|
||||||
{ input: "-2.333", expected: -2.333 },
|
{ input: "-2.333", expected: -2.333 },
|
||||||
])(
|
])(
|
||||||
"should return n when given the string representation of n",
|
"should return n when given the string representation of n",
|
||||||
({ input, expected }) => {
|
({ input, expected }) => {
|
||||||
// arrange
|
// arrange
|
||||||
// act
|
// act
|
||||||
const result = addStringOfNumbers(input);
|
const result = addStringOfNumbers(input);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(result).toBe(expected);
|
expect(result).toBe(expected);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
it.each(["--4", "1-23", "3..5"])(
|
it.each(["--4", "1-23", "3..5"])(
|
||||||
"should throw an error when given a string that is not a number",
|
"should throw an error when given a string that is not a number",
|
||||||
(input) => {
|
(input) => {
|
||||||
// arrange
|
// arrange
|
||||||
// act
|
// act
|
||||||
const act = () => addStringOfNumbers(input);
|
const act = () => addStringOfNumbers(input);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(act).toThrow("Not valid number");
|
expect(act).toThrow("Not valid number");
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
{ input: "2,3", expected: 5 },
|
{ input: "2,3", expected: 5 },
|
||||||
{ input: "-2.5,4", expected: 1.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",
|
"should add any quantity of comma separated numbers in a string",
|
||||||
({ input, expected }) => {
|
({ input, expected }) => {
|
||||||
// arrange
|
// arrange
|
||||||
// act
|
// act
|
||||||
const result = addStringOfNumbers(input);
|
const result = addStringOfNumbers(input);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(result).toBe(expected);
|
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
|
// arrange
|
||||||
const input = "-2.5;3";
|
const input = "-2.5;3";
|
||||||
|
|
||||||
@ -67,4 +69,47 @@ describe("addStringOfNumbers", () => {
|
|||||||
// assert
|
// assert
|
||||||
expect(act).toThrow("Invalid characters");
|
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);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
@ -1,17 +1,37 @@
|
|||||||
|
const defaultSeparator = ",";
|
||||||
|
|
||||||
export function addStringOfNumbers(input: string) {
|
export function addStringOfNumbers(input: string) {
|
||||||
if (input === "") return 0;
|
if (input === "") return 0;
|
||||||
|
|
||||||
// Regex to catch any character that is not a digit, comma, dot or minus sign
|
const customSeparatorMatches = input.match(/^\/\/(.+)\n/);
|
||||||
const isContainingBadCharacters = /[^-,.\d]/.test(input);
|
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 (isContainingBadCharacters) throw new Error("Invalid characters");
|
||||||
|
|
||||||
if (!input.includes(",")) {
|
if (!focusedExpression.includes(separator)) {
|
||||||
const num = Number(input);
|
const num = Number(focusedExpression);
|
||||||
if (isNaN(num)) throw new Error("Not valid number");
|
if (isNaN(num)) throw new Error("Not valid number");
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
const arr = input.split(",");
|
const arr = focusedExpression.split(separator);
|
||||||
return arr.reduce((acc, str) => {
|
return arr.reduce((acc, str) => {
|
||||||
const num = Number(str);
|
const num = Number(str);
|
||||||
if (isNaN(num)) throw new Error("Not valid numbers");
|
if (isNaN(num)) throw new Error("Not valid numbers");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user