| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export const truncate = (input) => { |
| 7 | throw new Error('Remove this line and implement the function'); | |
| 7 | // Edge Case: null or undefined input | |
| 8 | if (input === null || input === undefined) { | |
| 9 | return ''; | |
| 10 | } | |
| 11 | ||
| 12 | // Edge Case: empty string | |
| 13 | if (input === '') { | |
| 14 | return ''; | |
| 15 | } | |
| 16 | ||
| 17 | // Convert to string if it's not already | |
| 18 | const str = String(input); | |
| 19 | ||
| 20 | // Edge Case: string with 5 or fewer characters | |
| 21 | if (Array.from(str).length <= 5) { | |
| 22 | return str; | |
| 23 | } | |
| 24 | ||
| 25 | // Edge Case: string with more than 5 Unicode characters | |
| 26 | // Using Array.from to properly handle Unicode characters (including emojis) | |
| 27 | const characters = Array.from(str); | |
| 28 | return characters.slice(0, 5).join(''); | |
| 8 | 29 | }; |
| 30 | ||
| 31 | // Handled Edge Cases: null or undefined input, empty string, string with 5 or fewer characters, string with more than 5 Unicode characters |