-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprivacy.ts
More file actions
36 lines (32 loc) · 1.12 KB
/
privacy.ts
File metadata and controls
36 lines (32 loc) · 1.12 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
// src/lib/privacy.ts
//
// Functions: maskUsername, isRedacted, redactedLength, REDACTED_PREFIX
/**
* Prefix used to identify redacted values in the database.
* Format: "▓<length>" i.e "▓7" for a 7-character original string.
*/
export const REDACTED_PREFIX = "▓"
/**
* Replaces a plaintext string with a length-preserving redacted marker.
* Returns null if input is null/undefined.
*/
export function maskUsername(value: string | null | undefined): string | null {
if (!value) return null
return `${REDACTED_PREFIX}${value.length}`
}
/**
* Checks whether a stored value is a redacted marker.
*/
export function isRedacted(value: string | null | undefined): boolean {
if (!value) return false
return value.startsWith(REDACTED_PREFIX)
}
/**
* Extracts the original character count from a redacted marker.
* Returns null if the value is not redacted or has no valid length.
*/
export function redactedLength(value: string | null | undefined): number | null {
if (!value || !isRedacted(value)) return null
const num = parseInt(value.slice(REDACTED_PREFIX.length), 10)
return Number.isFinite(num) ? num : null
}