+29
| var testCase = require('nodeunit').testCase, | ||
| encoding = require("./index"); | ||
| exports["General tests"] = { | ||
| "From UTF-8 to Latin_1": function(test){ | ||
| var input = "ÕÄÖÜ", | ||
| expected = new Buffer([0xd5, 0xc4, 0xd6, 0xdc]); | ||
| test.deepEqual(encoding.convert(input, "latin1"), expected); | ||
| test.done(); | ||
| }, | ||
| "From Latin_1 to UTF-8": function(test){ | ||
| var input = new Buffer([0xd5, 0xc4, 0xd6, 0xdc]), | ||
| expected = "ÕÄÖÜ"; | ||
| test.deepEqual(encoding.convert(input, "utf-8", "latin1").toString(), expected); | ||
| test.done(); | ||
| }, | ||
| "From Latin_13 to Latin_15": function(test){ | ||
| var input = new Buffer([0xd5, 0xc4, 0xd6, 0xdc, 0xd0]), | ||
| expected = new Buffer([0xd5, 0xc4, 0xd6, 0xdc, 0xA6]); | ||
| test.deepEqual(encoding.convert(input, "latin_15", "latin13"), expected); | ||
| test.done(); | ||
| }, | ||
| "From Latin_13 to Latin_15 lite": function(test){ | ||
| var input = new Buffer([0xd5, 0xc4, 0xd6, 0xdc, 0xd0]), | ||
| expected = new Buffer([0xd5, 0xc4, 0xd6, 0xdc, 0xA6]); | ||
| test.deepEqual(encoding.convert(input, "latin_15", "latin13", true), expected); | ||
| test.done(); | ||
| } | ||
| } |
+33
-4
@@ -10,3 +10,12 @@ var Iconv, iconvLite = require("iconv-lite");; | ||
| function convert(str, to, from){ | ||
| /** | ||
| * Convert encoding of an UTF-8 string or a buffer | ||
| * | ||
| * @param {String|Buffer} str String to be converted | ||
| * @param {String} to Encoding to be converted to | ||
| * @param {String} [from="UTF-8"] Encoding to be converted from | ||
| * @param {Boolean} useLite If set to ture, force to use iconvLite | ||
| * @return {Buffer} Encoded string | ||
| */ | ||
| function convert(str, to, from, useLite){ | ||
| from = checkEncoding(from || "UTF-8"); | ||
@@ -25,7 +34,5 @@ to = checkEncoding(to || "UTF-8"); | ||
| }else{ | ||
| if(Iconv){ | ||
| console.log(1); | ||
| if(Iconv && !useLite){ | ||
| result = convertIconv(str, to, from); | ||
| }else{ | ||
| console.log(2); | ||
| result = convertIconvLite(str, to, from); | ||
@@ -42,2 +49,10 @@ } | ||
| /** | ||
| * Convert encoding of astring with node-iconv (if available) | ||
| * | ||
| * @param {String|Buffer} str String to be converted | ||
| * @param {String} to Encoding to be converted to | ||
| * @param {String} [from="UTF-8"] Encoding to be converted from | ||
| * @return {Buffer} Encoded string | ||
| */ | ||
| function convertIconv(str, to, from){ | ||
@@ -50,2 +65,10 @@ var response; | ||
| /** | ||
| * Convert encoding of astring with iconv-lite (if node-iconv is not available) | ||
| * | ||
| * @param {String|Buffer} str String to be converted | ||
| * @param {String} to Encoding to be converted to | ||
| * @param {String} [from="UTF-8"] Encoding to be converted from | ||
| * @return {Buffer} Encoded string | ||
| */ | ||
| function convertIconvLite(str, to, from){ | ||
@@ -61,2 +84,8 @@ if(to == "UTF-8"){ | ||
| /** | ||
| * Converts charset name if needed | ||
| * | ||
| * @param {String} name Character set | ||
| * @return {String} Character set name | ||
| */ | ||
| function checkEncoding(name){ | ||
@@ -63,0 +92,0 @@ name = (name || "").toString().trim(). |
+5
-2
| { | ||
| "name": "encoding", | ||
| "version": "0.1.1", | ||
| "version": "0.1.2", | ||
| "description": "Remove accents from international characters", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| "test": "nodeunit test.js" | ||
| }, | ||
@@ -17,3 +17,6 @@ "repository": "git://github.com/andris9/encoding.git", | ||
| "iconv": "*" | ||
| }, | ||
| "devDependencies":{ | ||
| "nodeunit": "*" | ||
| } | ||
| } |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
5201
73.19%5
25%110
103.7%2
-33.33%1
Infinity%