encoding
Advanced tools
Comparing version 0.1.12 to 0.1.13
'use strict'; | ||
var iconvLite = require('iconv-lite'); | ||
// Load Iconv from an external file to be able to disable Iconv for webpack | ||
// Add /\/iconv-loader$/ to webpack.IgnorePlugin to ignore it | ||
var Iconv = require('./iconv-loader'); | ||
@@ -17,6 +14,5 @@ // Expose to the world | ||
* @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) { | ||
function convert(str, to, from) { | ||
from = checkEncoding(from || 'UTF-8'); | ||
@@ -29,3 +25,3 @@ to = checkEncoding(to || 'UTF-8'); | ||
if (from !== 'UTF-8' && typeof str === 'string') { | ||
str = new Buffer(str, 'binary'); | ||
str = Buffer.from(str, 'binary'); | ||
} | ||
@@ -35,18 +31,6 @@ | ||
if (typeof str === 'string') { | ||
result = new Buffer(str); | ||
result = Buffer.from(str); | ||
} else { | ||
result = str; | ||
} | ||
} else if (Iconv && !useLite) { | ||
try { | ||
result = convertIconv(str, to, from); | ||
} catch (E) { | ||
console.error(E); | ||
try { | ||
result = convertIconvLite(str, to, from); | ||
} catch (E) { | ||
console.error(E); | ||
result = str; | ||
} | ||
} | ||
} else { | ||
@@ -61,5 +45,4 @@ try { | ||
if (typeof result === 'string') { | ||
result = new Buffer(result, 'utf-8'); | ||
result = Buffer.from(result, 'utf-8'); | ||
} | ||
@@ -71,17 +54,2 @@ | ||
/** | ||
* Convert encoding of a string 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) { | ||
var response, iconv; | ||
iconv = new Iconv(from, to + '//TRANSLIT//IGNORE'); | ||
response = iconv.convert(str); | ||
return response.slice(0, response.length); | ||
} | ||
/** | ||
* Convert encoding of astring with iconv-lite | ||
@@ -111,9 +79,11 @@ * | ||
function checkEncoding(name) { | ||
return (name || '').toString().trim(). | ||
replace(/^latin[\-_]?(\d+)$/i, 'ISO-8859-$1'). | ||
replace(/^win(?:dows)?[\-_]?(\d+)$/i, 'WINDOWS-$1'). | ||
replace(/^utf[\-_]?(\d+)$/i, 'UTF-$1'). | ||
replace(/^ks_c_5601\-1987$/i, 'CP949'). | ||
replace(/^us[\-_]?ascii$/i, 'ASCII'). | ||
toUpperCase(); | ||
return (name || '') | ||
.toString() | ||
.trim() | ||
.replace(/^latin[\-_]?(\d+)$/i, 'ISO-8859-$1') | ||
.replace(/^win(?:dows)?[\-_]?(\d+)$/i, 'WINDOWS-$1') | ||
.replace(/^utf[\-_]?(\d+)$/i, 'UTF-$1') | ||
.replace(/^ks_c_5601\-1987$/i, 'CP949') | ||
.replace(/^us[\-_]?ascii$/i, 'ASCII') | ||
.toUpperCase(); | ||
} |
{ | ||
"name": "encoding", | ||
"version": "0.1.12", | ||
"description": "Convert encodings, uses iconv by default and fallbacks to iconv-lite if needed", | ||
"main": "lib/encoding.js", | ||
"scripts": { | ||
"test": "nodeunit test" | ||
}, | ||
"repository": "https://github.com/andris9/encoding.git", | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"dependencies": { | ||
"iconv-lite": "~0.4.13" | ||
}, | ||
"devDependencies": { | ||
"iconv": "~2.1.11", | ||
"nodeunit": "~0.9.1" | ||
} | ||
"name": "encoding", | ||
"version": "0.1.13", | ||
"description": "Convert encodings, uses iconv-lite", | ||
"main": "lib/encoding.js", | ||
"scripts": { | ||
"test": "nodeunit test" | ||
}, | ||
"repository": "https://github.com/andris9/encoding.git", | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"dependencies": { | ||
"iconv-lite": "^0.6.2" | ||
}, | ||
"devDependencies": { | ||
"nodeunit": "0.11.3" | ||
} | ||
} |
# Encoding | ||
**encoding** is a simple wrapper around [node-iconv](https://github.com/bnoordhuis/node-iconv) and [iconv-lite](https://github.com/ashtuchkin/iconv-lite/) to convert strings from one encoding to another. If node-iconv is not available for some reason, | ||
iconv-lite will be used instead of it as a fallback. | ||
**encoding** is a simple wrapper around [iconv-lite](https://github.com/ashtuchkin/iconv-lite/) to convert strings from one encoding to another. | ||
@@ -9,2 +8,4 @@ [![Build Status](https://secure.travis-ci.org/andris9/encoding.svg)](http://travis-ci.org/andris9/Nodemailer) | ||
Initially _encoding_ was a wrapper around _node-iconv_ (main) and _iconv-lite_ (fallback) and was used as the encoding layer for Nodemailer/mailparser. Somehow it also ended up as a dependency for a bunch of other project, none of these actually using _node-iconv_. The loading mechanics caused issues for front-end projects and Nodemailer/malparser had moved on, so _node-iconv_ was removed. | ||
## Install | ||
@@ -28,5 +29,5 @@ | ||
* **text** is either a Buffer or a String to be converted | ||
* **toCharset** is the characterset to convert the string | ||
* **fromCharset** (*optional*, defaults to UTF-8) is the source charset | ||
- **text** is either a Buffer or a String to be converted | ||
- **toCharset** is the characterset to convert the string | ||
- **fromCharset** (_optional_, defaults to UTF-8) is the source charset | ||
@@ -40,16 +41,4 @@ Output of the conversion is always a Buffer object. | ||
## iconv support | ||
By default only iconv-lite is bundled. If you need node-iconv support, you need to add it | ||
as an additional dependency for your project: | ||
..., | ||
"dependencies":{ | ||
"encoding": "*", | ||
"iconv": "*" | ||
}, | ||
... | ||
## License | ||
**MIT** |
'use strict'; | ||
var Iconv = require('../lib/iconv-loader'); | ||
var encoding = require('../lib/encoding'); | ||
exports['General tests'] = { | ||
'Iconv is available': function (test) { | ||
test.ok(Iconv); | ||
test.done(); | ||
}, | ||
'From UTF-8 to Latin_1 with Iconv': function (test) { | ||
'From UTF-8 to Latin_1': function (test) { | ||
var input = 'ÕÄÖÜ', | ||
expected = new Buffer([0xd5, 0xc4, 0xd6, 0xdc]); | ||
expected = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc]); | ||
test.deepEqual(encoding.convert(input, 'latin1'), expected); | ||
@@ -20,4 +13,4 @@ test.done(); | ||
'From Latin_1 to UTF-8 with Iconv': function (test) { | ||
var input = new Buffer([0xd5, 0xc4, 0xd6, 0xdc]), | ||
'From Latin_1 to UTF-8': function (test) { | ||
var input = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc]), | ||
expected = 'ÕÄÖÜ'; | ||
@@ -28,5 +21,5 @@ test.deepEqual(encoding.convert(input, 'utf-8', 'latin1').toString(), expected); | ||
'From UTF-8 to UTF-8 with Iconv': function (test) { | ||
'From UTF-8 to UTF-8': function (test) { | ||
var input = 'ÕÄÖÜ', | ||
expected = new Buffer('ÕÄÖÜ'); | ||
expected = Buffer.from('ÕÄÖÜ'); | ||
test.deepEqual(encoding.convert(input, 'utf-8', 'utf-8'), expected); | ||
@@ -36,43 +29,24 @@ test.done(); | ||
'From Latin_13 to Latin_15 with Iconv': function (test) { | ||
var input = new Buffer([0xd5, 0xc4, 0xd6, 0xdc, 0xd0]), | ||
expected = new Buffer([0xd5, 0xc4, 0xd6, 0xdc, 0xA6]); | ||
'From Latin_13 to Latin_15': function (test) { | ||
var input = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc, 0xd0]), | ||
expected = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc, 0xa6]); | ||
test.deepEqual(encoding.convert(input, 'latin_15', 'latin13'), expected); | ||
test.done(); | ||
}, | ||
} | ||
'From ISO-2022-JP to UTF-8 with Iconv': function (test) { | ||
var input = new Buffer('GyRCM1g5OzU7PVEwdzgmPSQ4IUYkMnFKczlwGyhC', 'base64'), | ||
expected = new Buffer('5a2m5qCh5oqA6KGT5ZOh56CU5L+u5qSc6KiO5Lya5aCx5ZGK', 'base64'); | ||
test.deepEqual(encoding.convert(input, 'utf-8', 'ISO-2022-JP'), expected); | ||
/* | ||
// ISO-2022-JP is not supported by iconv-lite | ||
"From ISO-2022-JP to UTF-8 with Iconv": function (test) { | ||
var input = Buffer.from( | ||
"GyRCM1g5OzU7PVEwdzgmPSQ4IUYkMnFKczlwGyhC", | ||
"base64" | ||
), | ||
expected = Buffer.from( | ||
"5a2m5qCh5oqA6KGT5ZOh56CU5L+u5qSc6KiO5Lya5aCx5ZGK", | ||
"base64" | ||
); | ||
test.deepEqual(encoding.convert(input, "utf-8", "ISO-2022-JP"), expected); | ||
test.done(); | ||
}, | ||
'From UTF-8 to Latin_1 with iconv-lite': function (test) { | ||
var input = 'ÕÄÖÜ', | ||
expected = new Buffer([0xd5, 0xc4, 0xd6, 0xdc]); | ||
test.deepEqual(encoding.convert(input, 'latin1', false, true), expected); | ||
test.done(); | ||
}, | ||
'From Latin_1 to UTF-8 with iconv-lite': function (test) { | ||
var input = new Buffer([0xd5, 0xc4, 0xd6, 0xdc]), | ||
expected = 'ÕÄÖÜ'; | ||
test.deepEqual(encoding.convert(input, 'utf-8', 'latin1', true).toString(), expected); | ||
test.done(); | ||
}, | ||
'From UTF-8 to UTF-8 with iconv-lite': function (test) { | ||
var input = 'ÕÄÖÜ', | ||
expected = new Buffer('ÕÄÖÜ'); | ||
test.deepEqual(encoding.convert(input, 'utf-8', 'utf-8', true), expected); | ||
test.done(); | ||
}, | ||
'From Latin_13 to Latin_15 with iconv-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(); | ||
} | ||
*/ | ||
}; |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
1
1
7123
7
124
42
+ Addediconv-lite@0.6.3(transitive)
- Removediconv-lite@0.4.24(transitive)
Updatediconv-lite@^0.6.2