diff --git a/.github/workflows/Ci.yml b/.github/workflows/Ci.yml index 68cd98c45b..cda14c3c6a 100644 --- a/.github/workflows/Ci.yml +++ b/.github/workflows/Ci.yml @@ -14,7 +14,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: "14" + node-version: "16.x" cache: npm - name: 📦 Install dependencies diff --git a/String/AlphaNumericPalindrome.js b/String/AlphaNumericPalindrome.js index b3dc7b7d64..f3de94b642 100644 --- a/String/AlphaNumericPalindrome.js +++ b/String/AlphaNumericPalindrome.js @@ -1,11 +1,11 @@ -/***************************************************************************** - * @function alphaNumericPlaindrome - * @description alphaNumericPlaindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case. +/** + * @function alphaNumericPalindrome + * @description alphaNumericPalindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case. * @param {string} str the string to check - * @returns {Boolean} - * @see [Factorial](https://en.wikipedia.org/wiki/Palindrome) + * @returns {boolean} + * @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome) * @example - * The function alphaNumericPlaindrome() receives a string with varying formats + * The function alphaNumericPalindrome() receives a string with varying formats * like "racecar", "RaceCar", and "race CAR" * The string can also have special characters * like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2" @@ -13,15 +13,18 @@ * But the catch is, we have to check only if the alphanumeric characters * are palindrome i.e remove spaces, symbols, punctuations etc * and the case of the characters doesn't matter - * - ****************************************************************************/ + */ +const alphaNumericPalindrome = (str) => { + if (typeof str !== 'string') { + throw new TypeError('Argument should be string') + } -const alphaNumericPlaindrome = (str) => { // removing all the special characters and turning everything to lowercase - const newStr = str.replace(/[^a-zA-Z0-9]*/g, '').toLowerCase() + const newStr = str.replace(/[^a-z0-9]+/ig, '').toLowerCase() + const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y) - for (let i = 0; i < newStr.length; i++) { - if (newStr[i] !== newStr[newStr.length - 1 - i]) { + for (let i = 0; i < midIndex; i++) { + if (newStr.at(i) !== newStr.at(~i)) { // ~n = -(n + 1) return false } } @@ -29,4 +32,4 @@ const alphaNumericPlaindrome = (str) => { return true } -export { alphaNumericPlaindrome } +export default alphaNumericPalindrome diff --git a/String/test/AlphaNumericPalindrome.test.js b/String/test/AlphaNumericPalindrome.test.js index f544c565be..ab7373b53b 100644 --- a/String/test/AlphaNumericPalindrome.test.js +++ b/String/test/AlphaNumericPalindrome.test.js @@ -1,21 +1,21 @@ -import { alphaNumericPlaindrome } from '../AlphaNumericPalindrome' +import alphaNumericPalindrome from '../AlphaNumericPalindrome' -test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { - expect(alphaNumericPlaindrome('eye')).toBe(true) -}) - -test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { - expect(alphaNumericPlaindrome('0_0 (: /-:) 0-0')).toBe(true) -}) +describe('Testing the alpha numeric palindrome', () => { + // should return true if the given string has alphanumeric characters that are palindrome irrespective of case and symbols + it('Testing with valid alphabetic palindrome', () => { + expect(alphaNumericPalindrome('eye')).toBe(true) + expect(alphaNumericPalindrome('Madam')).toBe(true) + expect(alphaNumericPalindrome('race CAR')).toBe(true) + expect(alphaNumericPalindrome('A man, a plan, a canal. Panama')).toBe(true) + }) -test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { - expect(alphaNumericPlaindrome('five|_/|four')).toBe(false) -}) - -test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { - expect(alphaNumericPlaindrome('A man, a plan, a canal. Panama')).toBe(true) -}) + it('Testing with number and symbol', () => { + expect(alphaNumericPalindrome('0_0 (: /-:) 0-0')).toBe(true) + expect(alphaNumericPalindrome('03_|53411435|_30')).toBe(true) + }) -test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { - expect(alphaNumericPlaindrome('1 eye for of 1 eye.')).toBe(false) + it('Testing with alphabets and symbols', () => { + expect(alphaNumericPalindrome('five|_/|evif')).toBe(true) + expect(alphaNumericPalindrome('five|_/|four')).toBe(false) + }) }) diff --git a/package-lock.json b/package-lock.json index 29273bc2ab..d7cb0da2ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "@babel/preset-env": "^7.11.5", "atob": "2.1.2", "jsdom": "^16.3.0", - "node": "^14.13.1", + "node": "^16.13.2", "node-fetch": "3.1.1" }, "devDependencies": { @@ -25,6 +25,9 @@ "husky": "^7.0.4", "jest": "^26.4.2", "standard": "^16.0.4" + }, + "engines": { + "node": ">=16.6.0" } }, "node_modules/@babel/code-frame": { @@ -8121,9 +8124,9 @@ "dev": true }, "node_modules/node": { - "version": "14.18.1", - "resolved": "https://registry.npmjs.org/node/-/node-14.18.1.tgz", - "integrity": "sha512-gBt7GlSQp68Bj5KoYx3dGgMCaE2PN0Qox3oiemTDINYHhJjML/Xd24jXuo7X1ehyJuVNZsLWYrJgX6DhHCze6w==", + "version": "16.13.2", + "resolved": "https://registry.npmjs.org/node/-/node-16.13.2.tgz", + "integrity": "sha512-6x0hohLASL0XaehYn1mrVw1UiiRjoxrujBxjqHGxGe/rFKKCJcDhoa5wNxxvBbyJm6mNgZYuzg0JTwvXmpeWWw==", "hasInstallScript": true, "dependencies": { "node-bin-setup": "^1.0.0" @@ -17157,9 +17160,9 @@ "dev": true }, "node": { - "version": "14.18.1", - "resolved": "https://registry.npmjs.org/node/-/node-14.18.1.tgz", - "integrity": "sha512-gBt7GlSQp68Bj5KoYx3dGgMCaE2PN0Qox3oiemTDINYHhJjML/Xd24jXuo7X1ehyJuVNZsLWYrJgX6DhHCze6w==", + "version": "16.13.2", + "resolved": "https://registry.npmjs.org/node/-/node-16.13.2.tgz", + "integrity": "sha512-6x0hohLASL0XaehYn1mrVw1UiiRjoxrujBxjqHGxGe/rFKKCJcDhoa5wNxxvBbyJm6mNgZYuzg0JTwvXmpeWWw==", "requires": { "node-bin-setup": "^1.0.0" } diff --git a/package.json b/package.json index 320b554813..8ba035922c 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@babel/preset-env": "^7.11.5", "atob": "2.1.2", "jsdom": "^16.3.0", - "node": "^14.13.1", + "node": "^16.13.2", "node-fetch": "3.1.1" }, "standard": { @@ -34,5 +34,8 @@ "husky": "^7.0.4", "jest": "^26.4.2", "standard": "^16.0.4" + }, + "engines": { + "node": ">=16.6.0" } }