44
55> ** 🔒 Simple PII redaction library for Node.js**
66
7- A fast, zero-dependency library that redacts PII from text. Works completely offline. No API keys, no setup, just install and use.
7+ A fast, zero-dependency library that redacts PII from text using regex patterns . Works completely offline. No API keys, no setup, just install and use.
88
9- ## ⚡ Features
10-
11- - ** <1ms per operation** - Optimized regex engine
12- - ** Zero dependencies** - Pure TypeScript, no bloat
13- - ** Works offline** - No internet or API keys needed
14- - ** TypeScript** - Full type safety and IDE support
15-
16- ### Requirements
17-
18- - ** Node.js 18+**
19- - ** TypeScript 5.0+** (optional, but recommended)
20-
21- ## 🚀 Installation & Usage
9+ ## 🚀 Installation
2210
2311``` bash
2412npm install @redactpii/node
@@ -28,7 +16,7 @@ pnpm add @redactpii/node
2816yarn add @redactpii/node
2917```
3018
31- ### 🔥 Quick Start
19+ ## 🔥 Quick Start
3220
3321``` typescript
3422import { Redactor } from ' @redactpii/node' ;
@@ -38,29 +26,28 @@ const clean = redactor.redact('Hi David Johnson, call 555-555-5555');
3826// Result: "Hi PERSON_NAME, call PHONE_NUMBER"
3927```
4028
41- ### 🎯 PII Detection
29+ ## 🎯 Built-in PII Detection Patterns
4230
43- Built-in patterns for:
31+ The library includes regex patterns for:
4432
4533- ** 👤 Names** - Person identification (greeting-based detection)
4634- ** 📧 Emails** - Email addresses
4735- ** 📞 Phones** - US phone numbers (all formats)
4836- ** 💳 Credit Cards** - Visa, Mastercard, Amex, Diners Club
4937- ** 🆔 SSN** - US Social Security Numbers
5038
51- ### 🔍 Check for PII Without Redacting
39+ ## 🔍 Check for PII Without Redacting
5240
5341``` typescript
5442const redactor = new Redactor ({ rules: { EMAIL: true } });
5543
5644if (redactor .hasPII (' Contact test@example.com for details' )) {
5745 console .log (' PII detected!' );
58- // Now redact it
5946 const clean = redactor .redact (' Contact test@example.com for details' );
6047}
6148```
6249
63- ### 📦 Redact Objects
50+ ## 📦 Redact Objects
6451
6552``` typescript
6653const redactor = new Redactor ({ rules: { EMAIL: true } });
@@ -83,70 +70,11 @@ const clean = redactor.redactObject(user);
8370// }
8471```
8572
86- ### 🤖 Using with LLMs (OpenAI, LangChain)
87-
88- Protect PII ** before** it hits AI APIs.
89-
90- <details >
91- <summary ><b >Example: Using with OpenAI Client</b ></summary >
92-
93- ``` typescript
94- import { Redactor } from ' @redactpii/node' ;
95- import OpenAI from ' openai' ;
96-
97- const redactor = new Redactor ({
98- rules: { SSN: true , EMAIL: true },
99- });
100-
101- const openai = new OpenAI ();
102-
103- // 1. Redact the prompt BEFORE you send it
104- const rawPrompt = ' My SSN is 123-45-6789 and my email is test@example.com' ;
105- const safePrompt = redactor .redact (rawPrompt );
106-
107- // 2. Send the "safe" prompt to the LLM
108- const completion = await openai .chat .completions .create ({
109- messages: [{ role: ' user' , content: safePrompt }],
110- model: ' gpt-4o' ,
111- });
112- ```
73+ ## 🎨 Customization
11374
114- </ details >
75+ ### Configure Rules
11576
116- <details >
117- <summary ><b >Example: Using with LangChain</b ></summary >
118-
119- ``` typescript
120- import { Redactor } from ' @redactpii/node' ;
121- import { ChatOpenAI } from ' @langchain/openai' ;
122-
123- const redactor = new Redactor ({
124- rules: { EMAIL: true },
125- });
126- const model = new ChatOpenAI ();
127-
128- // Create a "runnable" middleware to redact input
129- const redactingMiddleware = (input : { query: string }) => {
130- if (redactor .hasPII (input .query )) {
131- const safeQuery = redactor .redact (input .query );
132- return { ... input , query: safeQuery };
133- }
134- return input ;
135- };
136-
137- // Build your chain
138- const chain = redactingMiddleware .pipe (model );
139- // ... etc
140-
141- // Run the chain with PII
142- const result = await chain .invoke ({ query: ' My email is john@acme.com' });
143- ```
144-
145- </details >
146-
147- ### 🎨 Customization
148-
149- #### Configure Rules
77+ Enable or disable specific PII detection patterns:
15078
15179``` typescript
15280const redactor = new Redactor ({
@@ -160,7 +88,9 @@ const redactor = new Redactor({
16088});
16189```
16290
163- #### Custom Regex Patterns
91+ ### Custom Regex Patterns
92+
93+ Add your own regex patterns for domain-specific PII:
16494
16595``` typescript
16696const redactor = new Redactor ({
@@ -172,7 +102,9 @@ const redactor = new Redactor({
172102});
173103```
174104
175- #### Global Replacement
105+ ### Global Replacement
106+
107+ Use a single replacement string for all PII types:
176108
177109``` typescript
178110const redactor = new Redactor ({
@@ -183,9 +115,9 @@ const redactor = new Redactor({
183115redactor .redact (' test@example.com' ); // "[REDACTED]"
184116```
185117
186- #### Anonymization with Unique IDs
118+ ### Anonymization with Unique IDs
187119
188- Replace the same PII value with the same token throughout the text. Perfect for preserving relationships while protecting privacy.
120+ Replace the same PII value with the same token throughout the text:
189121
190122``` typescript
191123const redactor = new Redactor ({
@@ -212,9 +144,9 @@ const clean = redactor.redactObject(user);
212144// }
213145```
214146
215- #### Aggressive Mode
147+ ### Aggressive Mode
216148
217- More permissive patterns to catch obfuscated or unusual PII formatting.
149+ Use more permissive regex patterns to catch obfuscated or unusual PII formatting:
218150
219151``` typescript
220152const redactor = new Redactor ({
@@ -234,7 +166,7 @@ redactor.redact('Card ending in ****-****-****-1234');
234166// and won't catch these variations
235167```
236168
237- ### ❓ FAQ & Limitations
169+ ## ❓ FAQ
238170
239171** Is this regex-based?**
240172Yes, this library uses regex patterns for detection. It's fast and works offline, but has limitations.
@@ -247,43 +179,3 @@ The built-in patterns cover common, obvious PII types (emails, SSNs, credit card
247179
248180** Anonymization vs Redaction?**
249181By default, this library does ** redaction** (replacement with labels like ` EMAIL_ADDRESS ` ). However, you can enable ** anonymization** by setting ` anonymize: true ` , which replaces the same PII value with the same unique token (e.g., ` EMAIL_1 ` , ` EMAIL_2 ` ) throughout the text, preserving relationships while protecting privacy.
250-
251- ** When should I use this?**
252-
253- - ✅ Quick redaction of well-formatted, standard PII
254- - ✅ Pre-processing before sending data to LLMs/APIs
255- - ✅ Local, offline PII redaction
256- - ❌ Handling messy/unstructured data with misspellings
257- - ❌ Need for reversible anonymization
258-
259- ## 🧪 Quality Assurance
260-
261- - ** 34+ comprehensive tests** covering all APIs and edge cases
262- - ** 100% TypeScript** with strict mode
263- - ** Zero unsafe operations** - full type safety
264- - ** Pre-commit hooks** - automatic linting and type checking
265-
266- ### 🏃♂️ Development
267-
268- ``` bash
269- # Install dependencies
270- pnpm install
271-
272- # Run tests
273- pnpm test
274-
275- # Run with coverage
276- pnpm run coverage
277-
278- # Type checking
279- pnpm run typecheck
280-
281- # Linting
282- pnpm run lint
283-
284- # Full verification suite
285- pnpm run verify_all
286-
287- # Build for production
288- pnpm run build
289- ```
0 commit comments