Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change string method name to text #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const assertMaskIsString = (str) => {
}
};

const string = (str, mask = '*') => {
const text = (str, mask = '*') => {
assertSubjectIsString(str);
assertMaskIsString(mask);

Expand Down Expand Up @@ -40,18 +40,18 @@ const domain = (str, mask = '*') => {
const tldIndex = str.lastIndexOf('.');
if (tldIndex === -1) {
// not a domain name
return string(str, mask);
return text(str, mask);
}

const start = str.slice(0, tldIndex);
const end = str.slice(tldIndex + 1);

if (start.length === 0 || end.length === 0) {
// not a domain name
return string(str, mask);
return text(str, mask);
}

return `${string(start, mask)}.${end}`;
return `${text(start, mask)}.${end}`;
};

const email = (str, mask = '*') => {
Expand All @@ -61,17 +61,17 @@ const email = (str, mask = '*') => {

if (parts.length !== 2) {
// not an email address
return string(str, mask);
return text(str, mask);
}

const [start, end] = parts;

return `${string(start, mask)}@${domain(end, mask)}`;
return `${text(start, mask)}@${domain(end, mask)}`;
};


module.exports = {
string,
text,
domain,
email
};
20 changes: 10 additions & 10 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { string, domain, email } = require('..');
const { text, domain, email } = require('..');

describe('Mask PII', () => {
test('string', () => {
expect(string('')).toEqual('********');
expect(string('a')).toEqual('a********');
expect(string('Hello world')).toEqual('H********d');
expect(string('Hello world', '★')).toEqual('H★★★★★★★★d');
expect(string('Hello world', '#')).toEqual('H########d');
expect(string('Hello world', '##')).toEqual('H################d');
test('text', () => {
expect(text('')).toEqual('********');
expect(text('a')).toEqual('a********');
expect(text('Hello world')).toEqual('H********d');
expect(text('Hello world', '★')).toEqual('H★★★★★★★★d');
expect(text('Hello world', '#')).toEqual('H########d');
expect(text('Hello world', '##')).toEqual('H################d');

// test bad input

Expand All @@ -24,7 +24,7 @@ describe('Mask PII', () => {

for (const input of notStrings) {
expect(() => {
string(input); // bad subject
text(input); // bad subject
}).toThrow(new TypeError('Subject is not a string'));

expect(() => {
Expand All @@ -41,7 +41,7 @@ describe('Mask PII', () => {
}

expect(() => {
string('abc', input); // bad mask
text('abc', input); // bad mask
}).toThrow(new TypeError('Mask is not a string'));
}
});
Expand Down