MIME 编码c
重要
这是电子邮件/电子邮件js-mime-codec(MIT)的分叉. 上游 : https://github.com/emailjs/emailjs-mime-codec 页面存档备份,存于互联网档案馆
变化 :
- 使用最新的 ramda 库版本
库允许您在不同的 MIME 相关编码之间编码并解码. 引文使用过时参数coauthors (帮助) Base64等.
所有输入都可以使用任意字符集(在此情况下,值不能是字符串而是Uint8Array的阵列缓冲器),但输出总是unicode.
使用量
npm install --save @gooddata/emailjs-mime-codec-new
import {
encode, decode, convert,
mimeEncode, mimeDecode,
base64Encode, base64Decode,
quotedPrintableEncode, quotedPrintableDecode,
mimeWordEncode, mimeWordDecode,
mimeWordsEncode, mimeWordsDecode,
headerLineEncode, headerLinesDecode,
continuationEncode,
foldLines,
parseHeaderValue
} from '@gooddata/emailjs-mime-codec-new'
字符集函数
encode(data: String) -> Uint8Array
decode(data: Uint8Array, charset: String) -> String
convert(data, charset: String) -> String
encode 取字符串并返回编码为UTF-8的Uint8Array.
decode 带一个Uint8Array和一个字符集,并将输入的数组解码为字符串。 Charset 默认为 UTF- 8.
convert 链式编码并解码并用给定编码将输入字符串转换为被编码为UTF-8的Uint8Array.
折叠线
根据RFC 5322折叠出一条长线 http://tools.ietf.org/html/rfc5322#section-2.1.1
foldLines(str [, lineLengthMax[, afterSpace]]) -> String
- 字符串 - 要被折叠的字符串
- 线条 Length Max - 最长行长(默认为76)
- 空间后 - 如果是真的,在一行的尽头留下一个空间
例如:
foldLines('Content-Type: multipart/alternative; boundary="----zzzz----"')
结果
Content-Type: multipart/alternative;
boundary="----zzzz----"
mIME Word 编码
将字符串编码为 mime 编码的单词格式 http://en.wikipedia.org/wiki/MIME# Encoded-Word (英语) (另见 mimeWordDecode(中文(简体) )
mimeWordEncode(str [, mimeWordEncoding[, fromCharset]]) -> String
- 字符串 - 要编码的字符串或Uint8箭头
- mIME WORD 编码 - 为 Q 或 B 的 MIME 词编码( 默认为 “ Q ”)
- 从沙尔塞特 - 如果第一个参数是一个已输入的数组,请使用此编码来解码值为 Unicode
例如:
mimeWordEncode('See on õhin test', 'Q');
成为 UTF-8 和可引用的可打印编码
=?UTF-8?Q?See_on_=C3=B5hin_test?=
mime Word十进制
从 mime 编码字词格式解码字符串(另见 mimeWordEncode(中文(简体) )
mimeWordDecode(str) -> String
- 字符串 - 要解码的字符串
举个例子
mimeWordDecode('=?UTF-8?Q?See_on_=C3=B5hin_test?=');
将变成
See on õhin test
继续编码
根据 RFC2231 (法语) 参数值继续.
continuationEncode(key, str [, fromCharset]) -> Array
- 键 - 参数键(例如:.
filename(中文(简体) ) - 字符串 - 要编码的字符串或Uint8箭头值
- 从沙尔塞特 - 狦
str是一个已键入的数组, 使用此字符集在编码前解码值为 unicode
该方法返回一组编码部件,其结构如下: [{key:'...', value: '...'}]
示例
continuationEncode('filename', 'filename õäöü.txt', 20);
->
[ { key: 'filename*0*', value: 'utf-8\'\'filename%20' },
{ key: 'filename*1*', value: '%C3%B5%C3%A4%C3%B6' },
{ key: 'filename*2*', value: '%C3%BC.txt' } ]
这可以合并成一个格式化的标题 :
Content-disposition: attachment; filename*0*="utf-8''filename%20"
filename*1*="%C3%B5%C3%A4%C3%B6"; filename*2*="%C3%BC.txt"
引用的棱镜编码
将字符串编码为可引用的打印格式(另见 quotedPrintableDecode) (中文(简体) ). 最大行数
生成字符串的长度为76 + 2 字节.
quotedPrintableEncode(str [, fromCharset]) -> String
- 字符串 - 字符串或一个Uint8箭头来编码MIME
- 从沙尔塞特 - 如果第一个参数是一个已键入的数组,请在编码前使用这个字符集来解码值为 Unicode
引用的可折叠十进制
从可引用的打印格式解码字符串(另见 quotedPrintableEncode) (中文(简体) ).
quotedPrintableDecode(str [, fromCharset]) -> String
- 字符串 - MIME 编码字符串
- 从沙尔塞特 - 使用这个字符集来解码已编码的 MIME 字符串为 Unicode
基础64 编码
将字符串编码为 Base64 格式(另见 base64Decode) (中文(简体) ). 最大行数
生成字符串的长度为76 + 2 字节.
base64Encode(str [, fromCharset]) -> String
- 字符串 - 字符串或Uint8箭头到基数64编码
- 从沙尔塞特 - 如果第一个参数是一个已键入的数组,请在编码前使用这个字符集来解码值为 Unicode
基数64 十进制
从 Base64 格式解码字符串( 另见 base64Encode)到一个未编码的unicode字符串.
base64Decode(str [, fromCharset]) -> String
- 字符串 Base64 编码字符串
- 从沙尔塞特 使用此字符集来解码 Base64 编码字符串为 Unicode
mIME Word 编码
将字符串编码为 mime 单词.
mimeWordEncode(str[, mimeWordEncoding[, fromCharset]]) -> String
- 字符串 - 要编码的字符串或Uint8箭头
- mIME WORD 编码 - 为 Q 或 B 的 MIME 词编码( 默认为 “ Q ”)
- 从沙尔塞特 - 如果第一个参数是一个已键入的数组,请在编码前使用这个字符集来解码值为 Unicode
mIME Words 编码
在字符串中编码非 ascii 序列来缩写单词 .
mimeWordsEncode(str[, mimeWordEncoding[, fromCharset]]) -> String
- 字符串 - 要编码的字符串或Uint8箭头
- mIME WORD 编码 - 为 Q 或 B 的 MIME 词编码( 默认为 “ Q ”)
- 从沙尔塞特 - 如果第一个参数是一个已键入的数组,请在编码前使用这个字符集来解码值为 Unicode
mime Word十进制
解码完整的 MIME 单词编码字符串
mimeWordDecode(str) -> String
- 字符串 - 要解码的字符串。 MIME 单词包含字符集信息, 因此需要在此指定
mIME Words十进制
解码可能包含一个或多个 MIME 词的字符串 。 如果从字符串中找不到 MIME 单词, 则返回原字符串
mimeWordsDecode(str) -> String
- 字符串 - 要解码的字符串
标题线码
为 MIME 信件头编码并折叠标题行 。 简指 mimeWordsEncode + 键 foldLines。 。 。 .
headerLineEncode(key, value[, fromCharset])
- 键 - 密钥名称,不会被编码
- 价值 - 要编码的值
- 从沙尔塞特 - 如果说
value参数是一个已键入的数组, 使用此字符集在编码前解码到 unicode
信头线十进制
解一块头行. 不将缩写为每个标题 可能有自己的规则(如格式化的电子邮件地址等).
返回值是信头对象,其中信头密钥为对象密钥. 诺比! 数个具有相同密钥的值构成相同密钥的一系列值.
headerLinesDecode(headers) -> Object
- 页眉 - 信头串
解析页眉值
以 key=value 参数为结构对象。 处理时有用
content-type 和这样的.
parseHeaderValue(valueString) -> Object
- 数值 - 没有密钥的标题值
示例
parseHeaderValue('content-type: text/plain; CHARSET="UTF-8"');
产出
{
"value": "text/plain",
"params": {
"charset": "UTF-8"
}
}
许可证
The MIT License
Copyright (c) 2013 Andris Reinman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.```