Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(palindrome): check if string is a palindrome #2477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Validator | Description
**isVAT(str, countryCode)** | check if the string is a [valid VAT number][VAT Number] if validation is available for the given country code matching [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2]. <br/><br/>`countryCode` is one of `['AL', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'BY', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GT', 'HN', 'HR', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'KZ', 'LT', 'LU', 'LV', 'MK', 'MT', 'MX', 'NG', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'RS', 'RU', 'SA', 'SE', 'SI', 'SK', 'SM', 'SV', 'TR', 'UA', 'UY', 'UZ', 'VE']`.
**isWhitelisted(str, chars)** | check if the string consists only of characters that appear in the whitelist `chars`.
**matches(str, pattern [, modifiers])** | check if the string matches the pattern.<br/><br/>Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`.
**isPalindrome(str)** | check if the string is a [Palindrome](https://en.wikipedia.org/wiki/Palindrome).

## Sanitizers

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import isLicensePlate from './lib/isLicensePlate';
import isStrongPassword from './lib/isStrongPassword';

import isVAT from './lib/isVAT';
import isPalindrome from './lib/isPalindrome';

const version = '13.12.0';

Expand Down Expand Up @@ -243,6 +244,7 @@ const validator = {
isLicensePlate,
isVAT,
ibanLocales,
isPalindrome,
};

export default validator;
9 changes: 9 additions & 0 deletions src/lib/isPalindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import assertString from './util/assertString';

export default function isPalindrome(str) {
assertString(str);
/* remove all special characters, spaces and make lowercase*/

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Expected space or tab before '*/' in comment

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Expected space or tab before '*/' in comment

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Expected space or tab before '*/' in comment

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Expected space or tab before '*/' in comment

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Expected space or tab before '*/' in comment

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Expected space or tab before '*/' in comment

Check failure on line 5 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Expected space or tab before '*/' in comment
let removeChar = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 6 in src/lib/isPalindrome.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote
let reverseStr = removeChar.split('').reverse().join('');
return removeChar === reverseStr;
}
17 changes: 17 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15459,4 +15459,21 @@
],
});
});
it('should validate Palindromes', () => {
test({
validator: 'isPalindrome',
valid: [
'race car',
'A man, a plan, a canal. Panama',
'never odd or even',
'My age is 0, 0 si ega ym',
'civic',
],
invalid: [
'not a palindrome',
'nope',
'name is alan'

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Missing trailing comma

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Missing trailing comma

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Missing trailing comma

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Missing trailing comma

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Missing trailing comma

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Missing trailing comma

Check failure on line 15475 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Missing trailing comma
],
});
});
});
Loading