Skip to content

Commit

Permalink
Merge pull request #15 from zr87/feat/extending-and-overriding-accent…
Browse files Browse the repository at this point in the history
…-map

feat: add constructor param to extend/override the accent map
  • Loading branch information
ZRktty authored Aug 22, 2024
2 parents 05ab22e + a165fa1 commit af9fb89
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'); // --> "Fulani<strong>lo</strong> <strong>Ló</strong>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
Expand Down
15 changes: 14 additions & 1 deletion src/accentFolding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
22 changes: 22 additions & 0 deletions src/accentFolding.js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit af9fb89

Please sign in to comment.