https://github.com/foxxmd/redact-string
Redact part or all of a string
https://github.com/foxxmd/redact-string
javascript redact redaction replace-text string string-manipulation typescript
Last synced: about 1 year ago
JSON representation
Redact part or all of a string
- Host: GitHub
- URL: https://github.com/foxxmd/redact-string
- Owner: FoxxMD
- License: mit
- Created: 2023-07-11T14:59:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-11T15:42:08.000Z (over 2 years ago)
- Last Synced: 2025-02-14T06:36:00.751Z (about 1 year ago)
- Topics: javascript, redact, redaction, replace-text, string, string-manipulation, typescript
- Language: TypeScript
- Homepage: https://foxxmd.github.io/redact-string
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redact-string
Redact (replace) part or all of a string with a character.
## Why another redact package?
The other popular packages seem to be restricted to a certain behavior:
* Redaction of objects/json (instead of plain variables/values you control)
* Redaction **entirely** replaces value leaving no structure or hint of what it was
This didn't fit my needs. I wanted to be able to control how many characters were replaced as well as leave part of the structure intact for hinting in logging. A prime example being replacing _part_ of an IP address so users can view/post logs which confirm correct settings but don't give away full addresses IE `192.168.1.105` -> `**********105`
# Install/Usage
```
npm install @foxxmd/react-string
```
```js
import {reactString} from '@foxxmd/redact-string';
console.log(reactString('192.168.1.105', 3)); // 3 is the number of character to leave visible
// *********105
```
# Options
An optional, third argument can be passed that defines how the redact occurs:
```ts
export interface RedactOptions {
/**
* Replace characters starting at the start or end of string (default start)
* */
replaceFrom?: 'start' | 'end'
/**
* The character/string that characters are replaced with (default '*')
* */
replaceWith?: string
/**
* Which type of characters to replace in the string (default any)
* */
replace?: 'any' | 'alphanumeric' | 'alpha' | 'numeric'
}
```
# Examples
```js
// replace all but last 3 characters
console.log(reactString('192.168.1.105', 3));
// *********105
// replace all but first 3 characters
console.log(reactString('192.168.1.105', 3, {replaceFrom: 'end'}));
// 192**********
// replace all but last 3 characters, numeric only
console.log(reactString('192.168.1.105', 3, {replace: 'numeric'}));
// ***.***.*.105
// replace all but last 5 characters with 'X'
console.log(reactString('superSecretPassword', 5, {replaceWith: 'X'}));
// XXXXXXXXXXXXXXsword
```