@maskera/core 0.8.5 → 0.9.0

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/dist/index.d.cts CHANGED
@@ -52,7 +52,7 @@ declare function canonicalize(input: string): CanonicalText;
52
52
  * Built-in PII labels. The `string` fallback lets you add custom detectors
53
53
  * with arbitrary labels without fighting the type system.
54
54
  */
55
- type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "KONTONUMMER" | "JOURNALNUMMER" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
55
+ type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "KONTONUMMER" | "JOURNALNUMMER" | "FODELSEDATUM" | "FASTIGHETSBETECKNING" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
56
56
  /** A raw match found by a detector, before placeholder assignment. */
57
57
  interface Detection {
58
58
  /** Inclusive start index into the source string. */
@@ -85,6 +85,17 @@ interface RedactOptions {
85
85
  * Defaults to ``(label, i) => `[${label}_${i}]` ``.
86
86
  */
87
87
  placeholder?: (label: PiiLabel, index: number) => string;
88
+ /**
89
+ * Optional grouping key for placeholder reuse. Detections with the same
90
+ * label and grouping key share one placeholder token. Defaults to exact
91
+ * `detection.value` so `restore(redact(x)) === x` stays byte-exact.
92
+ */
93
+ canonicalKey?: (detection: Detection) => string;
94
+ /**
95
+ * Optional display value stored in `map[token]` when a new token is created.
96
+ * Defaults to `detection.value`.
97
+ */
98
+ canonicalValue?: (detection: Detection) => string;
88
99
  }
89
100
  interface RedactResult {
90
101
  /** The input with every detected value swapped for a placeholder. */
@@ -195,6 +206,20 @@ declare const kontonummer: Detector;
195
206
  * checksum-shaped values from being misclassified as payment identifiers.
196
207
  */
197
208
  declare const journalnummer: Detector;
209
+ /**
210
+ * Context-labeled birth date: "föddes 1974-03-12", "födelsedatum: 12 mars 1974",
211
+ * "född 810206". Bare dates without a birth cue stay untouched so contract,
212
+ * invoice and legal dates are never over-masked, and bare years ("född 1974")
213
+ * stay untouched to avoid over-masking biographical prose.
214
+ */
215
+ declare const fodelsedatum: Detector;
216
+ /**
217
+ * Context-labeled Swedish cadastral property designation (fastighetsbeteckning):
218
+ * "Fastighetsbeteckning: Provbyn Tallen 4:17", "fastigheten Provstad Söder 12:3".
219
+ * Requires the explicit block:unit suffix (`4:17`) so ordinary references to
220
+ * "fastigheten" without a cadastral id stay untouched.
221
+ */
222
+ declare const fastighetsbeteckning: Detector;
198
223
  declare const creditCard: Detector;
199
224
  declare const ipAddress: Detector;
200
225
  declare const url: Detector;
@@ -257,7 +282,7 @@ declare function runDetectors(input: string, detectors?: Detector[]): Detection[
257
282
  * external (e.g. an NER model in `maskera`) and you want the same stable
258
283
  * placeholder + overlap-resolution behaviour as {@link redact}.
259
284
  */
260
- declare function redactFromDetections(input: string, detections: Detection[], options?: Pick<RedactOptions, "placeholder">): RedactResult;
285
+ declare function redactFromDetections(input: string, detections: Detection[], options?: Pick<RedactOptions, "placeholder" | "canonicalKey" | "canonicalValue">): RedactResult;
261
286
  /**
262
287
  * Re-insert original values into a string that contains placeholder tokens.
263
288
  * Safe to call on LLM output that echoes the placeholders back.
@@ -297,5 +322,11 @@ declare function isPersonnummer(value: string): boolean;
297
322
  declare function isSamordningsnummer(value: string): boolean;
298
323
  /** Swedish organisationsnummer: 10 digits, third digit >= 2, Luhn valid. */
299
324
  declare function isOrganisationsnummer(value: string): boolean;
325
+ /**
326
+ * Validate a calendar birth date in ISO (`YYYY-MM-DD`), compact (`YYYYMMDD` /
327
+ * `YYMMDD`), day-first numeric (`DD/MM YYYY`, `DD.MM.YYYY`) or Swedish written
328
+ * (`12 mars 1974`) form.
329
+ */
330
+ declare function isBirthDate(value: string): boolean;
300
331
 
301
- export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
332
+ export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, fastighetsbeteckning, fodelsedatum, heuristicDetectors, iban, ipAddress, isBirthDate, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
package/dist/index.d.ts CHANGED
@@ -52,7 +52,7 @@ declare function canonicalize(input: string): CanonicalText;
52
52
  * Built-in PII labels. The `string` fallback lets you add custom detectors
53
53
  * with arbitrary labels without fighting the type system.
54
54
  */
55
- type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "KONTONUMMER" | "JOURNALNUMMER" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
55
+ type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "KONTONUMMER" | "JOURNALNUMMER" | "FODELSEDATUM" | "FASTIGHETSBETECKNING" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
56
56
  /** A raw match found by a detector, before placeholder assignment. */
57
57
  interface Detection {
58
58
  /** Inclusive start index into the source string. */
@@ -85,6 +85,17 @@ interface RedactOptions {
85
85
  * Defaults to ``(label, i) => `[${label}_${i}]` ``.
86
86
  */
87
87
  placeholder?: (label: PiiLabel, index: number) => string;
88
+ /**
89
+ * Optional grouping key for placeholder reuse. Detections with the same
90
+ * label and grouping key share one placeholder token. Defaults to exact
91
+ * `detection.value` so `restore(redact(x)) === x` stays byte-exact.
92
+ */
93
+ canonicalKey?: (detection: Detection) => string;
94
+ /**
95
+ * Optional display value stored in `map[token]` when a new token is created.
96
+ * Defaults to `detection.value`.
97
+ */
98
+ canonicalValue?: (detection: Detection) => string;
88
99
  }
89
100
  interface RedactResult {
90
101
  /** The input with every detected value swapped for a placeholder. */
@@ -195,6 +206,20 @@ declare const kontonummer: Detector;
195
206
  * checksum-shaped values from being misclassified as payment identifiers.
196
207
  */
197
208
  declare const journalnummer: Detector;
209
+ /**
210
+ * Context-labeled birth date: "föddes 1974-03-12", "födelsedatum: 12 mars 1974",
211
+ * "född 810206". Bare dates without a birth cue stay untouched so contract,
212
+ * invoice and legal dates are never over-masked, and bare years ("född 1974")
213
+ * stay untouched to avoid over-masking biographical prose.
214
+ */
215
+ declare const fodelsedatum: Detector;
216
+ /**
217
+ * Context-labeled Swedish cadastral property designation (fastighetsbeteckning):
218
+ * "Fastighetsbeteckning: Provbyn Tallen 4:17", "fastigheten Provstad Söder 12:3".
219
+ * Requires the explicit block:unit suffix (`4:17`) so ordinary references to
220
+ * "fastigheten" without a cadastral id stay untouched.
221
+ */
222
+ declare const fastighetsbeteckning: Detector;
198
223
  declare const creditCard: Detector;
199
224
  declare const ipAddress: Detector;
200
225
  declare const url: Detector;
@@ -257,7 +282,7 @@ declare function runDetectors(input: string, detectors?: Detector[]): Detection[
257
282
  * external (e.g. an NER model in `maskera`) and you want the same stable
258
283
  * placeholder + overlap-resolution behaviour as {@link redact}.
259
284
  */
260
- declare function redactFromDetections(input: string, detections: Detection[], options?: Pick<RedactOptions, "placeholder">): RedactResult;
285
+ declare function redactFromDetections(input: string, detections: Detection[], options?: Pick<RedactOptions, "placeholder" | "canonicalKey" | "canonicalValue">): RedactResult;
261
286
  /**
262
287
  * Re-insert original values into a string that contains placeholder tokens.
263
288
  * Safe to call on LLM output that echoes the placeholders back.
@@ -297,5 +322,11 @@ declare function isPersonnummer(value: string): boolean;
297
322
  declare function isSamordningsnummer(value: string): boolean;
298
323
  /** Swedish organisationsnummer: 10 digits, third digit >= 2, Luhn valid. */
299
324
  declare function isOrganisationsnummer(value: string): boolean;
325
+ /**
326
+ * Validate a calendar birth date in ISO (`YYYY-MM-DD`), compact (`YYYYMMDD` /
327
+ * `YYMMDD`), day-first numeric (`DD/MM YYYY`, `DD.MM.YYYY`) or Swedish written
328
+ * (`12 mars 1974`) form.
329
+ */
330
+ declare function isBirthDate(value: string): boolean;
300
331
 
301
- export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
332
+ export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, fastighetsbeteckning, fodelsedatum, heuristicDetectors, iban, ipAddress, isBirthDate, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
package/dist/index.js CHANGED
@@ -152,6 +152,63 @@ function isOrganisationsnummer(value) {
152
152
  if (Number(core[2]) < 2) return false;
153
153
  return luhnValid(core);
154
154
  }
155
+ var SWEDISH_MONTHS = {
156
+ januari: 1,
157
+ jan: 1,
158
+ februari: 2,
159
+ feb: 2,
160
+ mars: 3,
161
+ mar: 3,
162
+ april: 4,
163
+ apr: 4,
164
+ maj: 5,
165
+ juni: 6,
166
+ jun: 6,
167
+ juli: 7,
168
+ jul: 7,
169
+ augusti: 8,
170
+ aug: 8,
171
+ september: 9,
172
+ sep: 9,
173
+ sept: 9,
174
+ oktober: 10,
175
+ okt: 10,
176
+ november: 11,
177
+ nov: 11,
178
+ december: 12,
179
+ dec: 12
180
+ };
181
+ function isValidCalendarDate(year, month, day, requireCentury) {
182
+ if (requireCentury && (year < 1900 || year > 2099)) return false;
183
+ if (month < 1 || month > 12) return false;
184
+ return day >= 1 && day <= daysInMonth(year, month);
185
+ }
186
+ function isBirthDate(value) {
187
+ const trimmed = value.trim();
188
+ const iso = /^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})$/.exec(trimmed);
189
+ if (iso) {
190
+ return isValidCalendarDate(Number(iso[1]), Number(iso[2]), Number(iso[3]), true);
191
+ }
192
+ const compact8 = /^(\d{4})(\d{2})(\d{2})$/.exec(trimmed);
193
+ if (compact8) {
194
+ return isValidCalendarDate(Number(compact8[1]), Number(compact8[2]), Number(compact8[3]), true);
195
+ }
196
+ const compact6 = /^(\d{2})(\d{2})(\d{2})$/.exec(trimmed);
197
+ if (compact6) {
198
+ return isValidCalendarDate(Number(compact6[1]), Number(compact6[2]), Number(compact6[3]), false);
199
+ }
200
+ const dmy = /^(\d{1,2})[/. -](\d{1,2})(?:[/. -]|,\s*)(\d{4})$/.exec(trimmed);
201
+ if (dmy) {
202
+ return isValidCalendarDate(Number(dmy[3]), Number(dmy[2]), Number(dmy[1]), true);
203
+ }
204
+ const written = /^(\d{1,2})\s+([a-zåäö]+)\.?\s+(\d{4})$/i.exec(trimmed);
205
+ if (written) {
206
+ const month = SWEDISH_MONTHS[written[2].toLowerCase()];
207
+ if (!month) return false;
208
+ return isValidCalendarDate(Number(written[3]), month, Number(written[1]), true);
209
+ }
210
+ return false;
211
+ }
155
212
 
156
213
  // src/detectors/index.ts
157
214
  function regexDetector(label, regex, validate) {
@@ -175,7 +232,13 @@ function regexDetector(label, regex, validate) {
175
232
  scanner.lastIndex = 0;
176
233
  for (const m of input.matchAll(scanner)) {
177
234
  if (m.index === void 0) continue;
178
- const group = m[1] !== void 0 ? 1 : 0;
235
+ let group = 0;
236
+ for (let i = 1; i < m.length; i++) {
237
+ if (m[i] !== void 0) {
238
+ group = i;
239
+ break;
240
+ }
241
+ }
179
242
  const value = m[group];
180
243
  if (value.length === 0) continue;
181
244
  const span = hasIndices ? m.indices?.[group] : void 0;
@@ -316,8 +379,9 @@ var plusgiro = regexDetector(
316
379
  var iban = regexDetector("IBAN", /\bSE\d{2}(?:\s?\d){20}\b/gi);
317
380
  var kontonummer = regexDetector(
318
381
  "KONTONUMMER",
319
- /(?:^|[^\p{L}\p{N}])(?:bankkonto|kontonummer|utbetalningskonto|konto)(?:\s+nr\.?)?\s*[:#-]?\s*(\d{4}(?:\s?-\s?\d)?,\s+(?:\d[ -]?){5,14}\d|(?:\d[ -]?){6,19}\d)\b/giu,
382
+ /(?:^|[^\p{L}\p{N}])(?:bankkontonummer|bankkonto|kontonummer|kontonr|utbetalningskonto|konto)(?:\s+nr\.?)?(?:\s+(?:är|hos|på|i)){0,2}(?:(?:\s*[:#-]\s*|\s+)\(?(?:SEB|Swedbank|Handelsbanken|Nordea|Länsförsäkringar(?:\s+Bank)?|Skandiabanken|Skandia|Danske\s+Bank|SBAB|ICA\s+Banken|Avanza|Nordnet|Sparbanken)\)?)?(?:\s*[:#-]\s*|\s+)(\d{4}(?:\s?-\s?\d)?,\s+(?:\d[ -]?){5,14}\d|(?:\d[ -]?){6,19}\d)\b/giu,
320
383
  (value) => {
384
+ if (/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
321
385
  const digits = value.replace(/\D/g, "");
322
386
  return digits.length >= 7 && digits.length <= 20;
323
387
  }
@@ -326,6 +390,23 @@ var journalnummer = regexDetector(
326
390
  "JOURNALNUMMER",
327
391
  /(?:^|[^\p{L}\p{N}])(?:journalnummer|journalnr|journal-id|journalid)\s*[:#-]?\s*((?:[\p{L}\p{N}]{2,12}[-/]){1,3}[\p{L}\p{N}]{2,12}|\d{5,20})\b/giu
328
392
  );
393
+ var BIRTH_DATE_FULL = "(?:\\d{4}[-/.]\\d{1,2}[-/.]\\d{1,2}|\\d{1,2}[/.]\\d{1,2}(?:[/. -])\\d{4}|\\d{1,2}\\s+(?:januari|februari|mars|april|maj|juni|juli|augusti|september|oktober|november|december|jan\\.?|feb\\.?|mar\\.?|apr\\.?|jun\\.?|jul\\.?|aug\\.?|sept?\\.?|okt\\.?|nov\\.?|dec\\.?)\\s+\\d{4}|\\d{8}(?![-+\\s]?\\d{4}))";
394
+ var fodelsedatum = regexDetector(
395
+ "FODELSEDATUM",
396
+ new RegExp(
397
+ `(?:^|[^\\p{L}\\p{N}])(?:(?:f\xF6delsedatum|f\xF6ddes|f\xF6dd)(?:\\s+(?:\xE4r|den))?\\s*[:#-]?\\s*(${BIRTH_DATE_FULL}|\\d{6}(?![-+\\s]?\\d{4}))|(?<=^|[\\s(,])f\\.\\s*(${BIRTH_DATE_FULL}))(?=$|[^\\p{L}\\p{N}])`,
398
+ "giu"
399
+ ),
400
+ isBirthDate
401
+ );
402
+ var fastighetsbeteckning = regexDetector(
403
+ "FASTIGHETSBETECKNING",
404
+ /(?:^|[^\p{L}\p{N}])(?:fastighetsbeteckning|fastigheten|fastighet)(?:\s+är)?\s*[:#-]?\s*([A-ZÅÄÖ][A-Za-zÅÄÖåäö-]+(?:\s+[A-ZÅÄÖ][A-Za-zÅÄÖåäö-]+){0,3}\s+\d{1,4}:\d{1,5})(?=$|[^\p{L}\p{N}:])/giu,
405
+ (value) => {
406
+ const words = value.replace(/\s+\d{1,4}:\d{1,5}$/, "").split(/\s+/);
407
+ return words.length >= 1 && words.every((w) => /^[A-ZÅÄÖ]/.test(w));
408
+ }
409
+ );
329
410
  var creditCard = regexDetector("KORTNUMMER", /\b\d(?:[ -]?\d){12,18}\b/g, (v) => {
330
411
  const digits = v.replace(/\D/g, "");
331
412
  return digits.length >= 13 && digits.length <= 19 && luhnValid(digits);
@@ -350,7 +431,12 @@ var regnummer = regexDetector(
350
431
  (v) => !CURRENCY_CODES.has(v.slice(0, 3))
351
432
  );
352
433
  var heuristicDetectors = [adress, lagenhetsnummer, regnummer];
353
- var contextualDetectors = [kontonummer, journalnummer];
434
+ var contextualDetectors = [
435
+ kontonummer,
436
+ journalnummer,
437
+ fodelsedatum,
438
+ fastighetsbeteckning
439
+ ];
354
440
  var defaultDetectors = [
355
441
  email,
356
442
  url,
@@ -373,6 +459,8 @@ function defaultPlaceholder(label, index) {
373
459
  var SAME_SPAN_PRIORITY = {
374
460
  JOURNALNUMMER: -10,
375
461
  KONTONUMMER: -10,
462
+ FODELSEDATUM: -10,
463
+ FASTIGHETSBETECKNING: -10,
376
464
  TELEFON: 0,
377
465
  PERSONNUMMER: 10,
378
466
  SAMORDNINGSNUMMER: 11
@@ -466,6 +554,8 @@ function runDetectors(input, detectors = defaultDetectors) {
466
554
  function redactFromDetections(input, detections, options = {}) {
467
555
  const placeholder = options.placeholder ?? defaultPlaceholder;
468
556
  const ownPlaceholder = options.placeholder === void 0;
557
+ const canonicalKey = options.canonicalKey;
558
+ const canonicalValue = options.canonicalValue;
469
559
  for (const d of detections) {
470
560
  if (!Number.isInteger(d.start) || !Number.isInteger(d.end) || d.start < 0 || d.end > input.length || d.start >= d.end) {
471
561
  throw new Error(
@@ -491,7 +581,7 @@ function redactFromDetections(input, detections, options = {}) {
491
581
  const redactions = [];
492
582
  const occursInInput = createInputProbe(input, placeholder);
493
583
  for (const d of resolved) {
494
- const key = tokenKey(d.label, d.value);
584
+ const key = tokenKey(d.label, canonicalKey ? canonicalKey(d) : d.value);
495
585
  let token = tokenByKey.get(key);
496
586
  if (!token) {
497
587
  let next = counters.get(d.label) ?? 0;
@@ -511,7 +601,7 @@ function redactFromDetections(input, detections, options = {}) {
511
601
  }
512
602
  counters.set(d.label, next);
513
603
  tokenByKey.set(key, token);
514
- map[token] = d.value;
604
+ map[token] = canonicalValue ? canonicalValue(d) : d.value;
515
605
  }
516
606
  redactions.push({ ...d, replacement: token });
517
607
  }
@@ -574,6 +664,6 @@ function restore(text, map) {
574
664
  return out + text.slice(last);
575
665
  }
576
666
 
577
- export { adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
667
+ export { adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, fastighetsbeteckning, fodelsedatum, heuristicDetectors, iban, ipAddress, isBirthDate, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
578
668
  //# sourceMappingURL=index.js.map
579
669
  //# sourceMappingURL=index.js.map