diff --git a/src/utils/palindrome.js b/src/utils/palindrome.js new file mode 100644 index 0000000..3403315 --- /dev/null +++ b/src/utils/palindrome.js @@ -0,0 +1,17 @@ +/** + * Author: Niki Nielsen + * Date: 06/03/2026 + * File: palindrome.js + * Description: isPalindrome function. + */ + +"use strict"; + +function isPalindrome(str) { + if (typeof str !== "string") return false; + + const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ""); + return cleaned === cleaned.split("").reverse().join(""); +} + +module.exports = isPalindrome; diff --git a/test/utils/palindrome.spec.js b/test/utils/palindrome.spec.js new file mode 100644 index 0000000..954ea22 --- /dev/null +++ b/test/utils/palindrome.spec.js @@ -0,0 +1,23 @@ +/** + * Author: Niki Nielsen + * Date: 06/03/2026 + * File: palindrome.spec.js + * Description: Tests for the isPalindrome function. + */ +"use strict"; + +const isPalindrome = require("../../src/utils/palindrome"); + +describe("palindrome.js", () => { + it("should return true for a simple palindrome", () => { + expect(isPalindrome("racecar")).toBe(true); + }); + + it("should return false for a non-palindrome", () => { + expect(isPalindrome("hello")).toBe(false); + }); + + it("should handle punctuation and mixed case", () => { + expect(isPalindrome("A man, a plan, a canal: Panama")).toBe(true); + }); +});