diff --git a/README.md b/README.md
index 763bbc2..b5fd061 100644
--- a/README.md
+++ b/README.md
@@ -75,6 +75,25 @@ Use the 3d argument to specify the wrapping html tag (strong, em, span etc.):
af.highlightMatch('Fulanilo López', 'lo', 'strong'); // --> "Fulanilo López"
```
+
+## Extending and Overriding the Accent Map
+
+The `AccentFolding` class allows you to extend or override the default accent map by providing a custom map to the constructor.
+
+### Extending the Accent Map
+
+To extend the accent map with new mappings, pass an object with the new mappings to the constructor. For example:
+
+```js
+import AccentFolding from './accentFolding.js';
+
+const customAccentMap = { 'ö': 'oe', '✝': 't' };
+const accentFolder = new AccentFolding(customAccentMap);
+
+console.log(accentFolder.replace('Föhn')); // Outputs: Foehn
+console.log(accentFolder.replace('✝illa')); // Outputs: tilla
+```
+
## Requirements
Node.js version 14.7 or higher
diff --git a/src/accentFolding.js b/src/accentFolding.js
index d2a8e68..df772da 100644
--- a/src/accentFolding.js
+++ b/src/accentFolding.js
@@ -4,11 +4,17 @@ class AccentFolding {
#cache;
#accentMap;
- constructor() {
+ constructor(newMap = null) {
+ if (newMap !== null && typeof newMap !== 'object') {
+ throw new TypeError('newMap must be an object');
+ }
this.#accentMap = new Map([
...AccentFolding.convertAccentMapToArray(defaultAccentMap),
]);
this.#cache = new Map();
+ if (newMap) {
+ this.#extendAccentMap(newMap);
+ }
}
#fold(s) {
@@ -78,6 +84,13 @@ class AccentFolding {
static convertAccentMapToArray(accentMap) {
return Object.entries(accentMap);
}
+
+ #extendAccentMap(newMap) {
+ for (const [key, value] of Object.entries(newMap)) {
+ this.#accentMap.set(key, value);
+ }
+ this.#cache.clear(); // Clear cache to ensure new mappings are used
+ }
}
export default AccentFolding;
diff --git a/src/accentFolding.js.test.js b/src/accentFolding.js.test.js
index 61307eb..05e7cf4 100644
--- a/src/accentFolding.js.test.js
+++ b/src/accentFolding.js.test.js
@@ -120,4 +120,26 @@ describe('AccentFolding', () => {
expect(accentFolder.replace('')).toBe('');
});
});
+
+ describe('constructor', () => {
+ it('should initialize with default accent map', () => {
+ expect(accentFolder.replace('á')).toBe('a');
+ });
+
+ it('should extend the accent map with new mappings if provided', () => {
+ const customAccentFolder = new AccentFolding({ ö: 'oe', '✝': 't' });
+ expect(customAccentFolder.replace('Föhn')).toBe('Foehn');
+ expect(customAccentFolder.replace('✝illa')).toBe('tilla');
+ });
+
+ it('should override existing mappings if provided', () => {
+ const customAccentFolder = new AccentFolding({ á: 'aa' });
+ expect(customAccentFolder.replace('á')).toBe('aa');
+ });
+
+ it('should throw TypeError if newMap is not an object', () => {
+ expect(() => new AccentFolding(123)).toThrow(TypeError);
+ expect(() => new AccentFolding(123)).toThrow('newMap must be an object');
+ });
+ });
});