Closed btsimonh closed 4 years ago
Thanks for writing this; I've added your description at https://github.com/ashtuchkin/iconv-lite/wiki/Modifying-encoding-tables.
As for making getCodec(undefined)
not raise exceptions - I think you're right, it's better to provide a separate function to load the encodings. I'll think how to implement that.
My pleasure, and thanks for a nice repo :).
Hi,
I had reasons to need to convert BIG5 coded DBCS, but include private use characters in the conversion (0xfa40+ -> \UE000+). Since the 'big5' table here is actually HKSCS, which conflicts with my codes, I needed to create a new DBCS table, and thought I would document the process here.
In order to add a DBCS table based on another, you need to do a few things: 1/ you need to call iconv.getCodec(); so that iconv.encodings exists. 2/ create a table (or extra parts you want to add to a table). 3/ create a new encoding definition (like in dbcs-data.js). Note now I based it on a previous table (cp950) without having to directly require the relevant table file - requiring was difficult because of paths. 4/ Add the new defn directly to iconv.encodings. 5/ use your sparkly new table :).
Example code snippet:
Result is:
Note: I am not suggesting this is a good rendition of plain old Big5 - more work to do to analyse that, but it illustrates how to abuse iconv-lite to do 'special' encodings without forking. Note2: If (like me) you have a large and complex project where this code COULD exist more than once with different values, or the values are dynamic in some way, be aware of caching inside iconv-lite.
It may be a nice mod to make getCodec(undefined) not except, or provide another function which loads the encodings, but not necessary. Calling encodingExists(undefined) would actually do this. Also the addition of a cache reset function would be nice, although i'm sure setting iconv._codecDataCache = {} would work fine.
Maybe this could go in the wiki?
Simon