satyacode
Background
JavaScript
1/**
2 * 💬 WhatsApp Message Parser
3 *
4 * Chintu ek WhatsApp chat analyzer bana raha hai. Usse raw WhatsApp
5 * exported message line parse karni hai aur usme se date, time, sender,
6 * aur message alag alag extract karna hai.
7 *
8 * WhatsApp export format:
9 *   "DD/MM/YYYY, HH:MM - Sender Name: Message text here"
10 *
11 * Rules:
12 *   - Date extract karo: string ke start se pehle ", " (comma-space) tak
13 *   - Time extract karo: ", " ke baad se " - " (space-dash-space) tak
14 *   - Sender extract karo: " - " ke baad se pehle ": " (colon-space) tak
15 *   - Message text extract karo: pehle ": " ke baad (after sender) sab kuch, trimmed
16 *   - wordCount: message ke words count karo (split by space, filter empty strings)
17 *   - Sentiment detection (case-insensitive check on message text):
18 *     - Agar message mein "😂" ya ":)" ya "haha" hai => sentiment = "funny"
19 *     - Agar message mein "❤" ya "love" ya "pyaar" hai => sentiment = "love"
20 *     - Otherwise => sentiment = "neutral"
21 *     - Agar dono match hote hain, "funny" gets priority
22 *   - Hint: Use indexOf(), substring()/slice(), includes(), split(),
23 *     trim(), toLowerCase()
24 *
25 * Validation:
26 *   - Agar input string nahi hai, return null
27 *   - Agar string mein " - " nahi hai ya ": " nahi hai (after sender), return null
28 *
29 * @param {string} message - Raw WhatsApp exported message line
30 * @returns {{ date: string, time: string, sender: string, text: string, wordCount: number, sentiment: string } | null}
31 *
32 * @example
33 *   parseWhatsAppMessage("25/01/2025, 14:30 - Rahul: Bhai party kab hai? 😂")
34 *   // => { date: "25/01/2025", time: "14:30", sender: "Rahul",
35 *   //      text: "Bhai party kab hai? 😂", wordCount: 5, sentiment: "funny" }
36 *
37 *   parseWhatsAppMessage("01/12/2024, 09:15 - Priya: I love this song")
38 *   // => { date: "01/12/2024", time: "09:15", sender: "Priya",
39 *   //      text: "I love this song", wordCount: 4, sentiment: "love" }
40 */
41export function parseWhatsAppMessage(message) {
42  if(typeof message !== "string"){
43    return null
44  }
45  if(!message.includes(" - ")|| !message.includes(": ")){
46    return null
47  }
48
49  const commaIndex = message.indexOf(",")
50  const dashIndex = message.indexOf("-")
51  const colonIndex = message.indexOf(":", dashIndex)
52
53  const date = message.substring(0, commaIndex).trim()
54  const time = message.substring(commaIndex + 2, dashIndex).trim()
55  const sender = message.substring(dashIndex + 2, colonIndex).trim()
56  const messageExtract = message.substring(colonIndex + 2, message.length).trim()
57  const wordCount = (messageExtract.split(" ").filter(t => t.length !==0 )).length
58
59
60 function sentimentAnalysis(messageExtract){
61  let text = messageExtract.toLowerCase()
62
63  if(text.includes("😂") || text.includes(":)") || text.includes("haha")){
64    return "funny"
65  }else if(text.includes("❤") || text.includes("love") || text.includes("pyaar")){
66    return "love"
67  }else{
68    return "neutral"
69  }
70 }
71
72 const sentiment = sentimentAnalysis(messageExtract)
73  
74
75  return { date, time, sender, text: messageExtract, wordCount, sentiment }
76}
77