dundalek / latinize

Simple library to convert accents (diacritics) from strings to latin characters.
BSD 2-Clause "Simplified" License
148 stars 33 forks source link

How to use multiple instances? #16

Closed bluepuma77 closed 11 months ago

bluepuma77 commented 3 years ago

I need to work with two different translation tables:

One translates only non-German characters not included in the PDF embedded font: const umlauts = {'Ä': 'Ä', 'Ö': 'Ö', 'Ü': 'Ü', 'ä': 'ä', 'ö': 'ö', 'ü': 'ü', 'ß': 'ß'};

Another one is used to make search easier, it translates German characters: const umlauts = {'Ä': 'Ae', 'Ö': 'Oe', 'Ü': 'Ue', 'ä': 'ae', 'ö': 'oe', 'ü': 'ue', 'ß': 'ss'};

I am using typescript and did import latinize from 'latinize'; in two different files, but changing latinize.characters always changes the values in both instances. Using var latinize = require('latinize'); did not help on the high level, only when put into the function using the translation. But now it is initialised new every time.

Is it possible to use multiple instances at the same time?

dundalek commented 11 months ago

It is now possible to pass the mapping explicitly as a second argument:

const characters = {
  ...latinize.characters,
  'Ä': 'Ae', 'Ö': 'Oe', 'Ü': 'Ue', 'ä': 'ae', 'ö': 'oe', 'ü': 'ue'
};
latinize('ÄÖ', characters) // => "AeOe"