-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
294 lines (263 loc) Β· 11.1 KB
/
index.js
File metadata and controls
294 lines (263 loc) Β· 11.1 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* JavaScript Programming Methods - Main Index
*
* This file provides an overview and quick access to all the JavaScript
* programming methods implemented in this repository.
*
* Run this file to see a summary of all available methods and examples.
*/
console.log("π JavaScript Programming Methods Repository");
console.log("=" .repeat(50));
console.log();
// Method categories and their descriptions
const methodCategories = {
"Array Methods": {
path: "./array-methods/",
description: "Methods for manipulating and working with arrays",
files: [
"filter.js - Filter elements by condition (374 lines)",
"map.js - Transform array elements (255 lines)",
"reduce.js - Reduce to single value (425 lines)",
"forEach.js - Iterate over elements (330+ lines)",
"find.js - Find elements (find, findIndex, findLast) (380+ lines)",
"some-every.js - Test conditions (some, every) (400+ lines)",
"sort.js - Sort array elements (450+ lines)",
"includes-indexOf.js - Search methods (430+ lines)",
"flat-flatMap.js - Flatten nested arrays (380+ lines)",
"slice-splice.js - Extract and modify arrays (450+ lines)"
],
methods: [
"map() - Transform array elements",
"filter() - Filter array elements based on condition",
"reduce() - Reduce array to single value",
"forEach() - Iterate over array elements",
"find() / findIndex() - Find first matching element",
"findLast() / findLastIndex() - Find last matching (ES2023)",
"some() - Test if any element passes condition",
"every() - Test if all elements pass condition",
"sort() - Sort array elements in place",
"slice() - Extract portion without mutation",
"splice() - Add/remove elements (mutates array)",
"includes() - Check if array contains value",
"indexOf() / lastIndexOf() - Find index of value",
"flat() - Flatten nested arrays (ES2019)",
"flatMap() - Map and flatten (ES2019)"
]
},
"String Methods": {
path: "./string-methods/",
description: "Methods for manipulating and processing strings",
methods: [
"charAt() - Get character at specific index",
"substring() - Extract substring",
"indexOf() - Find index of substring",
"replace() - Replace substring or pattern",
"split() - Split string into array",
"trim() - Remove whitespace from ends",
"toLowerCase() - Convert to lowercase",
"toUpperCase() - Convert to uppercase",
"includes() - Check if string contains substring",
"startsWith() - Check if string starts with substring"
]
},
"Object Methods": {
path: "./object-methods/",
description: "Methods for creating and manipulating objects",
methods: [
"Object.keys() - Get object property names",
"Object.values() - Get object property values",
"Object.entries() - Get key-value pairs",
"Object.assign() - Copy properties between objects",
"Object.create() - Create object with prototype",
"Object.freeze() - Make object immutable",
"Object.seal() - Prevent property addition/deletion",
"hasOwnProperty() - Check if property exists",
"Object.defineProperty() - Define property with descriptor",
"Object.getPrototypeOf() - Get object prototype"
]
},
"Function Methods": {
path: "./function-methods/",
description: "Function utilities and advanced concepts",
methods: [
"call() - Call function with specific context",
"apply() - Call function with arguments array",
"bind() - Create bound function",
"Closures - Functions with preserved scope",
"Currying - Transform functions for partial application",
"Memoization - Cache function results",
"Debouncing - Delay function execution",
"Throttling - Limit function call frequency",
"Function composition - Combine functions",
"Higher-order functions - Functions that operate on functions"
]
},
"Promise Methods": {
path: "./promise-methods/",
description: "Asynchronous programming with Promises",
methods: [
"Promise.resolve() - Create resolved promise",
"Promise.reject() - Create rejected promise",
"Promise.all() - Wait for all promises",
"Promise.allSettled() - Wait for all to settle",
"Promise.race() - Wait for first to settle",
"Promise.any() - Wait for first successful",
"async/await - Modern async syntax",
"Error handling - try/catch with promises",
"Promise chaining - Sequential async operations",
"Parallel execution - Concurrent async operations"
]
},
"Utility Methods": {
path: "./utility-methods/",
description: "General utility functions and helpers",
methods: [
"Type checking - Robust type validation",
"Deep cloning - Create deep copies of objects",
"Array utilities - Advanced array operations",
"String utilities - String manipulation helpers",
"Number utilities - Math and formatting functions",
"Date utilities - Date manipulation and formatting",
"Validation utilities - Input validation functions",
"Performance utilities - Benchmarking and optimization",
"Deep comparison - Compare complex objects",
"Data transformation - Convert between formats"
]
}
};
// Display overview of all categories
console.log("π Method Categories Overview:");
console.log();
Object.entries(methodCategories).forEach(([category, info]) => {
console.log(`πΉ ${category}`);
console.log(` ${info.description}`);
console.log(` Path: ${info.path}`);
console.log(` Methods: ${info.methods.length} available`);
console.log();
});
// Quick examples section
console.log("β‘ Quick Examples:");
console.log();
// Array example
console.log("πΈ Array Methods Example:");
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(x => x * 2);
const evens = numbers.filter(x => x % 2 === 0);
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(` Input: [${numbers.join(', ')}]`);
console.log(` Doubled: [${doubled.join(', ')}] (using map)`);
console.log(` Evens: [${evens.join(', ')}] (using filter)`);
console.log(` Sum: ${sum} (using reduce)`);
console.log();
// String example
console.log("πΈ String Methods Example:");
const text = " Hello, JavaScript World! ";
console.log(` Original: "${text}"`);
console.log(` Trimmed: "${text.trim()}"`);
console.log(` Uppercase: "${text.trim().toUpperCase()}"`);
console.log(` Words: [${text.trim().split(' ').join(', ')}]`);
console.log(` Contains 'JavaScript': ${text.includes('JavaScript')}`);
console.log();
// Object example
console.log("πΈ Object Methods Example:");
const person = { name: "John", age: 30, city: "New York" };
console.log(` Object:`, person);
console.log(` Keys: [${Object.keys(person).join(', ')}]`);
console.log(` Values: [${Object.values(person).join(', ')}]`);
console.log(` Entries:`, Object.entries(person));
console.log();
// Promise example
console.log("πΈ Promise Methods Example:");
console.log(" Creating multiple promises...");
const promise1 = Promise.resolve("First");
const promise2 = Promise.resolve("Second");
const promise3 = Promise.resolve("Third");
Promise.all([promise1, promise2, promise3])
.then(results => {
console.log(` Promise.all result: [${results.join(', ')}]`);
});
console.log();
// Function example
console.log("πΈ Function Methods Example:");
// Currying example
const add = a => b => c => a + b + c;
const add5 = add(5);
const add5And10 = add5(10);
console.log(` Curried function: add(5)(10)(3) = ${add(5)(10)(3)}`);
console.log(` Partial application: add5And10(3) = ${add5And10(3)}`);
// Memoization example
function memoize(fn) {
const cache = {};
return function(...args) {
const key = JSON.stringify(args);
if (cache[key]) {
return cache[key];
}
const result = fn.apply(this, args);
cache[key] = result;
return result;
};
}
const expensiveFunction = (n) => {
console.log(` Computing for ${n}...`);
return n * n;
};
const memoizedFunction = memoize(expensiveFunction);
console.log(` First call: ${memoizedFunction(5)}`);
console.log(` Second call: ${memoizedFunction(5)} (cached)`);
console.log();
// Utility example
console.log("πΈ Utility Methods Example:");
// Type checking
const values = [42, "hello", [], {}, null, undefined];
values.forEach(value => {
const type = value === null ? 'null' :
Array.isArray(value) ? 'array' :
typeof value;
console.log(` ${JSON.stringify(value)} is type: ${type}`);
});
console.log();
// Usage instructions
console.log("π How to Use This Repository:");
console.log();
console.log("1. π Browse Categories:");
console.log(" Navigate to any method category folder to explore specific methods");
console.log();
console.log("2. πββοΈ Run Individual Files:");
console.log(" node array-methods/map.js");
console.log(" node string-methods/string-methods.js");
console.log(" node object-methods/object-methods.js");
console.log(" node function-methods/function-methods.js");
console.log(" node promise-methods/promise-methods.js");
console.log(" node utility-methods/utility-methods.js");
console.log();
console.log("3. π Learn from Examples:");
console.log(" Each file contains comprehensive examples with:");
console.log(" β
Clear explanations");
console.log(" β
Practical use cases");
console.log(" β
Performance considerations");
console.log(" β
Best practices");
console.log(" β
Common pitfalls to avoid");
console.log();
console.log("4. π οΈ Apply in Your Projects:");
console.log(" Copy and adapt the utility functions for your own projects");
console.log(" All functions are well-documented and tested");
console.log();
console.log("π― Key Benefits:");
console.log("β’ Comprehensive coverage of JavaScript methods");
console.log("β’ Real-world examples and use cases");
console.log("β’ Performance optimization tips");
console.log("β’ Modern JavaScript (ES6+) features");
console.log("β’ Best practices and patterns");
console.log("β’ Error handling techniques");
console.log();
console.log("π€ Contributing:");
console.log("Feel free to contribute by:");
console.log("β’ Adding new methods or examples");
console.log("β’ Improving existing documentation");
console.log("β’ Reporting bugs or issues");
console.log("β’ Suggesting enhancements");
console.log();
console.log("π License: MIT");
console.log("π Happy coding with JavaScript!");
console.log("=" .repeat(50));