Back to js-datatype-foundation
07-ration-card.js
JavaScript
1/**
2 * 🏠 Ration Card Registry - Object Basics
3 *
4 * Gram Panchayat mein ration card ka digital registry banana hai.
5 * Families ka data Object mein store hai. Object methods use karke
6 * registry manage kar!
7 *
8 * Data format: registry = {
9 * "RC001": { head: "Ram Prasad", members: 4, type: "BPL" },
10 * "RC002": { head: "Sita Devi", members: 3, type: "APL" },
11 * ...
12 * }
13 *
14 * Methods to explore: Object.keys(), Object.values(), Object.entries(),
15 * .hasOwnProperty(), delete operator
16 *
17 * Functions:
18 *
19 * 1. getFamilyNames(registry)
20 * - Object.keys() se saare ration card IDs nikalo
21 * - Agar registry object nahi hai ya null hai, return []
22 * - Example: getFamilyNames({"RC001":{...},"RC002":{...}}) => ["RC001", "RC002"]
23 *
24 * 2. getAllFamilies(registry)
25 * - Object.values() se saari family objects nikalo
26 * - Agar registry object nahi hai ya null hai, return []
27 * - Example: getAllFamilies({"RC001":{head:"Ram"}}) => [{head:"Ram"}]
28 *
29 * 3. getRationCardEntries(registry)
30 * - Object.entries() se [id, family] pairs nikalo
31 * - Agar registry object nahi hai ya null hai, return []
32 * - Example: getRationCardEntries({"RC001":{head:"Ram"}}) => [["RC001",{head:"Ram"}]]
33 *
34 * 4. hasRationCard(registry, cardId)
35 * - .hasOwnProperty() se check karo ki specific ration card hai ya nahi
36 * - Agar registry object nahi hai ya cardId string nahi hai, return false
37 * - Example: hasRationCard({"RC001":{head:"Ram"}}, "RC001") => true
38 * - Example: hasRationCard({"RC001":{head:"Ram"}}, "RC999") => false
39 *
40 * 5. removeRationCard(registry, cardId)
41 * - delete operator se ration card remove karo
42 * - Pehle hasOwnProperty se check karo ki card hai ya nahi
43 * - Return true agar card tha aur delete hua, false otherwise
44 * - Agar registry object nahi hai ya cardId string nahi hai, return false
45 * - Example: removeRationCard({"RC001":{head:"Ram"}}, "RC001") => true
46 *
47 * Hint: typeof registry === "object" && registry !== null && !Array.isArray(registry)
48 * se check karo ki input valid object hai.
49 *
50 * @example
51 * getFamilyNames({"RC001":{...}}) // => ["RC001"]
52 * hasRationCard({"RC001":{...}}, "RC001") // => true
53 * removeRationCard(registry, "RC001") // => true
54 */
55export function getFamilyNames(registry) {
56 if(typeof registry !== "object"|| registry == null || Array.isArray(registry)){
57 return []
58 }
59 return Object.keys(registry)
60}
61
62export function getAllFamilies(registry) {
63 if(typeof registry !== "object"|| registry == null){
64 return []
65 }
66 return Object.values(registry)
67}
68
69export function getRationCardEntries(registry) {
70 if(typeof registry !== "object"|| registry == null){
71 return []
72 }
73 return Object.entries(registry)
74}
75
76export function hasRationCard(registry, cardId) {
77 if(typeof registry !== "object"|| typeof cardId !== "string" || cardId.length == 0 || registry == null){
78 return false
79 }
80
81 return registry.hasOwnProperty(cardId)
82}
83
84export function removeRationCard(registry, cardId) {
85 if(typeof registry !== "object"|| typeof cardId !== "string" || registry === null){
86 return false
87 }
88
89 if(registry.hasOwnProperty(cardId)){
90 return delete registry[cardId];
91 }
92 return false
93}
94