(req, res, arg)
| 13 | * @returns {string} The plain text representation |
| 14 | */ |
| 15 | export function plain (req, res, arg) { |
| 16 | // Handle primitive types directly |
| 17 | if (arg === null || arg === undefined) { |
| 18 | return arg.toString(); |
| 19 | } |
| 20 | |
| 21 | // Check cache for objects we've already processed |
| 22 | if (typeof arg === "object" && plainCache.has(arg)) { |
| 23 | return plainCache.get(arg); |
| 24 | } |
| 25 | |
| 26 | let result; |
| 27 | |
| 28 | if (Array.isArray(arg)) { |
| 29 | result = arg.map(i => plain(req, res, i)).join(COMMA); |
| 30 | } else if (arg instanceof Date) { |
| 31 | result = arg.toISOString(); |
| 32 | } else if (typeof arg === "function") { |
| 33 | result = arg.toString(); |
| 34 | } else if (arg instanceof Object) { |
| 35 | result = JSON.stringify(arg, null, indent(req.headers.accept, req.server.json)); |
| 36 | } else { |
| 37 | result = arg.toString(); |
| 38 | } |
| 39 | |
| 40 | // Cache the result for objects |
| 41 | if (typeof arg === "object" && arg !== null) { |
| 42 | plainCache.set(arg, result); |
| 43 | } |
| 44 | |
| 45 | return result; |
| 46 | } |
no test coverage detected