UNPKG

string-masking

Version:

Mask strings while optionally preserving formatting characters

137 lines (122 loc) 3.22 kB
const test = require("node:test"); const assert = require("node:assert/strict"); const stringMask = require("../index"); test("masks all but the last N characters in legacy mode", () => { assert.deepEqual(stringMask("1234567890", 3), { status: "success", response: "XXXXXXX890", }); }); test("masks all but the first N characters in legacy mode", () => { assert.deepEqual(stringMask("1234567890", -3), { status: "success", response: "123XXXXXXX", }); }); test("masks the middle section in legacy mode", () => { assert.deepEqual(stringMask("1234567890", 0), { status: "success", response: "12XXXXXX90", }); }); test("accepts numeric input", () => { assert.deepEqual(stringMask(1234567890, 4), { status: "success", response: "XXXXXX7890", }); }); test("supports preserving separators with the legacy digit API", () => { assert.deepEqual( stringMask("4111-1111-1111-1111", 4, { preserveFormat: true }), { status: "success", response: "XXXX-XXXX-XXXX-1111", } ); }); test("supports options-based masking for custom visible ranges", () => { assert.deepEqual( stringMask("ABCD1234EFGH", { visibleStart: 2, visibleEnd: 2 }), { status: "success", response: "ABXXXXXXXXGH", } ); }); test("preserves formatting characters in options mode", () => { assert.deepEqual( stringMask("+91 98765 43210", { visibleStart: 2, visibleEnd: 2, preserveFormat: true, }), { status: "success", response: "+91 XXXXX XXX10", } ); }); test("supports a custom mask character", () => { assert.deepEqual( stringMask("john.doe@example.com", { visibleStart: 2, visibleEnd: 3, preserveFormat: true, maskChar: "*", }), { status: "success", response: "jo**.***@*******.com", } ); }); test("supports alternate masking in options mode", () => { assert.deepEqual( stringMask("ABCDEFGH", { alternateMask: true, }), { status: "success", response: "XBXDXFXH", } ); }); test("supports alternate masking with preserveFormat enabled", () => { assert.deepEqual( stringMask("+91 98765 43210", { visibleStart: 2, visibleEnd: 2, preserveFormat: true, alternateMask: true, }), { status: "success", response: "+91 X8X6X 4X210", } ); }); test("supports alternate masking through the legacy API options", () => { assert.deepEqual( stringMask("1234567890", 4, { alternateMask: true, }), { status: "success", response: "X2X4X67890", } ); }); test("returns a failure model for blank strings", () => { assert.deepEqual(stringMask("", 2), { status: "failure", response: "Blank string cannot be processed", NOTE: "Please email us on rohansolse@gmail.com for NPM related suggestions/bugs (with input).", }); }); test("validates options objects", () => { assert.deepEqual(stringMask("123456", { visibleEnd: -1 }), { status: "failure", response: "visibleEnd must be a non-negative integer", NOTE: "Please email us on rohansolse@gmail.com for NPM related suggestions/bugs (with input).", }); });