Represent a value as a string for the purpose of logging.
(value: unknown)
| 727 | |
| 728 | /** Represent a value as a string for the purpose of logging. */ |
| 729 | function _valueAsString(value: unknown) { |
| 730 | if (value === undefined) { |
| 731 | return 'undefined'; |
| 732 | } |
| 733 | try { |
| 734 | // `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer. |
| 735 | // Use a character that is unlikely to appear in real strings to denote the start and end of |
| 736 | // the regex. This allows us to strip out the extra quotes around the value added by |
| 737 | // `JSON.stringify`. Also do custom escaping on `"` characters to prevent `JSON.stringify` |
| 738 | // from escaping them as if they were part of a string. |
| 739 | const stringifiedValue = JSON.stringify(value, (_, v) => |
| 740 | v instanceof RegExp |
| 741 | ? `◬MAT_RE_ESCAPE◬${v.toString().replace(/"/g, '◬MAT_RE_ESCAPE◬')}◬MAT_RE_ESCAPE◬` |
| 742 | : v, |
| 743 | ); |
| 744 | // Strip out the extra quotes around regexes and put back the manually escaped `"` characters. |
| 745 | return stringifiedValue |
| 746 | .replace(/"◬MAT_RE_ESCAPE◬|◬MAT_RE_ESCAPE◬"/g, '') |
| 747 | .replace(/◬MAT_RE_ESCAPE◬/g, '"'); |
| 748 | } catch { |
| 749 | // `JSON.stringify` will throw if the object is cyclical, |
| 750 | // in this case the best we can do is report the value as `{...}`. |
| 751 | return '{...}'; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * Splits up a compound selector into its parts and escapes any quoted content. The quoted content |