CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-iconv-lite

Convert character encodings in pure javascript.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

core-encoding.mddocs/

Core Encoding API

Synchronous character encoding conversion functions for converting between strings and buffers with different character encodings.

Capabilities

Encode Function

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');

Decode Function

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');

Encoding Exists Function

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'));        // true

Legacy Aliases

Legacy 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

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

  • Invalid encoding: Throws Error with message "Encoding not recognized: 'ENCODING'"
  • Encoding errors: Replaced with default characters:
    • Unicode errors: "�" (U+FFFD)
    • Single-byte errors: "?"
  • Type conversion: Non-strings are converted to strings, non-buffers/strings for decode trigger deprecation warning

Character Replacement

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; // "?"

docs

core-encoding.md

index.md

low-level.md

streaming.md

tile.json