(obj: unknown, options: LoupeOptions = {})
| 249 | } |
| 250 | |
| 251 | export function objDisplay(obj: unknown, options: LoupeOptions = {}): string { |
| 252 | if (typeof options.truncate === 'undefined') { |
| 253 | options.truncate = 40 |
| 254 | } |
| 255 | const str = inspect(obj, options) |
| 256 | const type = Object.prototype.toString.call(obj) |
| 257 | |
| 258 | if (options.truncate && str.length >= options.truncate) { |
| 259 | if (type === '[object Function]') { |
| 260 | const fn = obj as () => void |
| 261 | return !fn.name ? '[Function]' : `[Function: ${fn.name}]` |
| 262 | } |
| 263 | else if (type === '[object Array]') { |
| 264 | return `[ Array(${(obj as []).length}) ]` |
| 265 | } |
| 266 | else if (type === '[object Object]') { |
| 267 | const keys = Object.keys(obj as object) |
| 268 | const kstr |
| 269 | = keys.length > 2 |
| 270 | ? `${keys.splice(0, 2).join(', ')}, ...` |
| 271 | : keys.join(', ') |
| 272 | return `{ Object (${kstr}) }` |
| 273 | } |
| 274 | else { |
| 275 | return str |
| 276 | } |
| 277 | } |
| 278 | return str |
| 279 | } |
no test coverage detected