ashtuchkin / iconv-lite

Convert character encodings in pure javascript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mechanism to add encodings from external npm packages

ashtuchkin opened this issue · comments

Some encodings are very rare (e.g. utf7 and iso-2022-jp #60), so it's always a hard decision to include them in iconv-lite, as they add space and memory requirements. To make this choice simpler, it would be nice to add an extension mechanism that would make it easy to add them as separate npm packages.

Something like this:

const iconv = require("iconv-lite");
iconv.addEncoding(require("encoding-iso-2022-jp"));

What we'll need:

  • Solidify & publish codec interface.
  • Solidify & publish backend interface.
  • Add addEncoding() function. It will by default check that the new package does not override any existing encodings/aliases. This behavior can be disabled by passing {replace: true} as a second arg. Function should be idempotent.
  • Extract utf7 as an example.
  • Create a test harness that would help authors make sure their codecs work on the whole range of supported environments.

Question: would it be better to use immutable-like interface iconv = iconv.withEncoding(require('...'));? It would help with testability, but overall I think it's not worth it, as users will have to either do it in every file that needs iconv, or pass it around in some kind of a global.