gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 961 B
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{ContentError as t}from"../error/ContentError.js";import{hex as n}from"./Hex.js";import{sNum as e}from"../safe/safe.js";export const quotedPrintable={fromBytes:function(t,o){let r;null!=(null==o?void 0:o.lineLength)?(e("opts.lineLength",o.lineLength).atLeast(3).atMost(998).throwNot(),r=o.lineLength):r=76;let i="",l=0;function s(t){l+t.length>=r&&(i+="=\r\n",l=0),i+=t,l+=t.length}for(const e of t)s(61===e?"=3D":9===e?"\t":e>=32&&e<=127?String.fromCodePoint(e):"="+n.fromByte(e));return i},toBytes:function(e){const o=new Uint8Array(e.length);let r=0,i=0;for(;r<e.length;){const l=e[r++];if("="===l){const l=e.substring(r,r+2);if(r+=2,"\r\n"===l)continue;try{o[i++]=n.toByte(l)}catch(n){throw new t("invalid escape","Quoted printable","="+l)}}else{const n=l.charCodeAt(0);if(!(n<=255))throw new t("invalid character","Quoted printable",String.fromCodePoint(n));o[i++]=n}}return o.slice(0,i)}};