New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@foxxmd/redact-string

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@foxxmd/redact-string

Redact part or all of string by replacing character

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

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
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:

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


// 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

FAQs

Package last updated on 11 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts