diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f759a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Node dependencies +node_modules/ + +# Logs +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + +# Environment files +.env +.env.local +.env.*.local + +# OS files +.DS_Store +Thumbs.db + +# Editor / IDE settings +.vscode/ +.idea/ + +# Coverage reports +coverage/ + +# Build output (if applicable) +dist/ +build/ diff --git a/README.md b/README.md index a70037b..9bcf904 100644 --- a/README.md +++ b/README.md @@ -1 +1,38 @@ -# nodejs-practice-repository \ No newline at end of file +# Node.js Practice Repository + +This repository contains small Node.js utility functions with automated unit tests. +Each feature is developed on its own branch and submitted via a Pull Request. + +--- + +## Palindrome Function + +### Description + +Adds a utility function that checks whether a given string is a palindrome. +The function ignores spaces, punctuation, and letter casing. + +Example: + +- `"racecar"` → `true` +- `"A man a plan a canal Panama"` → `true` +- `"hello"` → `false` + +--- + +## Setup Instructions + +### Prerequisites + +- Node.js (LTS version recommended) +- npm (included with Node.js) + +### Install Dependencies + +Clone the repository and install dependencies: + +```bash +git clone https://github.com/tink012184/nodejs-practice-repository.git +cd nodejs-practice-repository +npm install +``` diff --git a/package-lock.json b/package-lock.json index 8fce75c..8e2718d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,7 @@ "": { "name": "nodejs-practice-repository", "version": "1.0.0", - "license": "ISC", + "license": "MIT", "devDependencies": { "jest": "^29.7.0" } diff --git a/src/palindrome.js b/src/palindrome.js new file mode 100644 index 0000000..63faacc --- /dev/null +++ b/src/palindrome.js @@ -0,0 +1,20 @@ +/** + * Checks if a string is a palindrome. + * Ignores spaces, punctuation, and letter casing. + * + * @param {string} input + * @returns {boolean} + */ +function isPalindrome(input) { + if (typeof input !== "string") { + return false; + } + + const cleaned = input.toLowerCase().replace(/[^a-z0-9]/g, ""); + + const reversed = cleaned.split("").reverse().join(""); + + return cleaned === reversed; +} + +module.exports = { isPalindrome }; diff --git a/test/palindrome.test.js b/test/palindrome.test.js new file mode 100644 index 0000000..df8185f --- /dev/null +++ b/test/palindrome.test.js @@ -0,0 +1,15 @@ +const { isPalindrome } = require("../src/palindrome"); + +describe("isPalindrome", () => { + test("returns true for a simple palindrome", () => { + expect(isPalindrome("racecar")).toBe(true); + }); + + test("returns true for a palindrome with spaces and capitalization", () => { + expect(isPalindrome("A man a plan a canal Panama")).toBe(true); + }); + + test("returns false for a non-palindrome string", () => { + expect(isPalindrome("hello world")).toBe(false); + }); +});