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

string-masking

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-masking

Mask strings while optionally preserving formatting characters

latest
npmnpm
Version
1.2.1
Version published
Weekly downloads
85
70%
Maintainers
1
Weekly downloads
 
Created
Source

string-masking

This package masks strings in two ways:

  • Legacy digit mode for the original package behavior.
  • Options-based mode for cleaner masking with support for formatted strings.

What This Repository Does

The repository exports a single function from index.js:

const stringMask = require("string-masking");

It returns a model in this shape:

{
  status: "success" | "failure",
  response: "masked output or error message"
}

Failures also include a NOTE field.

Legacy API

Keep the last N characters visible

const stringMask = require("string-masking");

const output = stringMask("1234567890", 3);
console.log(output);

// { status: 'success', response: 'XXXXXXX890' }

Keep the first N characters visible

const stringMask = require("string-masking");

const output = stringMask("1234567890", -3);
console.log(output);

// { status: 'success', response: '123XXXXXXX' }

Mask the middle portion

const stringMask = require("string-masking");

const output = stringMask("1234567890", 0);
console.log(output);

// { status: 'success', response: '12XXXXXX90' }

Improved API

Use an options object when you want more control.

Keep a custom number of characters at the start and end

const stringMask = require("string-masking");

const output = stringMask("ABCD1234EFGH", {
  visibleStart: 2,
  visibleEnd: 2
});

console.log(output);

// { status: 'success', response: 'ABXXXXXXXXGH' }

Preserve separators such as spaces, dashes, and symbols

const stringMask = require("string-masking");

const output = stringMask("4111-1111-1111-1111", {
  visibleEnd: 4,
  preserveFormat: true
});

console.log(output);

// { status: 'success', response: 'XXXX-XXXX-XXXX-1111' }

Mask alternate characters

const stringMask = require("string-masking");

const output = stringMask("ABCDEFGH", {
  alternateMask: true
});

console.log(output);

// { status: 'success', response: 'XBXDXFXH' }

Use the legacy digit argument with formatting support

const stringMask = require("string-masking");

const output = stringMask("ABHA-9988-XY12", 4, {
  preserveFormat: true,
  maskChar: "*"
});

console.log(output);

// { status: 'success', response: '****-****-XY12' }

Options

OptionTypeDefaultDescription
visibleStartinteger0Number of maskable characters to keep visible at the start
visibleEndinteger0Number of maskable characters to keep visible at the end
preserveFormatbooleanfalsePreserves non-alphanumeric separators while masking
maskCharstring"X"Single character used for masking
alternateMaskbooleanfalseMasks every other eligible character inside the maskable region

When preserveFormat is true, letters and numbers are masked, while characters such as spaces, -, ., @, and + stay in place.

When alternateMask is true, alternating starts from the first character that would otherwise be masked after visibleStart and visibleEnd are applied.

Suggestions For Further Improvement

  • Add TypeScript definitions so the options API is easier to consume in editors.
  • Publish a major version when you want to formalize the new options-based API.
  • Add a maskPattern option later if you want different masking rules for emails, phones, or IDs.
  • Add CI to run npm test automatically before publishing.

Contribution

Rohan Solse
Suggestions and recommendations are welcome.
Contact: rohansolse@gmail.com

Keywords

string-masking

FAQs

Package last updated on 23 Mar 2026

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