-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (140 loc) · 4.25 KB
/
Copy pathindex.js
File metadata and controls
163 lines (140 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const calc = {
total: 0,
add: function (value) {
this.total += value;
return this;
},
multiply: function (value) {
this.total *= value;
return this;
},
subtract: function (value) {
this.total -= value;
return this;
},
divide: function (value) {
this.total /= value;
return this;
},
};
const result = calc.add(10).add(10).subtract(1).divide(2);
//implement PUB SUB library
//implement tic tac toe
//HLD of jira software
//box model ,flex, specificity
//flipkart ->
//FIND DEPTH OF A NODE IN HTML TREE
//IMPLEMENT LRU CACHE
//product list on left(from api), chat on right on click on product(options from api)
//redux implementation
//amazon
// Given a series of child-parent relations like
// capture the relationship of these entities so you can print the
// relationships in a nested format at any point.
//understand this!!!
function findAndAddChild(obj, child, parent) {
// If we found the parent, add the child to it
if (parent in obj) {
obj[parent][child] = {};
return true;
}
// Recursively search through all nested objects
for (let key in obj) {
if (
typeof obj[key] === "object" &&
findAndAddChild(obj[key], child, parent)
) {
return true;
}
}
return false;
}
function addRelation(map, [child, parent]) {
// If parent doesn't exist anywhere in the structure, add it at top level
if (!findAndAddChild(map, child, parent)) {
if (!map[parent]) map[parent] = {};
map[parent][child] = {};
}
return map;
}
let map = {};
map = addRelation(map, ["dog", "mammal"]);
map = addRelation(map, ["shark", "fish"]);
map = addRelation(map, ["cat", "mammal"]);
map = addRelation(map, ["mammal", "animal"]);
map = addRelation(map, ["fish", "animal"]);
map = addRelation(map, ["nemo", "fish"]);
map = addRelation(map, ["puppy", "dog"]);
map = addRelation(map, ["small-fish", "nemo"]);
console.log(JSON.stringify(map, null, 2));
return;
/**
*
* @param {*} input - eg "a.b.c",2
* @param {*} finalValue {a: {b: c: 2}}
*/
function stringToObject(path, value) {
if (typeof path !== "string" || path.length === 0) {
throw new TypeError("Path must be a non-empty string.");
}
const result = {};
const keys = [];
let currentKey = "";
let inQuotes = false;
// Parse the keys from the path
for (let i = 0; i < path.length; i++) {
const char = path[i];
if (char === '"') {
inQuotes = !inQuotes; // Toggle inQuotes state
} else if (char === "." && !inQuotes) {
if (currentKey) {
keys.push(currentKey);
currentKey = "";
}
} else {
currentKey += char;
}
}
if (currentKey) {
keys.push(currentKey); // Add the last key
}
let current = result;
// Build the object structure
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
// If it's the last key, assign the value
if (i === keys.length - 1) {
current[key] = value; // Assign the value to the final key
} else {
// Create a new object or array if it doesn't exist
if (!current[key]) {
current[key] = isNaN(keys[i + 1]) ? {} : []; // Create an array if the next key is numeric
}
current = current[key]; // Move to the next level
}
}
return result;
}
// Test cases
console.log(stringToObject("a.b.c", 1)); // { a: { b: { c: 1 } } }
// console.log(stringToObject("", 1)); // Throws TypeError
console.log(stringToObject('a."b.c"."d.e"', 2)); // { a: { 'b.c': { 'd.e': 2 } } }
console.log(stringToObject("users.0.name", "devtools tech")); // { users: [{ name: "devtools tech" }] }
console.log(stringToObject("a.b.c", 1)); // { a: { b: { c: 1 } } }
// Test cases
// console.log(stringToObject("a.b.c", 1));
// { a: { b: { c: 1 } } }
// console.log(stringToObject("", 1));
// Throws TypeError
// console.log(stringToObject('a."b.c"."d.e"', 2));
// { a: { 'b.c': { 'd.e': 2 } } }
///console.log(stringToObject("users.0.name", "devtools tech"));
// { users: [{ name: "devtools tech" }] }
console.log(stringToObject("a.b.c", 1));
// { a: { b: { c: 1 } } }
// console.log(stringToObject("", 1));
// throw a TypeError
// console.log(stringToObject('a."b.c"."d.e"', 2));
// consider "b.c" and "d.e" as individual keys
// output => { a: { 'b.c': { 'd.e': 2 } } }
// console.log(stringToObject("users.0.name", "devtools tech"));