* 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 * @return {Buffer} Encoded string
(str, to, 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 |
nothing calls this directly
no test coverage detected