iconv-lite
Advanced tools
Comparing version 0.1.0 to 0.1.1
70
index.js
@@ -116,8 +116,74 @@ // Module exports | ||
}, | ||
// Codepage double-byte encodings. | ||
table: function(options) { | ||
if (!options.table) { | ||
throw new Error("Encoding '" + options.type +"' has incorect 'table' option"); | ||
} | ||
var table = options.table, key, | ||
revCharsTable = {}; | ||
for (key in table) { | ||
revCharsTable[table[key]] = parseInt(key); | ||
} | ||
return { | ||
toEncoding: function(str) { | ||
str = ensureString(str); | ||
var len = 0, strLen = str.length; | ||
for (var i = 0; i < strLen; i++) { | ||
if (!!(str.charCodeAt(i) >> 8)) { | ||
len += 2; | ||
} else { | ||
len ++; | ||
} | ||
} | ||
var newBuf = new Buffer(len); | ||
for (var i = 0, j = 0; i < strLen; i++) { | ||
var unicode = str.charCodeAt(i); | ||
if (!!(unicode >> 7)) { | ||
var gbkcode = revCharsTable[unicode] || 0xA1F0;//not found in table ,replace it | ||
newBuf[j++] = gbkcode >> 8;//high byte; | ||
newBuf[j++] = gbkcode & 0xFF;//low byte | ||
} else {//ascii | ||
newBuf[j++] = unicode; | ||
} | ||
} | ||
return newBuf; | ||
}, | ||
fromEncoding: function(buf) { | ||
buf = ensureBuffer(buf); | ||
var idx = 0, len = 0, | ||
newBuf = new Buffer(len*2),unicode,gbkcode; | ||
for (var i = 0, _len = buf.length; i < _len; i++, len++) { | ||
if (!!(buf[i] & 0x80)) {//the high bit is 1, so this byte is gbkcode's high byte.skip next byte | ||
i++; | ||
} | ||
} | ||
var newBuf = new Buffer(len*2); | ||
for (var i = 0, j = 0, _len = buf.length; i < _len; i++, j++) { | ||
var temp = buf[i], gbkcode, unicode; | ||
if (temp & 0x80) { | ||
gbkcode = (temp << 8) + buf[++i]; | ||
unicode = table[gbkcode] || iconv.defaultCharUnicode.charCodeAt(0);//not found in table, replace with defaultCharUnicode | ||
}else { | ||
unicode = temp; | ||
} | ||
newBuf[j*2] = unicode & 0xFF;//low byte | ||
newBuf[j*2+1] = unicode >> 8;//high byte | ||
} | ||
return newBuf.toString('ucs2'); | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
// Add aliases to convert functions | ||
iconv.encode = iconv.toEncoding; | ||
iconv.decode = iconv.fromEncoding; | ||
// Load other encodings from files in /encodings dir. | ||
var encodingsDir = __dirname+"/encodings/"; | ||
require('fs').readdirSync(encodingsDir).forEach(function(file) { | ||
var encodingsDir = __dirname+"/encodings/", | ||
fs = require('fs'); | ||
fs.readdirSync(encodingsDir).forEach(function(file) { | ||
if(fs.statSync(encodingsDir + file).isDirectory()) return; | ||
var encodings = require(encodingsDir + file) | ||
@@ -124,0 +190,0 @@ for (var key in encodings) |
{ | ||
"name": "iconv-lite", | ||
"description": "Convert character encodings in pure javascript.", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"keywords": ["iconv", "convert", "charset"], | ||
"author": "Alexander Shtuchkin <ashtuchkin@gmail.com>", | ||
"contributors": [ | ||
"jenkinv (https://github.com/jenkinv)" | ||
], | ||
@@ -18,4 +21,5 @@ "homepage": "http://github.com/ashtuchkin/node-iconv/", | ||
"devDependencies": { | ||
"vows": "" | ||
"vows": "", | ||
"iconv": "" | ||
} | ||
} |
@@ -9,8 +9,11 @@ iconv-lite - native javascript conversion between character encodings. | ||
// Convert from an encoded buffer to string. | ||
str = icon.fromEncoding(buf, 'win-1251'); | ||
str = iconv.fromEncoding(buf, 'win-1251'); | ||
// Or | ||
str = iconv.decode(buf, 'win-1251'); | ||
// Convert from string to an encoded buffer. | ||
buf = iconv.toEncoding("Sample input string", 'win-1251'); | ||
// Or | ||
buf = iconv.encode("Sample input string", 'win-1251'); | ||
## Supported encodings | ||
@@ -21,4 +24,5 @@ | ||
* All node.js native encodings: 'utf8', 'ucs2', 'ascii', 'binary', 'base64'. | ||
* 'latin1' | ||
* Base encodings: 'latin1' | ||
* Cyrillic encodings: 'windows-1251', 'koi8-r', 'iso 8859-5'. | ||
* Simplified chinese: 'gbk', 'gb2313'. | ||
@@ -47,1 +51,8 @@ Other encodings are easy to add, see the source. Please, participate. | ||
vows | ||
## TODO | ||
* Support streaming character conversion, something like util.pipe(req, iconv.fromEncodingStream('latin1')). | ||
* Add more encodings. | ||
* Add transliteration (best fit char). | ||
* Add tests and correct support of variable-byte encodings (currently work is delegated to node). |
@@ -51,2 +51,6 @@ var vows = require('vows'), | ||
}, | ||
"Aliases encode and decode work the same as toEncoding and fromEncoding": function() { | ||
assert.strictEqual(iconv.toEncoding(testString, "latin1").toString("binary"), iconv.encode(testString, "latin1").toString("binary")); | ||
assert.strictEqual(iconv.fromEncoding(testStringLatin1, "latin1"), iconv.decode(testStringLatin1, "latin1")); | ||
}, | ||
}).export(module) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
327909
16
398
56
2
7