Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | type StringMethod = (str: string) => string; function applyStringMethodToLeaves<T>(obj: T, method: StringMethod): T { if (typeof obj === "object" && obj !== null) { if (Array.isArray(obj)) { // If it's an array, apply recursively to each element in the array return obj.map((item) => applyStringMethodToLeaves(item, method)) as T; } else { // If it's an object, apply recursively to each property const newObj: any = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { newObj[key] = applyStringMethodToLeaves((obj as any)[key], method); } } return newObj as T; } } else if (typeof obj === "string") { // Apply the string method if it's a string return method(obj) as T; } return obj; } |