UNPKG

whatsapp-chat-parser

Version:

A package to parse WhatsApp chats with Node.js or in the browser 💬

3 lines • 2.63 kB
function x(t,n){return e=>e[t]>n}function M(t){return t<0}function w(t,n){return Object.values(t.reduce((e,s)=>{let o=`_${s[n]}`;return e[o]||(e[o]=[]),e[o].push(s),e},{}))}function B(t){return t.some(x(0,12))?!0:t.some(x(1,12))?!1:null}function D(t){let e=w(t,2).map(r=>r.slice(1).some((l,m)=>{let[i]=r[m],[u]=l;return M(u-i)})?!0:r.slice(1).some((l,m)=>{let[,i]=r[m],[,u]=l;return M(u-i)})?!1:null);return e.some(r=>r===!0)?!0:e.some(r=>r===!1)?!1:null}function C(t){let n=t.slice(1).map((o,r)=>o.map((a,c)=>Math.abs(t[r][c]-a))),[e,s]=n.reduce((o,r)=>{let[a,c]=o,[l,m]=r;return[a+l,c+m]},[0,0]);return e>s?!0:e<s?!1:null}function R(t){let n=B(t);if(n!==null)return n;let e=D(t);return e!==null?e:C(t)}function T(t,n,e){return[t.padStart(4,"2000"),n.padStart(2,"0"),e.padStart(2,"0")]}function A(t){let n=/[-/.] ?/,[e,s,o]=t.split(n),r=Math.max(e.length,s.length,o.length);return o.length===r?[e,s,o]:s.length===r?[e,o,s]:[s,o,e]}var p=/[:.]/;function F(t,n){let[e,s,o]=t.split(p);return e==="12"&&(e="00"),n==="PM"&&(e=String(parseInt(e,10)+12)),`${e}:${s}${o?`:${o}`:""}`}function P(t){let[n,e,s]=t.split(p);return`${n.padStart(2,"0")}:${e}:${s||"00"}`}function $(t){return t.replace(/[^apm]/gi,"").toUpperCase()}var k=/^(?:\u200E|\u200F)*\[?(\d{1,4}[-/.]\s?\d{1,4}[-/.]\s?\d{1,4})[,.]?\s\D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?:\s([ap]\.?\s?m\.?))?\]?(?:\s-|:)?\s/,N=/(.+?):\s([^]*)/,V=/([^]+)/,v=new RegExp(k.source+N.source,"i"),O=new RegExp(k.source+V.source,"i"),I=/^(?:\u200E|\u200F)*(?:<.+:(.+)>|([\w-]+\.\w+)\s[(<].+[)>])/;function E(t){return t.reduce((n,e)=>{if(v.test(e))n.push({system:!1,msg:e});else if(O.test(e))n.push({system:!0,msg:e});else if(typeof n[n.length-1]!="undefined"){let s=n.pop();n.push({system:s.system,msg:`${s.msg} ${e}`})}return n},[])}function Y(t){let n=t.match(I);return n?{fileName:(n[1]||n[2]).trim()}:null}function z(t,n={}){let{daysFirst:e}=n,{parseAttachments:s}=n,o=t.map(r=>{let{system:a,msg:c}=r;if(a){let[,f,d,h,y]=O.exec(c);return{date:f,time:d,ampm:h||null,author:null,message:y}}let[,l,m,i,u,g]=v.exec(c);return{date:l,time:m,ampm:i||null,author:u,message:g}});if(typeof e!="boolean"){let r=Array.from(new Set(o.map(({date:a})=>a)),a=>A(a).map(Number));e=R(r)}return o.map(({date:r,time:a,ampm:c,author:l,message:m})=>{let i,u,g,f=A(r);e===!1?[u,i,g]=f:[i,u,g]=f,[g,u,i]=T(g,u,i);let[d,h,y]=P(c?F(a,$(c)):a).split(p),b={date:new Date(+g,+u-1,+i,+d,+h,+y),author:l,message:m};if(s){let S=Y(m);S&&(b.attachment=S)}return b})}var q=/(?:\r\n|\r|\n)/;function W(t,n={parseAttachments:!1}){let e=t.split(q);return z(E(e),n)}export{W as parseString}; //# sourceMappingURL=index.js.map