Skip to content
Browse files

Add examples, new PI and tests

  • Loading branch information...
1 parent 1dfa478 commit 4016a98199d9bb4e93b603f08668a0f7bf6008b6 @dinhoabreu committed Mar 18, 2012
Showing with 61 additions and 6 deletions.
  1. +5 −0 README.md
  2. +5 −0 examples/ex01.js
  3. +24 −0 index.js
  4. +1 −1 package.json
  5. +26 −5 test/index.js
View
5 README.md
@@ -47,3 +47,8 @@ Encode from one character encoding to another:
var str = 'aéióuçÁEÍOÚC';
var ascii = codes.convert(str, 'ASCII//TRANSLIT', 'UTF-8');
console.log(str + ' => ' + ascii.toString());
+
+ // convert from UTF-8 to UTF-16LE
+ var cds = codes.create('utf16le', 'utf8');
+ var utf16 = cds.convert(new Buffer(str));
+ console.log(utf16);
View
5 examples/ex01.js
@@ -35,3 +35,8 @@ cs.end();
var str = 'aéióuçÁEÍOÚC';
var ascii = codes.convert(str, 'ASCII//TRANSLIT', 'UTF-8');
console.log(str + ' => ' + ascii.toString());
+
+// convert from UTF-8 to UTF-16LE
+var cds = codes.create('utf16le', 'utf8');
+var utf16 = cds.convert(new Buffer(str));
+console.log(utf16);
View
24 index.js
@@ -144,10 +144,34 @@ CodesStream.prototype._convert = function (input) {
return input;
}
+function Codes(to, from, options) {
+ var h = create(to, from, options);
+ this.toCode = h.to;
+ this.fromCode = h.from;
+ this.size = h.size;
+ this._iconv = h.iconv;
+}
+
+Codes.prototype.convert = function (input) {
+ var output = new Buffer(input.length * 4),
+ r = this._iconv.convert(input, output);
+ if (r.code == -1)
+ throw new Error(r.error);
+ return output.slice(0, r.offsetOut);
+}
+
exports.CodesStream = CodesStream;
+
+exports.Codes = Codes;
+
exports.createStream = function (to, from, options) {
return new CodesStream(to, from, options);
}
+
+exports.create = function (to, from, options) {
+ return new Codes(to, from, options);
+}
+
exports.convert = function (input, to, from, options) {
options = options || {};
options.size = Math.ceil(input.length * 4 / 8192) * 8192;
View
2 package.json
@@ -4,7 +4,7 @@
"description": "Node.js iconv bindings - Streamable, fast and lightweight",
"keywords": "iconv charset encoding encode decode convert code codes stream fast ligthweight",
"homepage": "https://github.com/dinhoabreu/codes",
- "version": "0.1.1",
+ "version": "0.1.2",
"repository": {
"type": "git",
"url": "git@github.com:dinhoabreu/codes.git"
View
31 test/index.js
@@ -37,7 +37,7 @@ vows.describe('Convert charset by Codes').addBatch({
});
return promise;
},
- "we get a Error": function (error, data) {
+ "we got a Error": function (error, data) {
assert.isNotNull(error);
assert.isUndefined(data);
}
@@ -61,7 +61,7 @@ vows.describe('Convert charset by Codes').addBatch({
});
return promise;
},
- "we get 'a'e'i'o'uc": function (error, data) {
+ "we got 'a'e'i'o'uc": function (error, data) {
assert.isNull(error);
assert.equal(data.toString(), "'a'e'i'o'uc");
}
@@ -87,7 +87,7 @@ vows.describe('Convert charset by Codes').addBatch({
});
return promise;
},
- "we get hex('e1e9edf3fae7')": function (error, data) {
+ "we got hex('e1e9edf3fae7')": function (error, data) {
assert.isNull(error);
assert.equal(data.toString('hex'), 'e1e9edf3fae7');
}
@@ -111,7 +111,7 @@ vows.describe('Convert charset by Codes').addBatch({
});
return promise;
},
- "we get hex('e1e9edf3fae7')": function (error, data) {
+ "we got hex('e1e9edf3fae7')": function (error, data) {
assert.isNull(error);
assert.equal(data.toString('hex'), 'e1e9edf3fae7');
}
@@ -135,10 +135,31 @@ vows.describe('Convert charset by Codes').addBatch({
reader.pipe(stream);
return promise;
},
- "we get hex('feff00e100e900ed00f300fa000a00e7000a')": function (error, data) {
+ "we got hex('feff00e100e900ed00f300fa000a00e7000a')": function (error, data) {
assert.isNull(error);
assert.equal(data.toString('hex'), 'feff00e100e900ed00f300fa000a00e7000a');
}
}
+ },
+ 'Test Big Buffer - UTF-8 to UTF-16LE': {
+ topic: function () {
+ var c = codes.create('UTF-16LE', 'UTF-8');
+ var utf8 = new Buffer(20000000);
+ for (var i = 0; i < utf8.length; i++) {
+ utf8[i] = 97 + i % 26; // cycle from 'a' to 'z'.
+ }
+ var utf16 = c.convert(utf8);
+ return [utf8, utf16];
+ },
+ 'we got a double length': function (a) {
+ assert.equal(a[1].length, a[0].length * 2);
+ },
+ 'we got the same characters': function (a) {
+ var utf8 = a[0], utf16 = a[1];
+ for (i = 0; i < utf8.length; ++i) {
+ assert.equal(utf16[i * 2], utf8[i]);
+ assert.equal(utf16[i * 2 + 1], 0);
+ }
+ }
}
}).export(module);

0 comments on commit 4016a98

Please sign in to comment.
Something went wrong with that request. Please try again.