├── .gitignore ├── .prettierrc.js ├── .travis.yml ├── package.json ├── LICENSE ├── README.md ├── test └── test.js └── lib └── encoding.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | printWidth: 160, 3 | tabWidth: 4, 4 | singleQuote: true, 5 | endOfLine: 'lf', 6 | trailingComma: 'none', 7 | arrowParens: 'avoid' 8 | }; 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - 6 5 | - 14 6 | arch: 7 | - amd64 8 | - ppc64le 9 | notifications: 10 | email: 11 | - andris@kreata.ee 12 | webhooks: 13 | urls: 14 | - https://webhooks.gitter.im/e/0ed18fd9b3e529b3c2cc 15 | on_success: change # options: [always|never|change] default: always 16 | on_failure: always # options: [always|never|change] default: always 17 | on_start: false # default: false 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "encoding", 3 | "version": "0.1.13", 4 | "description": "Convert encodings, uses iconv-lite", 5 | "main": "lib/encoding.js", 6 | "scripts": { 7 | "test": "nodeunit test" 8 | }, 9 | "repository": "https://github.com/andris9/encoding.git", 10 | "author": "Andris Reinman", 11 | "license": "MIT", 12 | "dependencies": { 13 | "iconv-lite": "^0.6.2" 14 | }, 15 | "devDependencies": { 16 | "nodeunit": "0.11.3" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2014 Andris Reinman 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 11 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 13 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 14 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 15 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 16 | SOFTWARE. 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Encoding 2 | 3 | **encoding** is a simple wrapper around [iconv-lite](https://github.com/ashtuchkin/iconv-lite/) to convert strings from one encoding to another. 4 | 5 | [![Build Status](https://secure.travis-ci.org/andris9/encoding.svg)](http://travis-ci.org/andris9/Nodemailer) 6 | [![npm version](https://badge.fury.io/js/encoding.svg)](http://badge.fury.io/js/encoding) 7 | 8 | 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. 9 | 10 | ## Install 11 | 12 | Install through npm 13 | 14 | npm install encoding 15 | 16 | ## Usage 17 | 18 | Require the module 19 | 20 | var encoding = require("encoding"); 21 | 22 | Convert with encoding.convert() 23 | 24 | var resultBuffer = encoding.convert(text, toCharset, fromCharset); 25 | 26 | Where 27 | 28 | - **text** is either a Buffer or a String to be converted 29 | - **toCharset** is the characterset to convert the string 30 | - **fromCharset** (_optional_, defaults to UTF-8) is the source charset 31 | 32 | Output of the conversion is always a Buffer object. 33 | 34 | Example 35 | 36 | var result = encoding.convert("ÕÄÖÜ", "Latin_1"); 37 | console.log(result); // 38 | 39 | ## License 40 | 41 | **MIT** 42 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var encoding = require('../lib/encoding'); 4 | 5 | exports['General tests'] = { 6 | 'From UTF-8 to Latin_1': function (test) { 7 | var input = 'ÕÄÖÜ', 8 | expected = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc]); 9 | test.deepEqual(encoding.convert(input, 'latin1'), expected); 10 | test.done(); 11 | }, 12 | 13 | 'From Latin_1 to UTF-8': function (test) { 14 | var input = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc]), 15 | expected = 'ÕÄÖÜ'; 16 | test.deepEqual(encoding.convert(input, 'utf-8', 'latin1').toString(), expected); 17 | test.done(); 18 | }, 19 | 20 | 'From UTF-8 to UTF-8': function (test) { 21 | var input = 'ÕÄÖÜ', 22 | expected = Buffer.from('ÕÄÖÜ'); 23 | test.deepEqual(encoding.convert(input, 'utf-8', 'utf-8'), expected); 24 | test.done(); 25 | }, 26 | 27 | 'From Latin_13 to Latin_15': function (test) { 28 | var input = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc, 0xd0]), 29 | expected = Buffer.from([0xd5, 0xc4, 0xd6, 0xdc, 0xa6]); 30 | test.deepEqual(encoding.convert(input, 'latin_15', 'latin13'), expected); 31 | test.done(); 32 | } 33 | 34 | /* 35 | // ISO-2022-JP is not supported by iconv-lite 36 | "From ISO-2022-JP to UTF-8 with Iconv": function (test) { 37 | var input = Buffer.from( 38 | "GyRCM1g5OzU7PVEwdzgmPSQ4IUYkMnFKczlwGyhC", 39 | "base64" 40 | ), 41 | expected = Buffer.from( 42 | "5a2m5qCh5oqA6KGT5ZOh56CU5L+u5qSc6KiO5Lya5aCx5ZGK", 43 | "base64" 44 | ); 45 | test.deepEqual(encoding.convert(input, "utf-8", "ISO-2022-JP"), expected); 46 | test.done(); 47 | }, 48 | */ 49 | }; 50 | -------------------------------------------------------------------------------- /lib/encoding.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var iconvLite = require('iconv-lite'); 4 | 5 | // Expose to the world 6 | module.exports.convert = convert; 7 | 8 | /** 9 | * Convert encoding of an UTF-8 string or a buffer 10 | * 11 | * @param {String|Buffer} str String to be converted 12 | * @param {String} to Encoding to be converted to 13 | * @param {String} [from='UTF-8'] Encoding to be converted from 14 | * @return {Buffer} Encoded string 15 | */ 16 | function convert(str, to, from) { 17 | from = checkEncoding(from || 'UTF-8'); 18 | to = checkEncoding(to || 'UTF-8'); 19 | str = str || ''; 20 | 21 | var result; 22 | 23 | if (from !== 'UTF-8' && typeof str === 'string') { 24 | str = Buffer.from(str, 'binary'); 25 | } 26 | 27 | if (from === to) { 28 | if (typeof str === 'string') { 29 | result = Buffer.from(str); 30 | } else { 31 | result = str; 32 | } 33 | } else { 34 | try { 35 | result = convertIconvLite(str, to, from); 36 | } catch (E) { 37 | console.error(E); 38 | result = str; 39 | } 40 | } 41 | 42 | if (typeof result === 'string') { 43 | result = Buffer.from(result, 'utf-8'); 44 | } 45 | 46 | return result; 47 | } 48 | 49 | /** 50 | * Convert encoding of astring with iconv-lite 51 | * 52 | * @param {String|Buffer} str String to be converted 53 | * @param {String} to Encoding to be converted to 54 | * @param {String} [from='UTF-8'] Encoding to be converted from 55 | * @return {Buffer} Encoded string 56 | */ 57 | function convertIconvLite(str, to, from) { 58 | if (to === 'UTF-8') { 59 | return iconvLite.decode(str, from); 60 | } else if (from === 'UTF-8') { 61 | return iconvLite.encode(str, to); 62 | } else { 63 | return iconvLite.encode(iconvLite.decode(str, from), to); 64 | } 65 | } 66 | 67 | /** 68 | * Converts charset name if needed 69 | * 70 | * @param {String} name Character set 71 | * @return {String} Character set name 72 | */ 73 | function checkEncoding(name) { 74 | return (name || '') 75 | .toString() 76 | .trim() 77 | .replace(/^latin[\-_]?(\d+)$/i, 'ISO-8859-$1') 78 | .replace(/^win(?:dows)?[\-_]?(\d+)$/i, 'WINDOWS-$1') 79 | .replace(/^utf[\-_]?(\d+)$/i, 'UTF-$1') 80 | .replace(/^ks_c_5601\-1987$/i, 'CP949') 81 | .replace(/^us[\-_]?ascii$/i, 'ASCII') 82 | .toUpperCase(); 83 | } 84 | --------------------------------------------------------------------------------