| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export const parallelLetterFrequency = async (texts) => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 7 | // Edge Case: Handle null or undefined input |
| 8 | if (texts === null || texts === undefined) { |
| 9 | return {}; |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Handle empty array input |
| 13 | if (texts.length === 0) { |
| 14 | return {}; |
| 15 | } |
| 16 | |
| 17 | // Function to count letters in a single text |
| 18 | const countLetters = (text) => { |
| 19 | // Edge Case: Handle null or undefined text elements |
| 20 | if (text === null || text === undefined) { |
| 21 | return {}; |
| 22 | } |
| 23 | |
| 24 | const letterCount = {}; |
| 25 | // Convert to lowercase and iterate through each character |
| 26 | for (const char of text.toLowerCase()) { |
| 27 | // Edge Case: Only count alphabetic characters (including Unicode) |
| 28 | // Use Unicode property escape to detect letters |
| 29 | if (/\p{L}/u.test(char)) { |
| 30 | letterCount[char] = (letterCount[char] || 0) + 1; |
| 31 | } |
| 32 | } |
| 33 | return letterCount; |
| 34 | }; |
| 35 | |
| 36 | // Process all texts in parallel |
| 37 | const results = await Promise.all(texts.map(countLetters)); |
| 38 | |
| 39 | // Combine all results into a single object |
| 40 | const totalCount = {}; |
| 41 | |
| 42 | // Edge Case: Handle empty results array |
| 43 | if (results.length === 0) { |
| 44 | return totalCount; |
| 45 | } |
| 46 | |
| 47 | for (const result of results) { |
| 48 | for (const [letter, count] of Object.entries(result)) { |
| 49 | totalCount[letter] = (totalCount[letter] || 0) + count; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return totalCount; |
| 8 | 54 | }; |
| 55 | |
| 56 | // Handled Edge Cases: null/undefined input, empty array input, null/undefined text elements, non-alphabetic characters, empty results |