satyacode
Background
Back to js-datatype-foundation

09-postcard-writer.js

JavaScript
1/**
2 * 💌 Indian Postcard Writer - String Advanced
3 *
4 * Dadi ji ko digital postcard likhna hai. Template literals se message banana,
5 * addresses check karna, formatting karna — string advanced methods se
6 * postcards likh!
7 *
8 * Methods to explore: template literals (`${}`), .startsWith(),
9 *   .endsWith(), .padStart(), .padEnd(), .match()
10 *
11 * Functions:
12 *
13 *   1. writePostcard(sender, receiver, message)
14 *      - Template literal se formatted postcard banao:
15 *        "Priy {receiver},\n\n{message}\n\nAapka/Aapki,\n{sender}"
16 *      - Agar koi bhi param string nahi hai ya trim ke baad empty hai, return ""
17 *      - Example: writePostcard("Guddu", "Dadi ji", "Hum theek hain")
18 *                 => "Priy Dadi ji,\n\nHum theek hain\n\nAapka/Aapki,\nGuddu"
19 *
20 *   2. isValidPincode(code)
21 *      - Indian pincodes: 6 digits, "0" se start nahi hota
22 *      - .startsWith("0") se check karo ki "0" se start nahi ho raha
23 *      - .length === 6 check karo
24 *      - Har character digit hona chahiye (use /^\d+$/ regex test or check each char)
25 *      - Agar code string nahi hai, return false
26 *      - Example: isValidPincode("400001") => true
27 *      - Example: isValidPincode("012345") => false
28 *
29 *   3. formatPostcardField(label, value, width)
30 *      - label.padEnd(width) + ": " + value — for aligned fields
31 *      - Wait, let me simplify: return label.padEnd(12) + ": " + value
32 *      - Agar width provided, use that instead of 12
33 *      - Agar label ya value string nahi hai, return ""
34 *      - Example: formatPostcardField("From", "Guddu") => "From        : Guddu"
35 *      - Example: formatPostcardField("To", "Dadi ji", 8) => "To      : Dadi ji"
36 *
37 *   4. isFromState(address, stateCode)
38 *      - .endsWith() se check karo ki address kisi state code se end hota hai
39 *      - Agar address ya stateCode string nahi hai, return false
40 *      - Example: isFromState("Guddu, Lucknow, UP", "UP") => true
41 *      - Example: isFromState("Priya, Mumbai, MH", "UP") => false
42 *
43 *   5. countVowels(message)
44 *      - .match(/[aeiouAEIOU]/g) se saare vowels dhundho
45 *      - Return: count (match result ki length, ya 0 agar null hai)
46 *      - Agar message string nahi hai, return 0
47 *      - Example: countVowels("Namaste India") => 6
48 *
49 * @example
50 *   writePostcard("Guddu", "Dadi ji", "Hum theek hain")
51 *   isValidPincode("400001")   // => true
52 *   countVowels("Namaste")     // => 3
53 */
54export function writePostcard(sender, receiver, message) {
55
56  function isValidString (...values){
57    return values.every(v =>{
58      return typeof v == "string" && v.trim().length > 0 && v !== null
59    })
60  }
61
62  if(!isValidString(sender, receiver, message)){
63    return ""
64  }
65  return `Priy ${receiver},\n\n${message}\n\nAapka/Aapki,\n${sender}`
66}
67
68export function isValidPincode(code) {
69  if(typeof code !== "string" || code.length !== 6 || code.startsWith("0")|| !/^\d+$/.test(code)){
70    return false;
71  }
72return true
73}
74
75export function formatPostcardField(label, value, width) {
76  if(typeof label !== "string" || typeof value !== "string"){
77    return ""
78  }
79  
80 const finalWidth = typeof width === "number" ? width : 12;
81  return `${label.padEnd(finalWidth)}: ${value}`
82}
83
84export function isFromState(address, stateCode) {
85   if(typeof address !== "string" || typeof stateCode !== "string"){
86    return false
87  }
88
89  return address.endsWith(stateCode)
90}
91
92export function countVowels(message) {
93  if (typeof message !== "string") {
94    return 0;
95  }
96  if(message.match(/[aeiouAEIOU]/g) == null){
97    return 0
98  }
99
100  return message.match(/[aeiouAEIOU]/g).length
101}
102