gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.49 kB
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{ContentError as n}from"../error/ContentError.js";const t="\r\n";export const yEnc={fromBytes:function(n,o){var e,l,i,r,c;const u=null!==(e=null==o?void 0:o.lineLength)&&void 0!==e?e:128,s=null!==(l=null==o?void 0:o.offset)&&void 0!==l?l:42,d=null!==(i=null==o?void 0:o.escapeChar)&&void 0!==i?i:"=",f=d.codePointAt(0);let v="",h=0;function a(n){return d+String.fromCodePoint(n+64&255)}function g(n,t){const o=n+s&255;switch(o){case 0:case 10:case 13:case f:return a(o);default:return t&&t.includes(o)?a(o):String.fromCodePoint(o)}}let P,A=0;const m=n.length-1;for(;A<m;)P=g(n[A++]),0!=h?h+P.length!=u?h+P.length>u?(v+=t+P,h=P.length):(v+=P,h+=P.length):" "==P||"\t"==P?(P=a(null!==(c=P.codePointAt(0))&&void 0!==c?c:0),v+=t+P,h=P.length):(v+=P+t,h=0):(" "!=P&&"\t"!=P&&"."!=P||(P=a(null!==(r=P.codePointAt(0))&&void 0!==r?r:0)),v+=P,h+=P.length);return P=g(n[m],[32,9]),h+P.length>=u&&(v+=t,h=0),v+P},toBytes:function(t,o){var e,l,i;const r=null!==(e=null==o?void 0:o.offset)&&void 0!==e?e:42,c=null!==(l=null==o?void 0:o.escapeChar)&&void 0!==l?l:"=",u=new Uint8Array(t.length);let s=0,d=0;function f(n){var t;return(null!==(t=n.codePointAt(0))&&void 0!==t?t:0)-64&255}for(;s<t.length;){const o=t[s++];if(o===c){const n=t[s++];u[d++]=f(n)-r&255}else{if("\r"===o){const e=t[s++];if("\n"===e)continue;throw new n("Invalid sequence","yEnc",o+e)}u[d++]=(null!==(i=o.codePointAt(0))&&void 0!==i?i:0)-r&255}}return u.slice(0,d)}};