satyacode
Background
Back to js-functions

06-shaadi-rsvp.js

JavaScript
1/**
2 * 💒 Shaadi RSVP Manager - Callback Functions
3 *
4 * Big fat Indian wedding ki planning chal rahi hai! Guest list manage
5 * karna hai using callback functions. Callback matlab ek function jo
6 * doosre function ko argument ke roop mein diya jaata hai.
7 *
8 * Functions:
9 *
10 *   1. processGuests(guests, filterFn)
11 *      - guests: array of guest objects
12 *      - filterFn: callback function that takes a guest, returns true/false
13 *      - Returns: array of guests for which filterFn returned true
14 *      - Agar guests not array or filterFn not function, return []
15 *
16 *   2. notifyGuests(guests, notifyCallback)
17 *      - Calls notifyCallback(guest) for EACH guest in array
18 *      - Collects return values from each callback call
19 *      - Returns: array of callback results
20 *      - Agar guests not array or notifyCallback not function, return []
21 *
22 *   3. handleRSVP(guest, onAccept, onDecline)
23 *      - If guest.rsvp === "yes", call onAccept(guest) and return its result
24 *      - If guest.rsvp === "no", call onDecline(guest) and return its result
25 *      - If guest.rsvp is anything else, return null
26 *      - Agar guest null/undefined or callbacks not functions, return null
27 *
28 *   4. transformGuestList(guests, ...transformFns)
29 *      - Takes guest array and any number of transform functions
30 *      - Each transformFn takes an array and returns a new array
31 *      - Apply transforms LEFT to RIGHT (first fn first)
32 *      - Return the final transformed array
33 *      - Agar guests not array, return []
34 *
35 * Hint: Callbacks are just functions passed as arguments to other functions.
36 *   The receiving function decides WHEN to call them.
37 *
38 * @example
39 *   processGuests(
40 *     [{ name: "Rahul", side: "bride" }, { name: "Priya", side: "groom" }],
41 *     guest => guest.side === "bride"
42 *   )
43 *   // => [{ name: "Rahul", side: "bride" }]
44 *
45 *   handleRSVP({ name: "Amit", rsvp: "yes" }, g => `${g.name} is coming!`, g => `${g.name} declined`)
46 *   // => "Amit is coming!"
47 */
48export function processGuests(guests, filterFn) {
49  if(!Array.isArray(guests) || typeof filterFn !== "function"){
50    return []
51  }
52    return guests.filter(filterFn)
53}
54
55export function notifyGuests(guests, notifyCallback) {
56  if(!Array.isArray(guests) || typeof notifyCallback !== "function"){
57    return []
58  }
59  return guests.map(notifyCallback)
60}
61
62export function handleRSVP(guest, onAccept, onDecline) {
63  if(guest == null || typeof guest == "undefined" || typeof onAccept !== 'function' || typeof onDecline !== "function"){
64    return null
65  }
66
67  if(guest.rsvp === "yes"){
68    return onAccept(guest)
69  }else if(guest.rsvp === "no"){
70    return onDecline(guest)
71  }else{
72    return null
73  }
74}
75
76export function transformGuestList(guests, ...transformFns) {
77  if(!Array.isArray(guests)){
78    return []
79  }
80  for (let fn of transformFns){
81    guests = fn(guests)
82  }
83
84  return guests
85}
86