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);
Keep the first N characters visible
const stringMask = require("string-masking");
const output = stringMask("1234567890", -3);
console.log(output);
Mask the middle portion
const stringMask = require("string-masking");
const output = stringMask("1234567890", 0);
console.log(output);
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);
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);
Mask alternate characters
const stringMask = require("string-masking");
const output = stringMask("ABCDEFGH", {
alternateMask: true
});
console.log(output);
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);
Options
visibleStart | integer | 0 | Number of maskable characters to keep visible at the start |
visibleEnd | integer | 0 | Number of maskable characters to keep visible at the end |
preserveFormat | boolean | false | Preserves non-alphanumeric separators while masking |
maskChar | string | "X" | Single character used for masking |
alternateMask | boolean | false | Masks 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