-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
524 lines (508 loc) · 14.4 KB
/
mod.ts
File metadata and controls
524 lines (508 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import {
any,
anyChar,
eof,
failure,
manyTill,
map,
optional,
type Parser,
recognizeAt,
seq,
skip1,
space,
step,
} from "@claudiu-ceia/combine";
import { __, dot, word } from "./src/common.ts";
import { asyncScan, type AsyncScanOptions } from "./src/async.ts";
import { buildSpanTree, renderMapNode, renderNode } from "./src/render.ts";
import type { RenderFn, RenderMapFn } from "./src/render.ts";
import { Quantity, type QuantityEntity } from "./src/Quantity.ts";
import { Range, type RangeEntity } from "./src/Range.ts";
import { Temperature, type TemperatureEntity } from "./src/Temperature.ts";
import { Time, type TimeEntity } from "./src/Time.ts";
import { Location, type LocationEntity } from "./src/Location.ts";
import { URL, type URLEntity } from "./src/URL.ts";
import { Email, type EmailEntity } from "./src/Email.ts";
import { Institution, type InstitutionEntity } from "./src/Institution.ts";
import { Language, type LanguageEntity } from "./src/Language.ts";
import { Phone, type PhoneEntity } from "./src/Phone.ts";
import { IPAddress, type IPAddressEntity } from "./src/IPAddress.ts";
import { SSN, type SSNEntity } from "./src/SSN.ts";
import { CreditCard, type CreditCardEntity } from "./src/CreditCard.ts";
import { UUID, type UUIDEntity } from "./src/UUID.ts";
import { ApiKey, type ApiKeyEntity } from "./src/ApiKey.ts";
import { IBAN, type IBANEntity } from "./src/IBAN.ts";
import { MACAddress, type MACAddressEntity } from "./src/MACAddress.ts";
import { JWT, type JWTEntity } from "./src/JWT.ts";
import {
CryptoAddress,
type CryptoAddressEntity,
} from "./src/CryptoAddress.ts";
import { BIC, type BICEntity } from "./src/BIC.ts";
/**
* Union of all entity types produced by the built-in parsers.
*
* This is the element type returned by `Duckling().extract(...)` when no
* custom parser list is provided.
*/
export type AnyEntity =
| TemperatureEntity
| TimeEntity
| QuantityEntity
| RangeEntity<TemperatureEntity>
| RangeEntity<TimeEntity>
| LocationEntity
| URLEntity
| EmailEntity
| InstitutionEntity
| LanguageEntity
| PhoneEntity
| IPAddressEntity
| SSNEntity
| CreditCardEntity
| UUIDEntity
| ApiKeyEntity
| IBANEntity
| MACAddressEntity
| JWTEntity
| CryptoAddressEntity
| BICEntity;
const DefaultParsers: [Parser<AnyEntity>, ...Parser<AnyEntity>[]] = [
Range.parser,
Time.parser,
Temperature.parser,
Quantity.parser,
Location.parser,
URL.parser,
Email.parser,
Institution.parser,
Language.parser,
Phone.parser,
IPAddress.parser,
SSN.parser,
CreditCard.parser,
UUID.parser,
ApiKey.parser,
IBAN.parser,
MACAddress.parser,
JWT.parser,
CryptoAddress.parser,
BIC.parser,
];
/**
* Union of entity types considered Personally Identifiable Information (PII).
*
* Covers: email addresses, phone numbers, IP addresses, Social Security
* Numbers, credit card numbers, UUIDs, API keys, IBANs, MAC addresses,
* JWTs, and cryptocurrency wallet addresses.
*/
export type PIIEntity =
| EmailEntity
| PhoneEntity
| IPAddressEntity
| SSNEntity
| CreditCardEntity
| UUIDEntity
| ApiKeyEntity
| IBANEntity
| MACAddressEntity
| JWTEntity
| CryptoAddressEntity
| BICEntity;
/**
* Pre-built parser tuple targeting PII entities.
*
* Pass this to {@link Duckling} for a quick redaction pipeline:
*
* @example
* ```ts
* import { Duckling, PIIParsers } from "@claudiu-ceia/ts-duckling";
*
* Duckling(PIIParsers).redact("Email me at a@b.com, SSN 123-45-6789");
* // → "Email me at ███████████████, SSN ███████████"
* ```
*/
export const PIIParsers: ParserTuple<
[
EmailEntity,
PhoneEntity,
IPAddressEntity,
SSNEntity,
CreditCardEntity,
UUIDEntity,
ApiKeyEntity,
IBANEntity,
MACAddressEntity,
JWTEntity,
CryptoAddressEntity,
BICEntity,
]
> = [
Email.parser,
Phone.parser,
IPAddress.parser,
SSN.parser,
CreditCard.parser,
UUID.parser,
ApiKey.parser,
IBAN.parser,
MACAddress.parser,
JWT.parser,
CryptoAddress.parser,
BIC.parser,
];
type NonEmptyArray<T> = [T, ...T[]];
type ParserTuple<T extends NonEmptyArray<unknown>> = {
[K in keyof T]: Parser<T[K]>;
};
/**
* Options for {@link Duckling().redact}.
*
* @property mask - The character used to replace each redacted character.
* Defaults to `"█"`.
* @property kinds - If provided, only entities whose `kind` matches one of
* these values are redacted. When omitted **all** extracted
* entities are redacted.
*/
export interface RedactOptions<K extends string = string> {
mask?: string;
kinds?: K[];
}
export type { AsyncScanOptions } from "./src/async.ts";
export type {
RenderEntity,
RenderFn,
RenderMapEntity,
RenderMapFn,
} from "./src/render.ts";
/**
* Create an extractor that scans free-form text and returns all matching
* entities.
*
* Called without arguments, uses all built-in parsers and returns
* `AnyEntity[]`.
*
* @example
* ```ts
* const entities = Duckling().extract("It's 90F outside, email me at a@b.com");
* // entities: AnyEntity[]
* ```
*/
export function Duckling(): {
extract: (text: string) => AnyEntity[];
/** @experimental */
extractAsync: (
text: string,
opts?: AsyncScanOptions,
) => Promise<AnyEntity[]>;
redact: (text: string, opts?: RedactOptions<AnyEntity["kind"]>) => string;
render: (text: string, fn: RenderFn<AnyEntity>) => string;
/** @experimental */
renderAsync: (
text: string,
fn: RenderFn<AnyEntity>,
opts?: AsyncScanOptions,
) => Promise<string>;
renderMap: <R>(text: string, fn: RenderMapFn<AnyEntity, R>) => (string | R)[];
/** @experimental */
renderMapAsync: <R>(
text: string,
fn: RenderMapFn<AnyEntity, R>,
opts?: AsyncScanOptions,
) => Promise<(string | R)[]>;
};
/**
* Create an extractor with a specific set of parsers. The return type is
* automatically narrowed to the union of the entity types produced by the
* given parsers.
*
* @param parsers - A non-empty array of entity parsers.
*
* @example
* ```ts
* const entities = Duckling([Email.parser, URL.parser]).extract(
* "Reach me at a@b.com or https://example.com",
* );
* // entities: (EmailEntity | URLEntity)[]
* ```
*/
export function Duckling<T extends NonEmptyArray<unknown>>(
parsers: ParserTuple<T>,
): {
extract: (text: string) => T[number][];
/** @experimental */
extractAsync: (
text: string,
opts?: AsyncScanOptions,
) => Promise<T[number][]>;
redact: (
text: string,
opts?: RedactOptions<
T[number] extends { kind: infer K extends string } ? K : string
>,
) => string;
render: (text: string, fn: RenderFn<T[number]>) => string;
/** @experimental */
renderAsync: (
text: string,
fn: RenderFn<T[number]>,
opts?: AsyncScanOptions,
) => Promise<string>;
renderMap: <R>(
text: string,
fn: RenderMapFn<T[number], R>,
) => (string | R)[];
/** @experimental */
renderMapAsync: <R>(
text: string,
fn: RenderMapFn<T[number], R>,
opts?: AsyncScanOptions,
) => Promise<(string | R)[]>;
};
// deno-lint-ignore no-explicit-any
export function Duckling(parsers?: any): any {
const p: Parser<unknown>[] = parsers ?? DefaultParsers;
let parser: Parser<unknown>;
if (p.length === 0) {
parser = (ctx) => failure(ctx, "entity");
}
const entities = map(
step(
recognizeAt(...(p as NonEmptyArray<Parser<unknown>>)),
"shortest",
),
(recs) => recs.map((r) => r.value),
);
const unstructured = any(dot(word), __(word), space());
parser = map(
seq(
optional(space()),
map(
manyTill(
any(entities, skip1(unstructured), skip1(anyChar())),
skip1(eof()),
),
([...matches]) =>
matches.filter((m): m is AnyEntity[] => Array.isArray(m)).flat(),
),
),
([, res]) => res,
);
const parse = (input: string): unknown[] => {
const result = parser({ text: input, index: 0 });
return result.success ? (result.value as unknown[]) : [];
};
return {
extract: parse,
/**
* @experimental
*
* Async version of {@link extract} that yields to the event loop
* periodically, preventing main-thread blocking on large inputs.
*
* Uses `scheduler.yield()` when available (Chrome 129+, Edge 129+,
* Firefox 142+), falling back to `setTimeout(0)`.
*
* @example
* ```ts
* const controller = new AbortController();
* const entities = await Duckling().extractAsync(longText, {
* signal: controller.signal,
* yieldEvery: 256,
* });
* ```
*/
extractAsync: (
input: string,
opts?: AsyncScanOptions,
): Promise<unknown[]> => {
return asyncScan(
input,
p as NonEmptyArray<Parser<unknown>>,
opts,
);
},
/**
* Replace entity spans using a callback function.
*
* Entities are arranged into a tree: wider spans become parents of
* narrower ones contained within them. The callback receives each entity
* together with the already-rendered text of its children, allowing
* nested rendering (e.g. an SSN wrapping its quantity sub-parts).
*
* Return a replacement string to transform the span, or `undefined` to
* leave it as-is.
*
* @example
* ```ts
* // Wrap entities in HTML tags
* Duckling().render(
* "Email me at a@b.com",
* ({ entity, children }) =>
* `<mark data-kind="${entity.kind}">${children}</mark>`,
* );
* // → 'Email me at <mark data-kind="email">a@b.com</mark>'
* ```
*/
render: (
input: string,
fn: RenderFn<unknown>,
): string => {
const all = parse(input) as {
kind: string;
start: number;
end: number;
text: string;
}[];
if (all.length === 0) return input;
const tree = buildSpanTree(all, input.length);
return renderNode(tree, input, fn);
},
/**
* @experimental
*
* Async version of {@link render} that yields to the event loop
* during entity extraction.
*/
renderAsync: async (
input: string,
fn: RenderFn<unknown>,
opts?: AsyncScanOptions,
): Promise<string> => {
const all = (await asyncScan(
input,
p as NonEmptyArray<Parser<unknown>>,
opts,
)) as {
kind: string;
start: number;
end: number;
text: string;
}[];
if (all.length === 0) return input;
const tree = buildSpanTree(all, input.length);
return renderNode(tree, input, fn);
},
/**
* Map entity spans to arbitrary values, returning an array of segments.
*
* Like {@link render}, entities are arranged into a span tree. The
* callback receives each entity and its children as `(string | R)[]`
* segments — ideal for frameworks like React where you need element
* arrays rather than concatenated strings.
*
* @example
* ```tsx
* // React: wrap entities in <mark> elements
* const segments = Duckling([Email.parser]).renderMap<JSX.Element>(
* "Contact a@b.com please",
* ({ entity, children }) =>
* <mark data-kind={entity.kind}>{children}</mark>,
* );
* // → ["Contact ", <mark data-kind="email">a@b.com</mark>, " please"]
* ```
*/
renderMap: <R>(
input: string,
// deno-lint-ignore no-explicit-any
fn: RenderMapFn<any, R>,
): (string | R)[] => {
const all = parse(input) as {
kind: string;
start: number;
end: number;
text: string;
}[];
if (all.length === 0) return [input];
const tree = buildSpanTree(all, input.length);
return renderMapNode<R>(tree, input, fn);
},
/**
* @experimental
*
* Async version of {@link renderMap} that yields to the event loop
* during entity extraction.
*/
renderMapAsync: async <R>(
input: string,
// deno-lint-ignore no-explicit-any
fn: RenderMapFn<any, R>,
opts?: AsyncScanOptions,
): Promise<(string | R)[]> => {
const all = (await asyncScan(
input,
p as NonEmptyArray<Parser<unknown>>,
opts,
)) as {
kind: string;
start: number;
end: number;
text: string;
}[];
if (all.length === 0) return [input];
const tree = buildSpanTree(all, input.length);
return renderMapNode<R>(tree, input, fn);
},
/**
* Extract entities and replace each matched span with a mask character.
*
* Implemented on top of {@link render}. When `opts.kinds` is provided,
* only entities of those kinds are redacted; otherwise **every** detected
* entity is masked.
*
* @example
* ```ts
* Duckling().redact("Email me at a@b.com, SSN 123-45-6789");
* // → "Email me at ███████, SSN ███████████"
*
* Duckling().redact("Call +14155552671", { kinds: ["phone"] });
* // → "Call ████████████"
*
* Duckling().redact("SSN 123-45-6789", { mask: "X" });
* // → "SSN XXXXXXXXXXX"
* ```
*/
redact: (
input: string,
opts: RedactOptions = {},
): string => {
const mask = opts.mask ?? "█";
const all = parse(input) as {
kind: string;
start: number;
end: number;
text: string;
}[];
const filtered = opts.kinds
? all.filter((e) => opts.kinds!.includes(e.kind))
: all;
if (filtered.length === 0) return input;
const tree = buildSpanTree(filtered, input.length);
return renderNode(tree, input, ({ entity }) => {
return mask.repeat(entity.end - entity.start);
});
},
};
}
export * from "./src/Entity.ts";
export * from "./src/Quantity.ts";
export * from "./src/Range.ts";
export * from "./src/Temperature.ts";
export * from "./src/Time.ts";
export * from "./src/Location.ts";
export * from "./src/URL.ts";
export * from "./src/Email.ts";
export * from "./src/Phone.ts";
export * from "./src/IPAddress.ts";
export * from "./src/SSN.ts";
export * from "./src/CreditCard.ts";
export * from "./src/UUID.ts";
export * from "./src/Institution.ts";
export * from "./src/Language.ts";
export * from "./src/ApiKey.ts";
export * from "./src/IBAN.ts";
export * from "./src/MACAddress.ts";
export * from "./src/JWT.ts";
export * from "./src/CryptoAddress.ts";
export * from "./src/BIC.ts";