Skip to content

Commit

Permalink
v0.1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jul 10, 2020
1 parent 91ae950 commit a7554ca
Show file tree
Hide file tree
Showing 7 changed files with 2,057 additions and 142 deletions.
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
printWidth: 160,
tabWidth: 4,
singleQuote: true,
endOfLine: 'lf',
trailingComma: 'none',
arrowParens: 'avoid'
};
23 changes: 6 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# 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.

[![Build Status](https://secure.travis-ci.org/andris9/encoding.svg)](http://travis-ci.org/andris9/Nodemailer)
[![npm version](https://badge.fury.io/js/encoding.svg)](http://badge.fury.io/js/encoding)

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

Install through npm
Expand All @@ -24,9 +25,9 @@ Convert with encoding.convert()

Where

* **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

Output of the conversion is always a Buffer object.

Expand All @@ -35,18 +36,6 @@ Example
var result = encoding.convert("ÕÄÖÜ", "Latin_1");
console.log(result); //<Buffer d5 c4 d6 dc>

## 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**
56 changes: 13 additions & 43 deletions lib/encoding.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'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');

// Expose to the world
module.exports.convert = convert;
Expand All @@ -14,38 +11,25 @@ module.exports.convert = convert;
* @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) {
function convert(str, to, from) {
from = checkEncoding(from || 'UTF-8');
to = checkEncoding(to || 'UTF-8');
str = str || '';

var result;

if (from !== 'UTF-8' && typeof str === 'string') {
str = new Buffer(str, 'binary');
str = Buffer.from(str, 'binary');
}

if (from === to) {
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 {
try {
result = convertIconvLite(str, to, from);
Expand All @@ -55,29 +39,13 @@ function convert(str, to, from, useLite) {
}
}


if (typeof result === 'string') {
result = new Buffer(result, 'utf-8');
result = Buffer.from(result, 'utf-8');
}

return result;
}

/**
* 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
*
Expand All @@ -103,11 +71,13 @@ function convertIconvLite(str, to, from) {
* @return {String} Character set name
*/
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();
}
14 changes: 0 additions & 14 deletions lib/iconv-loader.js

This file was deleted.

Loading

0 comments on commit a7554ca

Please sign in to comment.