* * @param {Array } args * @returns
(args)
| 499 | * @returns |
| 500 | */ |
| 501 | function format(args) { |
| 502 | if (args.length <= 1) return [args[0]]; |
| 503 | |
| 504 | const originalArgs = [].concat(args); |
| 505 | const styles = []; |
| 506 | let msg = args.splice(0, 1)[0]; |
| 507 | |
| 508 | if (typeof msg !== "string") return originalArgs; |
| 509 | |
| 510 | let matched = matchRegex(msg); |
| 511 | let match; |
| 512 | while ((match = matched.next())) { |
| 513 | if (match.done) break; |
| 514 | let value = ""; |
| 515 | const specifier = match.value[0]; |
| 516 | const pos = match.value.index; |
| 517 | |
| 518 | if (!args.length) { |
| 519 | value = specifier; |
| 520 | } else { |
| 521 | value = args.splice(0, 1)[0]; |
| 522 | if ([undefined, null].includes(value)) { |
| 523 | value = value + ""; |
| 524 | } |
| 525 | |
| 526 | switch (specifier) { |
| 527 | case "%c": |
| 528 | styles.push({ |
| 529 | value, |
| 530 | pos, |
| 531 | }); |
| 532 | value = ""; |
| 533 | break; |
| 534 | case "%s": |
| 535 | if (typeof value === "object") { |
| 536 | value = value.constructor.name; |
| 537 | } |
| 538 | break; |
| 539 | case "%o": |
| 540 | case "%O": |
| 541 | let id = new Date().getMilliseconds() + ""; |
| 542 | window.__objs[id] = value; |
| 543 | value = `<c-object onclick='console.log(window.__objs[${id}])'>Object</c-object>`; |
| 544 | break; |
| 545 | case "%d": |
| 546 | case "%i": |
| 547 | value = Number.parseInt(value); |
| 548 | break; |
| 549 | case "%f": |
| 550 | value = Number.parseFloat(value); |
| 551 | break; |
| 552 | default: |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | // Only escape HTML for the %o/%O case where we're injecting actual HTML |
| 557 | const escapedValue = |
| 558 | specifier === "%o" || specifier === "%O" ? value : escapeHTML(value); |
no test coverage detected