Convert character encodings in pure javascript.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Synchronous character encoding conversion functions for converting between strings and buffers with different character encodings.
Converts a JavaScript string to an encoded Buffer using the specified character encoding.
/**
* Convert string to encoded buffer
* @param content - String to encode (converted to string if not already)
* @param encoding - Target encoding name (case-insensitive)
* @param options - Optional encoding options
* @returns Encoded buffer
*/
function encode(content, encoding, options);Usage Examples:
const iconv = require('iconv-lite');
// Basic encoding
const buf = iconv.encode("Hello, 世界", 'utf8');
console.log(buf); // <Buffer 48 65 6c 6c 6f 2c 20 e4 b8 96 e7 95 8c>
// Encoding with BOM
const bufWithBOM = iconv.encode("Hello", 'utf16le', { addBOM: true });
// Windows encoding
const winBuf = iconv.encode("Café", 'win1252');Converts an encoded Buffer to a JavaScript string using the specified character encoding.
/**
* Convert encoded buffer to string
* @param buffer - Buffer or Uint8Array to decode
* @param encoding - Source encoding name (case-insensitive)
* @param options - Optional decoding options
* @returns Decoded string
*/
function decode(buffer, encoding, options);Usage Examples:
const iconv = require('iconv-lite');
// Basic decoding
const str = iconv.decode(Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]), 'ascii');
console.log(str); // "Hello"
// Decoding with BOM stripping (default behavior)
const utf16Str = iconv.decode(utf16Buffer, 'utf16le');
// Decoding with BOM preservation
const strWithBOM = iconv.decode(utf16Buffer, 'utf16le', { stripBOM: false });
// Multibyte encoding
const chineseStr = iconv.decode(gbkBuffer, 'gbk');Checks if a specific character encoding is supported by the library.
/**
* Check if encoding is supported
* @param encoding - Encoding name to check
* @returns True if encoding is supported, false otherwise
*/
function encodingExists(encoding);Usage Examples:
const iconv = require('iconv-lite');
// Check common encodings
console.log(iconv.encodingExists('utf8')); // true
console.log(iconv.encodingExists('iso-8859-1')); // true
console.log(iconv.encodingExists('invalid')); // false
// Case-insensitive check
console.log(iconv.encodingExists('UTF-8')); // true
console.log(iconv.encodingExists('utf8')); // trueLegacy function names provided for backward compatibility.
/**
* Legacy alias for encode()
* @deprecated Use encode() instead
*/
function toEncoding(str, encoding, options);
/**
* Legacy alias for decode()
* @deprecated Use decode() instead
*/
function fromEncoding(buf, encoding, options);/**
* Options for encode/decode functions
*/
const Options = {
/** Strip BOM (Byte Order Mark) from decoded output (default: true for decode) */
stripBOM,
/** Add BOM to encoded output (default: false) */
addBOM,
/** Default encoding fallback */
defaultEncoding
};Error with message "Encoding not recognized: 'ENCODING'"When characters cannot be represented in the target encoding, they are replaced with encoding-specific default characters accessible via:
/** Default character for Unicode encoding errors */
const defaultCharUnicode; // "�"
/** Default character for single-byte encoding errors */
const defaultCharSingleByte; // "?"