What is utf7?
The utf7 npm package provides utilities for encoding and decoding text using the UTF-7 encoding scheme. UTF-7 is a variable-length character encoding that was designed for representing Unicode text using a stream of ASCII characters. It is particularly useful for encoding text in environments that are not 8-bit clean, such as email systems.
What are utf7's main functionalities?
Encoding to UTF-7
This feature allows you to encode a string into UTF-7 format. The provided code sample demonstrates how to encode the string 'Hello, World!' into its UTF-7 representation.
const utf7 = require('utf7');
const encoded = utf7.encode('Hello, World!');
console.log(encoded); // Outputs: 'Hello, World!'
Decoding from UTF-7
This feature allows you to decode a UTF-7 encoded string back into its original form. The provided code sample demonstrates how to decode the UTF-7 encoded string 'Hello, World!' back to 'Hello, World!'.
const utf7 = require('utf7');
const decoded = utf7.decode('Hello, World!');
console.log(decoded); // Outputs: 'Hello, World!'
Other packages similar to utf7
iconv-lite
The iconv-lite package provides pure JavaScript character encoding conversion. It supports a wide range of encodings, including UTF-8, UTF-16, and many legacy encodings. Unlike utf7, iconv-lite does not specifically focus on UTF-7 but offers broader encoding support.
buffer
The buffer package is a Node.js core module that provides a way of handling binary data directly in JavaScript. It supports various encoding schemes, including UTF-8 and Base64. While it does not specifically target UTF-7, it can be used for general encoding and decoding tasks.
UTF-7
Encodes and decodes JavaScript (Unicode/UCS-2) strings to UTF-7 ASCII strings. It supports two modes: UTF-7 as defined in RFC 2152 and Modified UTF-7 as defined by the IMAP standard in RFC 3501, section 5.1.3
Usage
RFC 2152
var utf7 = require('utf7');
var encoded = utf7.encode('Jyväskylä');
assert.equal('Jyv+AOQ-skyl+AOQ-', encoded);
var decoded = utf7.decode(encoded);
assert.equal('Jyväskylä', decoded);
By default, .encode()
only encodes the default characeters defined in RFC 2152. To also encode optional characters, use .encodeAll()
or specify the characters you want to encode as the second argument to .encode()
.
IMAP (RFC 3501)
var utf7 = require('utf7').imap;
var encoded = utf7.encode('"你好" heißt "Hallo"');
assert.equal('"&T2BZfQ-" hei&AN8-t "Hallo"', encoded);
var decoded = utf7.decode(encoded);
assert.equal('"你好" heißt "Hallo"', decoded);