text-sanctifier 1.3.3 → 1.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/reveal.js ADDED
@@ -0,0 +1,40 @@
1
+ // ./src/reveal.js
2
+
3
+ import { inspectTextDetailed } from './inspectText.js';
4
+
5
+ /** @typedef {import('./inspectText.js').TrashCategory} TrashCategory */
6
+
7
+ /**
8
+ * @typedef {Object} RevealOptions
9
+ * @property {(codePoint: number, category: TrashCategory) => string} [format] - Text shown in place of
10
+ * each hidden character. Default: `<U+XXXX>` (at least four uppercase hex digits).
11
+ */
12
+
13
+ /**
14
+ * Makes hidden characters visible: every character `inspectTextDetailed` would report is
15
+ * replaced by a marker such as `<U+200B>`, and everything else is kept as it is,
16
+ * including tabs, newlines and emoji. Use it to show hidden text in logs, diffs, review
17
+ * tools and agent tool output. A literal `<U+200B>` already in the text looks the same as
18
+ * a marker; pass `format` to choose markers that can't occur in your input.
19
+ *
20
+ * @param {string} text
21
+ * @param {RevealOptions} [options]
22
+ * @returns {string}
23
+ */
24
+ export function revealInvisible(text, options = {}) {
25
+ if (typeof text !== 'string') {
26
+ throw new TypeError('revealInvisible expects a string input.');
27
+ }
28
+ const format = options && typeof options.format === 'function'
29
+ ? options.format
30
+ : (codePoint) => '<U+' + codePoint.toString(16).toUpperCase().padStart(4, '0') + '>';
31
+
32
+ // One finding per hidden character, in offset order; code points above U+FFFF span two units
33
+ let out = '';
34
+ let last = 0;
35
+ for (const f of inspectTextDetailed(text, { maxFindings: Infinity }).findings) {
36
+ out += text.slice(last, f.offset) + format(f.codePoint, f.category);
37
+ last = f.offset + (f.codePoint > 0xFFFF ? 2 : 1);
38
+ }
39
+ return out + text.slice(last);
40
+ }