forked from aristus/accent-folding
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from zr87/feat/add-replace-method-and-improve-…
…error-handling Feat/add replace method and improve error handling
- Loading branch information
Showing
5 changed files
with
879 additions
and
779 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,123 @@ | ||
import { expect, describe, it } from 'vitest'; | ||
import { expect, describe, it, beforeEach } from 'vitest'; | ||
import AccentFolding from './accentFolding.js'; | ||
import accentMap from './accentMap.json'; | ||
|
||
describe('AccentFolding', () => { | ||
const accentFolder = new AccentFolding(); | ||
let accentFolder; | ||
|
||
it('should recognize simple accents', () => { | ||
expect(accentFolder.highlightMatch('Fulanilo López', 'lo')).toBe( | ||
'Fulani<b>lo</b> <b>Ló</b>pez' | ||
); | ||
expect(accentFolder.highlightMatch('Erik Lørgensen', 'lo')).toBe( | ||
'Erik <b>Lø</b>rgensen' | ||
); | ||
expect(accentFolder.highlightMatch('James Lö', 'lo')).toBe( | ||
'James <b>Lö</b>' | ||
); | ||
beforeEach(() => { | ||
accentFolder = new AccentFolding(); | ||
}); | ||
|
||
it("should wrap matched fragment with custom tag added in second parameter '<strong>'", () => { | ||
expect( | ||
accentFolder.highlightMatch('Fulanilo López', 'lo', 'strong') | ||
).toEqual('Fulani<strong>lo</strong> <strong>Ló</strong>pez'); | ||
}); | ||
describe('highlightMatch', () => { | ||
it('should throw TypeError if str is not a string', () => { | ||
expect(() => accentFolder.highlightMatch(123, 'test')).toThrow(TypeError); | ||
expect(() => accentFolder.highlightMatch(123, 'test')).toThrow( | ||
'Both str and fragment must be strings' | ||
); | ||
}); | ||
|
||
it('wraps matched fragment with custom tag', () => { | ||
expect(accentFolder.highlightMatch('Fulanilo López', 'lo', 'strong')).toBe( | ||
'Fulani<strong>lo</strong> <strong>Ló</strong>pez' | ||
); | ||
}); | ||
it('should throw TypeError if fragment is not a string', () => { | ||
expect(() => accentFolder.highlightMatch('test', 123)).toThrow(TypeError); | ||
expect(() => accentFolder.highlightMatch('test', 123)).toThrow( | ||
'Both str and fragment must be strings' | ||
); | ||
}); | ||
|
||
it('is case insensitive', () => { | ||
expect(accentFolder.highlightMatch('FULANILO LÓPEZ', 'lo')).toBe( | ||
'FULANI<b>LO</b> <b>LÓ</b>PEZ' | ||
); | ||
}); | ||
it('should throw TypeError if wrapTag is not a string', () => { | ||
expect(() => accentFolder.highlightMatch('test', 'es', 123)).toThrow( | ||
TypeError | ||
); | ||
expect(() => accentFolder.highlightMatch('test', 'es', 123)).toThrow( | ||
'wrapTag must be a string' | ||
); | ||
}); | ||
|
||
it('handles empty strings', () => { | ||
expect(accentFolder.highlightMatch('', 'test')).toBe(''); | ||
expect(accentFolder.highlightMatch('Test', '')).toBe('Test'); | ||
}); | ||
it('should recognize simple accents', () => { | ||
expect(accentFolder.highlightMatch('Fulanilo López', 'lo')).toBe( | ||
'Fulani<b>lo</b> <b>Ló</b>pez' | ||
); | ||
expect(accentFolder.highlightMatch('Erik Lørgensen', 'lo')).toBe( | ||
'Erik <b>Lø</b>rgensen' | ||
); | ||
expect(accentFolder.highlightMatch('James Lö', 'lo')).toBe( | ||
'James <b>Lö</b>' | ||
); | ||
}); | ||
|
||
it('returns original string when no match is found', () => { | ||
expect(accentFolder.highlightMatch('Hello World', 'xyz')).toBe( | ||
'Hello World' | ||
); | ||
it("should wrap matched fragment with custom tag added in second parameter '<strong>'", () => { | ||
expect( | ||
accentFolder.highlightMatch('Fulanilo López', 'lo', 'strong') | ||
).toEqual('Fulani<strong>lo</strong> <strong>Ló</strong>pez'); | ||
}); | ||
|
||
it('wraps matched fragment with custom tag', () => { | ||
expect( | ||
accentFolder.highlightMatch('Fulanilo López', 'lo', 'strong') | ||
).toBe('Fulani<strong>lo</strong> <strong>Ló</strong>pez'); | ||
}); | ||
|
||
it('is case insensitive', () => { | ||
expect(accentFolder.highlightMatch('FULANILO LÓPEZ', 'lo')).toBe( | ||
'FULANI<b>LO</b> <b>LÓ</b>PEZ' | ||
); | ||
}); | ||
|
||
it('handles empty strings', () => { | ||
expect(accentFolder.highlightMatch('', 'test')).toBe(''); | ||
expect(accentFolder.highlightMatch('Test', '')).toBe('Test'); | ||
}); | ||
|
||
it('returns original string when no match is found', () => { | ||
expect(accentFolder.highlightMatch('Hello World', 'xyz')).toBe( | ||
'Hello World' | ||
); | ||
}); | ||
|
||
it('handles multiple matches', () => { | ||
expect(accentFolder.highlightMatch('lólá lòlã', 'la')).toBe( | ||
'ló<b>lá</b> lò<b>lã</b>' | ||
); | ||
}); | ||
|
||
it('handles special characters in fragment', () => { | ||
expect(accentFolder.highlightMatch('a+b=c', '+')).toBe('a<b>+</b>b=c'); | ||
}); | ||
|
||
// it('preserves HTML in original string', () => { | ||
// expect(accentFold.highlightMatch("<p>Héllo</p>", "he")).toBe("<p><b>Hé</b>llo</p>"); | ||
// }); | ||
}); | ||
|
||
it('handles multiple matches', () => { | ||
expect(accentFolder.highlightMatch('lólá lòlã', 'la')).toBe( | ||
'ló<b>lá</b> lò<b>lã</b>' | ||
describe('replace', () => { | ||
it('should throw TypeError if input is not a string', () => { | ||
expect(() => accentFolder.replace(123)).toThrow(TypeError); | ||
expect(() => accentFolder.replace(123)).toThrow('Input must be a string'); | ||
}); | ||
|
||
it.each(Object.entries(accentMap))( | ||
'should replace %s with %s', | ||
(accentedChar, expectedChar) => { | ||
expect(accentFolder.replace(accentedChar)).toBe(expectedChar); | ||
} | ||
); | ||
}); | ||
it('should recognize simple accents', () => { | ||
expect(accentFolder.replace('naïve')).toBe('naive'); | ||
}); | ||
|
||
it('handles special characters in fragment', () => { | ||
expect(accentFolder.highlightMatch('a+b=c', '+')).toBe('a<b>+</b>b=c'); | ||
}); | ||
it('should replace multiple accented characters', () => { | ||
expect(accentFolder.replace('résumé')).toBe('resume'); | ||
}); | ||
|
||
// it('preserves HTML in original string', () => { | ||
// expect(accentFold.highlightMatch("<p>Héllo</p>", "he")).toBe("<p><b>Hé</b>llo</p>"); | ||
// }); | ||
it('should handle mixed accented and non-accented text', () => { | ||
expect(accentFolder.replace('Café au lait')).toBe('Cafe au lait'); | ||
}); | ||
it('should return the same string if no accented characters are present', () => { | ||
expect(accentFolder.replace('hello world')).toBe('hello world'); | ||
}); | ||
|
||
it('should handle empty string', () => { | ||
expect(accentFolder.replace('')).toBe(''); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.