UNPKG

whatsapp-chat-parser

Version:

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

3 lines (2 loc) • 3.12 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).whatsappChatParser={})}(this,(function(t){"use strict";function e(t,e){return n=>n[t]>e}function n(t){return t<0}function s(t){var e,s;const r=(e=t,s=2,Object.values(e.reduce((t,e)=>{const n="_"+e[s];return t[n]||(t[n]=[]),t[n].push(e),t},{}))).map(t=>{if(t.slice(1).some((e,s)=>{const[r]=t[s],[u]=e;return n(u-r)}))return!0;return!t.slice(1).some((e,s)=>{const[,r]=t[s],[,u]=e;return n(u-r)})&&null});if(r.some(t=>!0===t))return!0;return!r.some(t=>!1===t)&&null}function r(t){const n=function(t){return!!t.some(e(0,12))||!t.some(e(1,12))&&null}(t);if(null!==n)return n;const r=s(t);return null!==r?r:function(t){const e=t.slice(1).map((e,n)=>e.map((e,s)=>Math.abs(t[n][s]-e))),[n,s]=e.reduce((t,e)=>{const[n,s]=t,[r,u]=e;return[n+r,s+u]},[0,0]);return n>s||!(n<s)&&null}(t)}function u(t){const[e,n,s]=t.split(/[-/.] ?/),r=Math.max(e.length,n.length,s.length);return s.length===r?[e,n,s]:n.length===r?[e,s,n]:[n,s,e]}const o=/[:.]/;const a=/^(?:\u200E|\u200F)*\[?(\d{1,4}[-/.] ?\d{1,4}[-/.] ?\d{1,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?\s?m\.?))?\]?(?: -|:)? (.+?): ([^]*)/i,c=/^(?:\u200E|\u200F)*\[?(\d{1,4}[-/.] ?\d{1,4}[-/.] ?\d{1,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?\s?m\.?))?\]?(?: -|:)? ([^]+)/i,i=/<.+:(.+)>|([A-Z\d-]+\.\w+)\s\(.+\)/;function m(t){return t.reduce((t,e)=>{if(a.test(e))t.push({system:!1,msg:e});else if(c.test(e))t.push({system:!0,msg:e});else if(void 0!==t[t.length-1]){const n=t.pop();t.push({system:n.system,msg:`${n.msg}\n${e}`})}return t},[])}function l(t,e={}){let{daysFirst:n}=e;const{parseAttachments:s}=e,m=t.map(t=>{const{system:e,msg:n}=t;if(e){const[,t,e,s,r]=c.exec(n);return{date:t,time:e,ampm:s||null,author:"System",message:r}}const[,s,r,u,o,i]=a.exec(n);return{date:s,time:r,ampm:u||null,author:o,message:i}});if("boolean"!=typeof n){const t=Array.from(new Set(m.map(({date:t})=>t)),t=>u(t).map(Number));n=r(t)}return m.map(({date:t,time:e,ampm:r,author:a,message:c})=>{let m,l,p;const f=u(t);!1===n?[l,m,p]=f:[m,l,p]=f,[p,l,m]=function(t,e,n){return[t.padStart(4,"2000"),e.padStart(2,"0"),n.padStart(2,"0")]}(p,l,m);const[d,h,g]=function(t){const[e,n,s]=t.split(o);return`${e.padStart(2,"0")}:${n}:${s||"00"}`}(r?function(t,e){let[n,s,r]=t.split(o);return"12"===n&&(n="00"),"PM"===e&&(n=String(parseInt(n,10)+12)),`${n}:${s}${r?":"+r:""}`}(e,function(t){return t.replace(/[^apm]/gi,"").toUpperCase()}(r)):e).split(o),y={date:new Date(+p,+l-1,+m,+d,+h,+g),author:a,message:c};if(s){const t=function(t){const e=t.match(i);return e?{fileName:(e[1]||e[2]).trim()}:null}(c);t&&(y.attachment=t)}return y})}const p=/(?:\r\n|\r|\n)/;t.parseString=function(t,e={parseAttachments:!1}){return Promise.resolve(t).then(t=>t.split(p)).then(m).then(t=>l(t,e))},t.parseStringSync=function(t,e={parseAttachments:!1}){return l(m(t.split(p)),e)},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=whatsapp-chat-parser.min.js.map