Skip to content
Merged
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const count = stringzy.analyze.wordCount('Hello world'); // 2
- [isUpperCase](#isuppercase) - Checks if given string only has upper case characters.
- [isAlphabetic](#isalphabetic) - Checks if input string contains only Alphabets (case insensitive)
- [isAlphaNumeric](#isalphanumeric) - Checks if input string contains only Alphabets and Digits (case insensitive)
- [isAnagram](#isanagram)- Checks if both strings are anagrams of each other. (ignores case and punctuations)

### Analysis

Expand Down Expand Up @@ -730,6 +731,29 @@ isAlphaNumeric(''); // false
| --------- | ------ | -------- | ----------------------------------------------- |
| text | string | required | The input string to check for alphanumeric only |

#### <a id="isanagram"></a>`isAnagram(str1, str2)`

Checks whether two strings are anagrams of each other (contain the same characters in the same frequency, regardless of order).
- Comparison is case-insensitive.
- Spaces and punctuation are ignored.
- Throws an error if either input is not a string.

```javascript
import { isAnagram } from 'stringzy';

isAnagram('listen', 'silent'); // true
isAnagram('Debit Card', 'Bad Credit'); // true
isAnagram('Astronomer', 'Moon starer'); // true
isAnagram('hello', 'world'); // false
isAnagram('a', 'b'); // false
isAnagram('', ''); // true
```

| Parameter | Type | Default | Description |
| --------- | ------ | -------- | ---------------------------------------- |
| str1 | string | required | The first string to check as an anagram |
| str2 | string | required | The second string to check as an anagram |

---

### 📊 Analysis
Expand Down
24 changes: 13 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"url": "git+https://github.com/Samarth2190/stringzy.git"
},
"devDependencies": {
"@types/node": "^24.0.4",
"@types/node": "^24.5.2",
"prettier": "^3.6.2",
"typescript": "^5.8.3"
}
Expand Down
48 changes: 48 additions & 0 deletions src/tests/validations/isAnagram.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { isAnagram } from '../../validations/isAnagram';

describe('isAnagram', () => {
it('returns true for valid anagrams (simple lowercase words)', () => {
assert.strictEqual(isAnagram('listen', 'silent'), true);
assert.strictEqual(isAnagram('evil', 'vile'), true);
});

it('returns true for case-insensitive matches', () => {
assert.strictEqual(isAnagram('Listen', 'Silent'), true);
assert.strictEqual(isAnagram('Debit Card', 'Bad Credit'), true);
});

it('returns true when ignoring spaces and punctuation', () => {
assert.strictEqual(isAnagram('Astronomer', 'Moon starer'), true);
assert.strictEqual(isAnagram('The eyes!!', 'They see'), true);
});

it('returns false for non-anagrams', () => {
assert.strictEqual(isAnagram('hello', 'world'), false);
assert.strictEqual(isAnagram('abc', 'abcd'), false);
});

it('returns true for single character anagrams', () => {
assert.strictEqual(isAnagram('a', 'a'), true);
});

it('returns false for different single characters', () => {
assert.strictEqual(isAnagram('a', 'b'), false);
});

it('returns true for empty strings (both empty)', () => {
assert.strictEqual(isAnagram('', ''), true);
});

it('returns false when only one string is empty', () => {
assert.strictEqual(isAnagram('', 'a'), false);
assert.strictEqual(isAnagram('a', ''), false);
});

it('throws an error if inputs are not strings', () => {
assert.throws(() => isAnagram(123 as any, 'abc'), /Both inputs must be strings/);
assert.throws(() => isAnagram(null as any, 'abc'), /Both inputs must be strings/);
assert.throws(() => isAnagram(undefined as any, 'abc'), /Both inputs must be strings/);
});
});
5 changes: 4 additions & 1 deletion src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {isLowerCase} from './isLowerCase';
export {isUpperCase} from './isUpperCase';
export { isAlphabetic } from './isAlphabetic';
export { isAlphaNumeric } from './isAlphaNumeric';
export { isAnagram } from './isAnagram';

import { isCoordinates } from './isCoordinates';
import { isDate } from './isDate';
Expand All @@ -25,6 +26,7 @@ import { isLowerCase } from './isLowerCase';
import { isUpperCase } from './isUpperCase';
import { isAlphabetic } from './isAlphabetic';
import { isAlphaNumeric } from './isAlphaNumeric';
import { isAnagram } from './isAnagram';

export const validations = {
isCoordinates,
Expand All @@ -39,5 +41,6 @@ export const validations = {
isLowerCase,
isUpperCase,
isAlphabetic,
isAlphaNumeric
isAlphaNumeric,
isAnagram
};
23 changes: 23 additions & 0 deletions src/validations/isAnagram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Checks whether two strings are anagrams of each other.
*
* Rules:
* - Comparison is case-insensitive.
* - Spaces and punctuation are ignored.
*
* @param {string} str1 - The first input string.
* @param {string} str2 - The second input string.
* @returns {boolean} True if the inputs are anagrams, otherwise false.
* @throws {TypeError} If either input is not a string.
*/
export function isAnagram(str1: string, str2: string): boolean {
if (typeof str1 !== 'string' || typeof str2 !== 'string') {
throw new TypeError('Both inputs must be strings');
}

// Normalize: lowercase, remove spaces & punctuation
const normalize = (str: string) =>
str.toLowerCase().replace(/[^a-z0-9]/g, '').split('').sort().join('');

return normalize(str1) === normalize(str2);
}