text-sanctifier 1.3.4 → 1.3.6

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.
@@ -1,18 +1,83 @@
1
1
  /**
2
- * @typedef {Object} SanctifyOptions
3
- * @property {boolean} [purgeInvisibleChars]
4
- * @property {boolean} [purgeEmojis]
5
- * @property {boolean} [nukeControls]
6
- * @property {boolean} [keyboardOnlyFilter]
7
- * @property {boolean} [normalizeNewlines]
8
- * @property {boolean} [trimSpacesAroundNewlines]
9
- * @property {boolean} [collapseNewLines]
10
- * @property {boolean} [preserveParagraphs]
11
- * @property {boolean} [collapseSpaces]
12
- * @property {boolean} [finalTrim]
13
- * @property {number} [maxLength]
14
- * @property {'throw'|'truncate'|'noop'} [onMaxLength]
2
+ * Strict sanitizer:
3
+ * - Purge emojis
4
+ * - Collapse all newlines
5
+ * - Collapse spaces
6
+ * - Nuke control characters
7
+ * @param {string} text
8
+ * @returns {string}
15
9
  */
10
+ export function sanctifyStrict(text: string): string;
11
+ /**
12
+ * Loose sanitizer:
13
+ * - Collapse spaces
14
+ * - Preserve paragraphs
15
+ * - Normalize newlines
16
+ * @param {string} text
17
+ * @returns {string}
18
+ */
19
+ export function sanctifyLoose(text: string): string;
20
+ /**
21
+ * Keyboard-only (with emojis):
22
+ * - Keeps emojis and printable ASCII
23
+ * - Strips non-standard characters
24
+ * - Normalizes typographic trash
25
+ * @param {string} text
26
+ * @returns {string}
27
+ */
28
+ export function sanctifyKeyboardOnlyEmoji(text: string): string;
29
+ /**
30
+ * Keyboard-only (strict):
31
+ * - Removes emojis
32
+ * - Collapses all whitespace
33
+ * - Restricts to printable ASCII only
34
+ * @param {string} text
35
+ * @returns {string}
36
+ */
37
+ export function sanctifyKeyboardOnly(text: string): string;
38
+ /**
39
+ * Pasted text and model output:
40
+ * - Look-alike spaces (NBSP, narrow NBSP, typographic spaces) become ordinary spaces,
41
+ * so words never run together
42
+ * - Zero-width spaces, word joiners and BOMs are removed
43
+ * - Newlines are normalized to `\n`
44
+ * Keeps emoji joiners, variation selectors, bidi marks, indentation and line breaks.
45
+ * @param {string} text
46
+ * @returns {string}
47
+ */
48
+ export function sanctifyPasted(text: string): string;
49
+ /**
50
+ * Text Sanctifier
51
+ *
52
+ * Brutal text normalizer and invisible trash scrubber,
53
+ * configurable to kill whatever ghosts you want dead.
54
+ *
55
+ * ⚠️ Note: This is plain-text normalization/filtering — not an HTML/XSS sanitizer.
56
+ *
57
+ * @param {string} text
58
+ * @param {boolean} [purgeInvisibleChars=false] - Remove ZWSP, NBSP, bidi, tags, variation selectors, etc.
59
+ * @param {boolean} [purgeEmojis=false] - Remove emoji characters entirely.
60
+ * @param {boolean} [nukeControls=false] - Remove non-whitespace control characters.
61
+ * @param {boolean} [keyboardOnlyFilter=false] - Keep printable ASCII + full emoji sequences only (drops other Unicode).
62
+ * @param {boolean} [normalizeNewlines=false] - Convert all newlines to `\n`.
63
+ * @param {boolean} [trimSpacesAroundNewlines=false] - Remove spaces/tabs around newlines.
64
+ * @param {boolean} [collapseNewLines=false] - Collapse `\n` runs (optionally preserve paragraphs).
65
+ * @param {boolean} [preserveParagraphs=false] - Preserve paragraph breaks when collapsing newlines.
66
+ * @param {boolean} [collapseSpaces=false] - Collapse multiple spaces into one.
67
+ * @param {boolean} [finalTrim=false] - `.trim()` the final output (head/tail).
68
+ * @param {number} [maxLength=Infinity] - Hard input length cap (UTF-16 code units via `text.length`).
69
+ * @param {'throw'|'truncate'|'noop'} [onMaxLength='throw'] - Behavior when `text.length` exceeds `maxLength`:
70
+ * - `'throw'`: throw a `RangeError`
71
+ * - `'truncate'`: slice to `maxLength` before processing
72
+ * - `'noop'`: return the original input unchanged (skips processing)
73
+ * @param {boolean} [normalizeSpaces=false] - Replace look-alike spaces with an ordinary space. Runs first,
74
+ * so `purgeInvisibleChars` no longer joins the words around them.
75
+ * @param {boolean} [purgeZeroWidthSpaces=false] - Remove zero-width spaces, word joiners and BOMs only.
76
+ * @returns {string}
77
+ * @throws {TypeError} If `text` is not a string.
78
+ * @throws {RangeError} If input exceeds `maxLength` and `onMaxLength` is `'throw'`.
79
+ */
80
+ export function sanctifyText(text: string, purgeInvisibleChars?: boolean, purgeEmojis?: boolean, nukeControls?: boolean, keyboardOnlyFilter?: boolean, normalizeNewlines?: boolean, trimSpacesAroundNewlines?: boolean, collapseNewLines?: boolean, preserveParagraphs?: boolean, collapseSpaces?: boolean, finalTrim?: boolean, maxLength?: number, onMaxLength?: "throw" | "truncate" | "noop", normalizeSpaces?: boolean, purgeZeroWidthSpaces?: boolean): string;
16
81
  /**
17
82
  * Summons a customized sanctifier function with pre-bound booleans.
18
83
  *
@@ -31,9 +96,36 @@
31
96
  * @param {boolean} [defaultOptions.finalTrim]
32
97
  * @param {number} [defaultOptions.maxLength=Infinity] - Hard input length cap (UTF-16 code units).
33
98
  * @param {'throw' | 'truncate' | 'noop'} [defaultOptions.onMaxLength='throw'] - Behavior when input exceeds maxLength.
99
+ * @param {boolean} [defaultOptions.normalizeSpaces]
100
+ * @param {boolean} [defaultOptions.purgeZeroWidthSpaces]
34
101
  * @returns {(text: string) => string}
102
+ *
103
+ * The presets are attached as properties (`summonSanctifier.strict`, `.loose`,
104
+ * `.keyboardOnlyEmoji`, `.keyboardOnly`, `.pasted`) and are also standalone exports.
105
+ * Built in a call marked pure, so a bundle that imports only a preset leaves it out.
106
+ * @type {typeof createSanctifier & {
107
+ * strict: typeof sanctifyStrict,
108
+ * loose: typeof sanctifyLoose,
109
+ * keyboardOnlyEmoji: typeof sanctifyKeyboardOnlyEmoji,
110
+ * keyboardOnly: typeof sanctifyKeyboardOnly,
111
+ * pasted: typeof sanctifyPasted,
112
+ * }}
35
113
  */
36
- export function summonSanctifier(defaultOptions?: {
114
+ export const summonSanctifier: typeof createSanctifier & {
115
+ strict: typeof sanctifyStrict;
116
+ loose: typeof sanctifyLoose;
117
+ keyboardOnlyEmoji: typeof sanctifyKeyboardOnlyEmoji;
118
+ keyboardOnly: typeof sanctifyKeyboardOnly;
119
+ pasted: typeof sanctifyPasted;
120
+ };
121
+ /**
122
+ * Look-alike spaces: the `nonbreaking_space` category except U+3000, the ordinary
123
+ * space of CJK text. A test checks this against the category table.
124
+ */
125
+ export const SPACE_LOOKALIKES_REGEX: RegExp;
126
+ /** Zero-width space, word joiner and BOM: invisible and never needed in plain text. */
127
+ export const ZERO_WIDTH_SPACES_REGEX: RegExp;
128
+ export type SanctifyOptions = {
37
129
  purgeInvisibleChars?: boolean;
38
130
  purgeEmojis?: boolean;
39
131
  nukeControls?: boolean;
@@ -46,87 +138,60 @@ export function summonSanctifier(defaultOptions?: {
46
138
  finalTrim?: boolean;
47
139
  maxLength?: number;
48
140
  onMaxLength?: "throw" | "truncate" | "noop";
49
- }): (text: string) => string;
50
- export namespace summonSanctifier {
51
- /**
52
- * Strict sanitizer:
53
- * - Purge emojis
54
- * - Collapse all newlines
55
- * - Collapse spaces
56
- * - Nuke control characters
57
- */
58
- /** @param {string} text @returns {string} */
59
- function strict(text: string): string;
60
- /**
61
- * Loose sanitizer:
62
- * - Collapse spaces
63
- * - Preserve paragraphs
64
- * - Normalize newlines
65
- */
66
- /** @param {string} text @returns {string} */
67
- function loose(text: string): string;
68
141
  /**
69
- * Keyboard-only (with emojis):
70
- * - Keeps emojis and printable ASCII
71
- * - Strips non-standard characters
72
- * - Normalizes typographic trash
142
+ * - Replace look-alike spaces with an ordinary space (runs first).
73
143
  */
74
- /** @param {string} text @returns {string} */
75
- function keyboardOnlyEmoji(text: string): string;
144
+ normalizeSpaces?: boolean;
76
145
  /**
77
- * Keyboard-only (strict):
78
- * - Removes emojis
79
- * - Collapses all whitespace
80
- * - Restricts to printable ASCII only
146
+ * - Remove U+200B, U+2060 and U+FEFF only, keeping joiners and selectors.
81
147
  */
82
- /** @param {string} text @returns {string} */
83
- function keyboardOnly(text: string): string;
84
- }
85
- /**
86
- * Text Sanctifier
87
- *
88
- * Brutal text normalizer and invisible trash scrubber,
89
- * configurable to kill whatever ghosts you want dead.
90
- *
91
- * ⚠️ Note: This is plain-text normalization/filtering — not an HTML/XSS sanitizer.
92
- *
93
- * @param {string} text
94
- * @param {boolean} [purgeInvisibleChars=false] - Remove ZWSP, NBSP, bidi, tags, variation selectors, etc.
95
- * @param {boolean} [purgeEmojis=false] - Remove emoji characters entirely.
96
- * @param {boolean} [nukeControls=false] - Remove non-whitespace control characters.
97
- * @param {boolean} [keyboardOnlyFilter=false] - Keep printable ASCII + full emoji sequences only (drops other Unicode).
98
- * @param {boolean} [normalizeNewlines=false] - Convert all newlines to `\n`.
99
- * @param {boolean} [trimSpacesAroundNewlines=false] - Remove spaces/tabs around newlines.
100
- * @param {boolean} [collapseNewLines=false] - Collapse `\n` runs (optionally preserve paragraphs).
101
- * @param {boolean} [preserveParagraphs=false] - Preserve paragraph breaks when collapsing newlines.
102
- * @param {boolean} [collapseSpaces=false] - Collapse multiple spaces into one.
103
- * @param {boolean} [finalTrim=false] - `.trim()` the final output (head/tail).
104
- * @param {number} [maxLength=Infinity] - Hard input length cap (UTF-16 code units via `text.length`).
105
- * @param {'throw'|'truncate'|'noop'} [onMaxLength='throw'] - Behavior when `text.length` exceeds `maxLength`:
106
- * - `'throw'`: throw a `RangeError`
107
- * - `'truncate'`: slice to `maxLength` before processing
108
- * - `'noop'`: return the original input unchanged (skips processing)
109
- * @returns {string}
110
- * @throws {TypeError} If `text` is not a string.
111
- * @throws {RangeError} If input exceeds `maxLength` and `onMaxLength` is `'throw'`.
112
- */
113
- export function sanctifyText(text: string, purgeInvisibleChars?: boolean, purgeEmojis?: boolean, nukeControls?: boolean, keyboardOnlyFilter?: boolean, normalizeNewlines?: boolean, trimSpacesAroundNewlines?: boolean, collapseNewLines?: boolean, preserveParagraphs?: boolean, collapseSpaces?: boolean, finalTrim?: boolean, maxLength?: number, onMaxLength?: "throw" | "truncate" | "noop"): string;
148
+ purgeZeroWidthSpaces?: boolean;
149
+ };
150
+ import { CONTROL_CHARS_REGEX } from './categories.generated.js';
151
+ import { INVISIBLE_TRASH_REGEX } from './categories.generated.js';
152
+ import { ASCII_KEYBOARD_SAFE_REGEX } from './textPatterns.js';
153
+ import { EMOJI_REGEX } from './textPatterns.js';
154
+ import { normalizeTypographicJank } from './textPatterns.js';
114
155
  /**
115
- * Normalizes typographic Unicode punctuation into ASCII equivalents.
116
- * @param {string} text
117
- * @returns {string}
156
+ * @typedef {Object} SanctifyOptions
157
+ * @property {boolean} [purgeInvisibleChars]
158
+ * @property {boolean} [purgeEmojis]
159
+ * @property {boolean} [nukeControls]
160
+ * @property {boolean} [keyboardOnlyFilter]
161
+ * @property {boolean} [normalizeNewlines]
162
+ * @property {boolean} [trimSpacesAroundNewlines]
163
+ * @property {boolean} [collapseNewLines]
164
+ * @property {boolean} [preserveParagraphs]
165
+ * @property {boolean} [collapseSpaces]
166
+ * @property {boolean} [finalTrim]
167
+ * @property {number} [maxLength]
168
+ * @property {'throw'|'truncate'|'noop'} [onMaxLength]
169
+ * @property {boolean} [normalizeSpaces] - Replace look-alike spaces with an ordinary space (runs first).
170
+ * @property {boolean} [purgeZeroWidthSpaces] - Remove U+200B, U+2060 and U+FEFF only, keeping joiners and selectors.
118
171
  */
119
- export function normalizeTypographicJank(text: string): string;
120
172
  /**
121
- * Matches any character that is NOT:
122
- * - Printable ASCII (U+0020–U+007E)
123
- * - Newline characters: \n (LF), \r (CR)
124
- * This allows for \n, \r, and \r\n line endings.
173
+ * Summons a customized sanctifier function with pre-bound booleans.
174
+ *
175
+ * Accepts full flag names and returns a text-cleaning function.
176
+ *
177
+ * @param {Object} [defaultOptions={}]
178
+ * @param {boolean} [defaultOptions.purgeInvisibleChars]
179
+ * @param {boolean} [defaultOptions.purgeEmojis]
180
+ * @param {boolean} [defaultOptions.nukeControls]
181
+ * @param {boolean} [defaultOptions.keyboardOnlyFilter]
182
+ * @param {boolean} [defaultOptions.normalizeNewlines]
183
+ * @param {boolean} [defaultOptions.trimSpacesAroundNewlines]
184
+ * @param {boolean} [defaultOptions.collapseNewLines]
185
+ * @param {boolean} [defaultOptions.preserveParagraphs]
186
+ * @param {boolean} [defaultOptions.collapseSpaces]
187
+ * @param {boolean} [defaultOptions.finalTrim]
188
+ * @param {number} [defaultOptions.maxLength=Infinity] - Hard input length cap (UTF-16 code units).
189
+ * @param {'throw' | 'truncate' | 'noop'} [defaultOptions.onMaxLength='throw'] - Behavior when input exceeds maxLength.
190
+ * @param {boolean} [defaultOptions.normalizeSpaces]
191
+ * @param {boolean} [defaultOptions.purgeZeroWidthSpaces]
192
+ * @returns {(text: string) => string}
125
193
  */
126
- export const ASCII_KEYBOARD_SAFE_REGEX: RegExp;
127
- /** @type {RegExp} */
128
- export let EMOJI_REGEX: RegExp;
129
- export type SanctifyOptions = {
194
+ declare function createSanctifier(defaultOptions?: {
130
195
  purgeInvisibleChars?: boolean;
131
196
  purgeEmojis?: boolean;
132
197
  nukeControls?: boolean;
@@ -139,7 +204,7 @@ export type SanctifyOptions = {
139
204
  finalTrim?: boolean;
140
205
  maxLength?: number;
141
206
  onMaxLength?: "throw" | "truncate" | "noop";
142
- };
143
- import { CONTROL_CHARS_REGEX } from './categories.generated.js';
144
- import { INVISIBLE_TRASH_REGEX } from './categories.generated.js';
145
- export { CONTROL_CHARS_REGEX, INVISIBLE_TRASH_REGEX };
207
+ normalizeSpaces?: boolean;
208
+ purgeZeroWidthSpaces?: boolean;
209
+ }): (text: string) => string;
210
+ export { CONTROL_CHARS_REGEX, INVISIBLE_TRASH_REGEX, ASCII_KEYBOARD_SAFE_REGEX, EMOJI_REGEX, normalizeTypographicJank };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Normalizes typographic Unicode punctuation into ASCII equivalents.
3
+ * @param {string} text
4
+ * @returns {string}
5
+ */
6
+ export function normalizeTypographicJank(text: string): string;
7
+ /**
8
+ * Matches any character that is NOT:
9
+ * - Printable ASCII (U+0020–U+007E)
10
+ * - Newline characters: \n (LF), \r (CR)
11
+ * This allows for \n, \r, and \r\n line endings.
12
+ */
13
+ export const ASCII_KEYBOARD_SAFE_REGEX: RegExp;
14
+ /** @type {RegExp} */
15
+ export const EMOJI_REGEX: RegExp;
@@ -0,0 +1,2 @@
1
+ /** The installed version of text-sanctifier, as in its package.json. */
2
+ export const VERSION: "1.3.6";
package/package.json CHANGED
@@ -1,80 +1,79 @@
1
- {
2
- "name": "text-sanctifier",
3
- "version": "1.3.4",
4
- "type": "module",
5
- "description": "A brutal text normalizer and invisible trash scrubber for modern web projects.",
6
- "engines": {
7
- "node": ">=18"
8
- },
9
- "main": "./src/index.js",
10
- "module": "./src/index.js",
11
- "browser": "./dist/text-sanctifier.min.js",
12
- "bin": {
13
- "text-sanctifier": "bin/text-sanctifier.js"
14
- },
15
- "files": [
16
- "src",
17
- "bin",
18
- "dist/text-sanctifier.min.js",
19
- "dist/types",
20
- "LICENSE",
21
- "README.md",
22
- "ADDITIONAL_TERMS.md",
23
- "robots.txt"
24
- ],
25
- "exports": {
26
- ".": {
27
- "types": "./dist/types/index.d.ts",
28
- "default": "./src/index.js"
29
- },
30
- "./hidden": {
31
- "types": "./dist/types/hiddenText.d.ts",
32
- "default": "./src/hiddenText.js"
33
- },
34
- "./browser": {
35
- "types": "./dist/types/index.d.ts",
36
- "default": "./dist/text-sanctifier.min.js"
37
- }
38
- },
39
- "types": "./dist/types/index.d.ts",
40
- "sideEffects": false,
41
- "scripts": {
42
- "gen": "node scripts/gen-categories.js",
43
- "gen:check": "node scripts/gen-categories.js --check",
44
- "build": "node scripts/buildc.js",
45
- "build:types": "tsc -p tsconfig.types.json",
46
- "test:types": "tsc -p tests/types/tsconfig.json",
47
- "test": "node tests/sanctifyText.test.js && node tests/inspectTextDetailed.test.js && node tests/cli.test.js",
48
- "test-min": "node tests/sanctifyText.test.min.js",
49
- "ci": "npm run gen:check && npm run test && npm run build && npm run build:types && npm run test:types && npm run test-min"
50
- },
51
- "keywords": [
52
- "text",
53
- "sanitize",
54
- "normalize",
55
- "invisible",
56
- "scrubber",
57
- "unicode",
58
- "clean",
59
- "sanctify"
60
- ],
61
- "author": "WATT3D <WATT3D@protonmail.com>",
62
- "license": "SEE LICENSE IN LICENSE",
63
- "repository": {
64
- "type": "git",
65
- "url": "git+https://github.com/iWhatty/text-sanctifier.git"
66
- },
67
- "bugs": {
68
- "url": "https://github.com/iWhatty/text-sanctifier/issues"
69
- },
70
- "homepage": "https://github.com/iWhatty/text-sanctifier#readme",
71
- "directories": {
72
- "test": "tests"
73
- },
74
- "devDependencies": {
75
- "esbuild": "^0.25.3",
76
- "google-closure-compiler": "^20240317.0.0",
77
- "terser": "^5.39.0",
78
- "typescript": "^5.9.3"
79
- }
80
- }
1
+ {
2
+ "name": "text-sanctifier",
3
+ "version": "1.3.6",
4
+ "type": "module",
5
+ "description": "A brutal text normalizer and invisible trash scrubber for modern web projects.",
6
+ "engines": {
7
+ "node": ">=18"
8
+ },
9
+ "main": "./src/index.js",
10
+ "module": "./src/index.js",
11
+ "browser": "./dist/text-sanctifier.min.js",
12
+ "bin": {
13
+ "text-sanctifier": "bin/text-sanctifier.js"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "bin",
18
+ "dist/text-sanctifier.min.js",
19
+ "dist/types",
20
+ "LICENSE",
21
+ "README.md",
22
+ "ADDITIONAL_TERMS.md",
23
+ "robots.txt"
24
+ ],
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/types/index.d.ts",
28
+ "default": "./src/index.js"
29
+ },
30
+ "./hidden": {
31
+ "types": "./dist/types/hiddenText.d.ts",
32
+ "default": "./src/hiddenText.js"
33
+ },
34
+ "./browser": {
35
+ "types": "./dist/types/index.d.ts",
36
+ "default": "./dist/text-sanctifier.min.js"
37
+ }
38
+ },
39
+ "types": "./dist/types/index.d.ts",
40
+ "sideEffects": false,
41
+ "scripts": {
42
+ "gen": "node scripts/gen-categories.js && node scripts/gen-version.js",
43
+ "gen:check": "node scripts/gen-categories.js --check && node scripts/gen-version.js --check",
44
+ "build": "node scripts/build.js",
45
+ "build:types": "tsc -p tsconfig.types.json",
46
+ "test:types": "tsc -p tests/types/tsconfig.json",
47
+ "test": "node tests/sanctifyText.test.js && node tests/inspectTextDetailed.test.js && node --expose-gc tests/hiddenText.test.js && node tests/cli.test.js",
48
+ "test-min": "node tests/sanctifyText.test.min.js",
49
+ "ci": "npm run gen:check && npm run test && npm run build && npm run size && npm run build:types && npm run test:types && npm run test-min",
50
+ "size": "node scripts/check-size.js"
51
+ },
52
+ "keywords": [
53
+ "text",
54
+ "sanitize",
55
+ "normalize",
56
+ "invisible",
57
+ "scrubber",
58
+ "unicode",
59
+ "clean",
60
+ "sanctify"
61
+ ],
62
+ "author": "WATT3D <WATT3D@protonmail.com>",
63
+ "license": "SEE LICENSE IN LICENSE",
64
+ "repository": {
65
+ "type": "git",
66
+ "url": "git+https://github.com/iWhatty/text-sanctifier.git"
67
+ },
68
+ "bugs": {
69
+ "url": "https://github.com/iWhatty/text-sanctifier/issues"
70
+ },
71
+ "homepage": "https://github.com/iWhatty/text-sanctifier#readme",
72
+ "directories": {
73
+ "test": "tests"
74
+ },
75
+ "devDependencies": {
76
+ "esbuild": "^0.25.3",
77
+ "typescript": "^5.9.3"
78
+ }
79
+ }