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: add constructor param to extend/override the accent map #15

Merged
merged 1 commit into from
Aug 22, 2024
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
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');
});
});
});