UNPKG

whatsapp-chat-parser

Version:

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

3 lines • 3.12 kB
"use strict";var x=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var V=(n,t)=>{for(var e in t)x(n,e,{get:t[e],enumerable:!0})},I=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of C(t))!N.call(n,s)&&s!==e&&x(n,s,{get:()=>t[s],enumerable:!(r=D(t,s))||r.enumerable});return n};var Y=n=>I(x({},"__esModule",{value:!0}),n);var W={};V(W,{parseString:()=>Q});module.exports=Y(W);function M(n,t){return e=>e[n]>t}function A(n){return n<0}function R(n,t){return Object.values(n.reduce((e,r)=>{let s=`_${r[t]}`;return e[s]||(e[s]=[]),e[s].push(r),e},{}))}function q(n){return n.some(M(0,12))?!0:n.some(M(1,12))?!1:null}function L(n){let e=R(n,2).map(o=>o.slice(1).some((l,m)=>{let[i]=o[m],[u]=l;return A(u-i)})?!0:o.slice(1).some((l,m)=>{let[,i]=o[m],[,u]=l;return A(u-i)})?!1:null);return e.some(o=>o===!0)?!0:e.some(o=>o===!1)?!1:null}function U(n){let t=n.slice(1).map((s,o)=>s.map((a,c)=>Math.abs(n[o][c]-a))),[e,r]=t.reduce((s,o)=>{let[a,c]=s,[l,m]=o;return[a+l,c+m]},[0,0]);return e>r?!0:e<r?!1:null}function T(n){let t=q(n);if(t!==null)return t;let e=L(n);return e!==null?e:U(n)}function F(n,t,e){return[n.padStart(4,"2000"),t.padStart(2,"0"),e.padStart(2,"0")]}function b(n){let t=/[-/.] ?/,[e,r,s]=n.split(t),o=Math.max(e.length,r.length,s.length);return s.length===o?[e,r,s]:r.length===o?[e,s,r]:[r,s,e]}var p=/[:.]/;function P(n,t){let[e,r,s]=n.split(p);return e==="12"&&(e="00"),t==="PM"&&(e=String(parseInt(e,10)+12)),`${e}:${r}${s?`:${s}`:""}`}function $(n){let[t,e,r]=n.split(p);return`${t.padStart(2,"0")}:${e}:${r||"00"}`}function k(n){return n.replace(/[^apm]/gi,"").toUpperCase()}var v=/^(?:\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/,_=/(.+?):\s([^]*)/,G=/([^]+)/,O=new RegExp(v.source+_.source,"i"),E=new RegExp(v.source+G.source,"i"),H=/^(?:\u200E|\u200F)*(?:<.+:(.+)>|([\w-]+\.\w+)\s[(<].+[)>])/;function z(n){return n.reduce((t,e)=>{if(O.test(e))t.push({system:!1,msg:e});else if(E.test(e))t.push({system:!0,msg:e});else if(typeof t[t.length-1]!="undefined"){let r=t.pop();t.push({system:r.system,msg:`${r.msg} ${e}`})}return t},[])}function J(n){let t=n.match(H);return t?{fileName:(t[1]||t[2]).trim()}:null}function B(n,t={}){let{daysFirst:e}=t,{parseAttachments:r}=t,s=n.map(o=>{let{system:a,msg:c}=o;if(a){let[,f,d,h,y]=E.exec(c);return{date:f,time:d,ampm:h||null,author:null,message:y}}let[,l,m,i,u,g]=O.exec(c);return{date:l,time:m,ampm:i||null,author:u,message:g}});if(typeof e!="boolean"){let o=Array.from(new Set(s.map(({date:a})=>a)),a=>b(a).map(Number));e=T(o)}return s.map(({date:o,time:a,ampm:c,author:l,message:m})=>{let i,u,g,f=b(o);e===!1?[u,i,g]=f:[i,u,g]=f,[g,u,i]=F(g,u,i);let[d,h,y]=$(c?P(a,k(c)):a).split(p),S={date:new Date(+g,+u-1,+i,+d,+h,+y),author:l,message:m};if(r){let w=J(m);w&&(S.attachment=w)}return S})}var K=/(?:\r\n|\r|\n)/;function Q(n,t={parseAttachments:!1}){let e=n.split(K);return B(z(e),t)}0&&(module.exports={parseString}); //# sourceMappingURL=index.cjs.map