diff --git a/encodings/ascii-compat-generated.js b/encodings/ascii-compat-generated.js new file mode 100644 index 0000000..25580ac --- /dev/null +++ b/encodings/ascii-compat-generated.js @@ -0,0 +1,381 @@ + +// Generated data for ASCII-compatible codecs. Don't edit manually. Regenerate using generation/gen-ascii-compat.js script. +module.exports = { + "437": true, + "737": true, + "775": true, + "808": true, + "850": true, + "852": true, + "855": true, + "856": true, + "857": true, + "858": true, + "860": true, + "861": true, + "862": true, + "863": true, + "865": true, + "866": true, + "869": true, + "874": true, + "922": true, + "932": true, + "936": true, + "949": true, + "950": true, + "1046": true, + "1124": true, + "1125": true, + "1129": true, + "1133": true, + "1161": true, + "1162": true, + "1163": true, + "1250": true, + "1251": true, + "1252": true, + "1253": true, + "1254": true, + "1255": true, + "1256": true, + "1257": true, + "1258": true, + "10000": true, + "10006": true, + "10007": true, + "10029": true, + "10079": true, + "10081": true, + "20866": true, + "21866": true, + "28591": true, + "28592": true, + "28593": true, + "28594": true, + "28595": true, + "28596": true, + "28597": true, + "28598": true, + "28599": true, + "28600": true, + "28601": true, + "28603": true, + "28604": true, + "28605": true, + "28606": true, + "ansix3.4": true, + "ansix3.41968": true, + "ansix3.41986": true, + "arabic": true, + "arabic8": true, + "armscii8": true, + "ascii": true, + "ascii8bit": true, + "asmo708": true, + "base64": true, + "big5": true, + "big5hkscs": true, + "binary": true, + "celtic": true, + "celtic8": true, + "cesu8": true, + "chinese": true, + "cnbig5": true, + "cp1046": true, + "cp1124": true, + "cp1125": true, + "cp1129": true, + "cp1133": true, + "cp1161": true, + "cp1162": true, + "cp1163": true, + "cp1250": true, + "cp1251": true, + "cp1252": true, + "cp1253": true, + "cp1254": true, + "cp1255": true, + "cp1256": true, + "cp1257": true, + "cp1258": true, + "cp20866": true, + "cp21866": true, + "cp28591": true, + "cp28592": true, + "cp28593": true, + "cp28594": true, + "cp28595": true, + "cp28596": true, + "cp28597": true, + "cp28598": true, + "cp28599": true, + "cp28600": true, + "cp28601": true, + "cp28603": true, + "cp28604": true, + "cp28605": true, + "cp28606": true, + "cp367": true, + "cp437": true, + "cp737": true, + "cp775": true, + "cp808": true, + "cp819": true, + "cp850": true, + "cp852": true, + "cp855": true, + "cp856": true, + "cp857": true, + "cp858": true, + "cp860": true, + "cp861": true, + "cp862": true, + "cp863": true, + "cp865": true, + "cp866": true, + "cp869": true, + "cp874": true, + "cp922": true, + "cp932": true, + "cp936": true, + "cp949": true, + "cp950": true, + "cpgr": true, + "csascii": true, + "csbig5": true, + "cseuckr": true, + "csgb2312": true, + "cshproman8": true, + "csibm1046": true, + "csibm1124": true, + "csibm1125": true, + "csibm1129": true, + "csibm1133": true, + "csibm1161": true, + "csibm1162": true, + "csibm1163": true, + "csibm437": true, + "csibm737": true, + "csibm775": true, + "csibm850": true, + "csibm852": true, + "csibm855": true, + "csibm856": true, + "csibm857": true, + "csibm858": true, + "csibm860": true, + "csibm861": true, + "csibm862": true, + "csibm863": true, + "csibm865": true, + "csibm866": true, + "csibm869": true, + "csibm922": true, + "csiso58gb231280": true, + "csisolatin1": true, + "csisolatin2": true, + "csisolatin3": true, + "csisolatin4": true, + "csisolatin5": true, + "csisolatin6": true, + "csisolatinarabic": true, + "csisolatincyrillic": true, + "csisolatingreek": true, + "csisolatinhebrew": true, + "cskoi8r": true, + "csksc56011987": true, + "csmacintosh": true, + "cspc775baltic": true, + "cspc850multilingual": true, + "cspc862latinhebrew": true, + "cspc8codepage437": true, + "cspcp852": true, + "csshiftjis": true, + "cyrillic": true, + "ecma114": true, + "ecma118": true, + "elot928": true, + "euccn": true, + "eucjp": true, + "euckr": true, + "gb18030": true, + "gb2312": true, + "gb23121980": true, + "gb231280": true, + "gbk": true, + "georgianacademy": true, + "georgianps": true, + "greek": true, + "greek8": true, + "hebrew": true, + "hebrew8": true, + "hex": true, + "hproman8": true, + "ibm1046": true, + "ibm1051": true, + "ibm1124": true, + "ibm1125": true, + "ibm1129": true, + "ibm1133": true, + "ibm1161": true, + "ibm1162": true, + "ibm1163": true, + "ibm1168": true, + "ibm367": true, + "ibm437": true, + "ibm737": true, + "ibm775": true, + "ibm808": true, + "ibm819": true, + "ibm850": true, + "ibm852": true, + "ibm855": true, + "ibm856": true, + "ibm857": true, + "ibm858": true, + "ibm860": true, + "ibm861": true, + "ibm862": true, + "ibm863": true, + "ibm865": true, + "ibm866": true, + "ibm869": true, + "ibm878": true, + "ibm922": true, + "iso646.irv": true, + "iso646us": true, + "iso88591": true, + "iso885910": true, + "iso885911": true, + "iso885913": true, + "iso885914": true, + "iso885915": true, + "iso885916": true, + "iso88592": true, + "iso88593": true, + "iso88594": true, + "iso88595": true, + "iso88596": true, + "iso88597": true, + "iso88598": true, + "iso88599": true, + "isoceltic": true, + "isoir100": true, + "isoir101": true, + "isoir109": true, + "isoir110": true, + "isoir126": true, + "isoir127": true, + "isoir138": true, + "isoir144": true, + "isoir148": true, + "isoir149": true, + "isoir157": true, + "isoir166": true, + "isoir179": true, + "isoir199": true, + "isoir203": true, + "isoir226": true, + "isoir58": true, + "isoir6": true, + "koi8r": true, + "koi8ru": true, + "koi8t": true, + "koi8u": true, + "korean": true, + "ksc5601": true, + "ksc56011987": true, + "ksc56011989": true, + "l1": true, + "l10": true, + "l2": true, + "l3": true, + "l4": true, + "l5": true, + "l6": true, + "l7": true, + "l8": true, + "l9": true, + "latin1": true, + "latin10": true, + "latin2": true, + "latin3": true, + "latin4": true, + "latin5": true, + "latin6": true, + "latin7": true, + "latin8": true, + "latin9": true, + "mac": true, + "maccenteuro": true, + "maccroatian": true, + "maccyrillic": true, + "macgreek": true, + "maciceland": true, + "macintosh": true, + "macroman": true, + "macromania": true, + "macthai": true, + "macturkish": true, + "macukraine": true, + "msansi": true, + "msarab": true, + "mscyrl": true, + "msee": true, + "msgreek": true, + "mshebr": true, + "mskanji": true, + "msturk": true, + "pt154": true, + "r8": true, + "rk1048": true, + "roman8": true, + "shiftjis": true, + "sjis": true, + "strk10482002": true, + "thai": true, + "thai8": true, + "tis620": true, + "tis620.25291": true, + "tis620.25330": true, + "tis6200": true, + "turkish": true, + "turkish8": true, + "ucs2": true, + "unicode11utf8": true, + "us": true, + "usascii": true, + "utf16le": true, + "utf8": true, + "win1250": true, + "win1251": true, + "win1252": true, + "win1253": true, + "win1254": true, + "win1255": true, + "win1256": true, + "win1257": true, + "win1258": true, + "win874": true, + "winbaltrim": true, + "windows1250": true, + "windows1251": true, + "windows1252": true, + "windows1253": true, + "windows1254": true, + "windows1255": true, + "windows1256": true, + "windows1257": true, + "windows1258": true, + "windows31j": true, + "windows874": true, + "windows932": true, + "windows936": true, + "windows949": true, + "windows950": true, + "xgbk": true, + "xroman8": true, + "xsjis": true, + "xxbig5": true +} \ No newline at end of file diff --git a/generation/gen-ascii-compat.js b/generation/gen-ascii-compat.js new file mode 100644 index 0000000..c042857 --- /dev/null +++ b/generation/gen-ascii-compat.js @@ -0,0 +1,58 @@ +var fs = require("fs"), + path = require("path"); + +var destFileName = "encodings/ascii-compat-generated.js"; + +var encodings = require(path.join(__dirname, "..", "encodings")), + names = Object.keys(encodings), + // start off with some known ASCII-compatible charsets that are supported + // internally + compat = [ "utf8", "unicode11utf8", "ascii" ]; + +for (var i = 0, len = names.length, enc; i < len; ++i) { + enc = names[i]; + if (~compat.indexOf(enc) || typeof encodings[enc] === "function") + continue; + + var done = false, + nextenc = enc, + opts; + while (!done) { + var codec = encodings[nextenc]; + + switch (typeof codec) { + case "string": // Direct alias to other encoding. + nextenc = codec; + break; + + case "object": // Alias with options. Can be layered. + if (!opts) { + opts = codec; + opts.encodingName = nextenc; + } else { + for (var key in codec) + opts[key] = codec[key]; + } + + nextenc = codec.type; + break; + + default: + done = true; + } + } + + if (opts && opts.chars && opts.chars.length === 128) + compat.push(enc); +} + +compat.sort(); + +encodings = {}; +for (var i = 0, len = compat.length; i < len; ++i) + encodings[compat[i]] = true; + +// Write encodings. +fs.writeFileSync(path.join(__dirname, "..", destFileName), + "\n// Generated data for ASCII-compatible codecs. Don't edit manually. Regenerate using generation/gen-ascii-compat.js script.\n"+ + "module.exports = "+JSON.stringify(encodings, undefined, " ")); diff --git a/lib/index.js b/lib/index.js index 0775589..6319c80 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,8 @@ iconv.encodings = null; iconv.defaultCharUnicode = '�'; iconv.defaultCharSingleByte = '?'; +var RE_STRIP_CHARS = /[^0-9a-z]|:\d{4}$/g; + // Public API. iconv.encode = function encode(str, encoding, options) { str = "" + (str || ""); // Ensure string. @@ -59,7 +61,7 @@ iconv.getCodec = function getCodec(encoding) { iconv.encodings = require("../encodings"); // Lazy load all encoding definitions. // Canonicalize encoding name: strip all non-alphanumeric chars and appended year. - var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, ""); + var enc = (''+encoding).toLowerCase().replace(RE_STRIP_CHARS, ""); // Traverse iconv.encodings to find actual codec. var codecData, codecOptions; @@ -106,6 +108,14 @@ iconv.getCodec = function getCodec(encoding) { } } +var ASCII_COMPAT_MAP; +iconv.asciiCompatible = function asciiCompatible(enc) { + if (!ASCII_COMPAT_MAP) + ASCII_COMPAT_MAP = require('../encodings/ascii-compat-generated'); + enc = enc.toLowerCase().replace(RE_STRIP_CHARS, ''); + return (ASCII_COMPAT_MAP[enc] || false); +}; + // Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json. var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node; if (nodeVer) {