Skip to content

Commit

Permalink
refactor(finance): rename mask to maskNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 15, 2023
1 parent 5e421b0 commit 535e07a
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 75 deletions.
87 changes: 49 additions & 38 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import iban from './iban';

/**
Expand Down Expand Up @@ -197,8 +198,31 @@ export class FinanceModule {
* faker.finance.mask(3, false, false) // '298'
*
* @since 2.0.1
*
* @deprecated Use `faker.finance.maskNumber` instead.
*/
mask(length?: number, parens?: boolean, ellipsis?: boolean): string;
mask(length?: number, parens?: boolean, ellipsis?: boolean): string {
deprecated({
deprecated: 'faker.finance.mask',
proposed: 'faker.finance.maskNumber',
since: '8.0',
until: '9.0',
});
return this.maskNumber({ length, parens, ellipsis });
}

/**
* Generates a random masked number.
*
* @param length The length of the unmasked number. Defaults to `4`.
*
* @example
* faker.finance.maskNumber() // '(...9711)'
* faker.finance.maskNumber(3) // '(...342)'
*
* @since 8.0.0
*/
maskNumber(length?: number): string;
/**
* Generates a random masked number.
*
Expand All @@ -208,14 +232,14 @@ export class FinanceModule {
* @param options.ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
*
* @example
* faker.finance.mask() // '(...9711)'
* faker.finance.mask({ length: 3 }) // '(...342)'
* faker.finance.mask({ length: 3, parens: false }) // '...236'
* faker.finance.mask({ length: 3, parens: false, ellipsis: false }) // '298'
* faker.finance.maskNumber() // '(...9711)'
* faker.finance.maskNumber({ length: 3 }) // '(...342)'
* faker.finance.maskNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 2.0.1
* @since 8.0.0
*/
mask(options?: {
maskNumber(options?: {
length?: number;
parens?: boolean;
ellipsis?: boolean;
Expand All @@ -227,21 +251,17 @@ export class FinanceModule {
* @param optionsOrLength.length The length of the unmasked number. Defaults to `4`.
* @param optionsOrLength.parens Whether to use surrounding parenthesis. Defaults to `true`.
* @param optionsOrLength.ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
* @param legacyParens Whether to use surrounding parenthesis. Defaults to `true`.
* @param legacyEllipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
*
* @example
* faker.finance.mask() // '(...9711)'
* faker.finance.mask({ length: 3 }) // '(...342)'
* faker.finance.mask({ length: 3, parens: false }) // '...236'
* faker.finance.mask({ length: 3, parens: false, ellipsis: false }) // '298'
* faker.finance.mask(3) // '(...342)'
* faker.finance.mask(3, false) // '...236'
* faker.finance.mask(3, false, false) // '298'
* faker.finance.maskNumber() // '(...9711)'
* faker.finance.maskNumber(3) // '(...342)'
* faker.finance.maskNumber({ length: 3 }) // '(...342)'
* faker.finance.maskNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 2.0.1
* @since 8.0.0
*/
mask(
maskNumber(
optionsOrLength?:
| number
| {
Expand All @@ -263,9 +283,7 @@ export class FinanceModule {
* @default true
*/
ellipsis?: boolean;
},
legacyParens?: boolean,
legacyEllipsis?: boolean
}
): string;
/**
* Generates a random masked number.
Expand All @@ -274,18 +292,17 @@ export class FinanceModule {
* @param options.length The length of the unmasked number. Defaults to `4`.
* @param options.parens Whether to use surrounding parenthesis. Defaults to `true`.
* @param options.ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
* @param legacyParens Whether to use surrounding parenthesis. Defaults to `true`.
* @param legacyEllipsis Whether to use surrounding parenthesis. Defaults to `true`.
*
* @example
* faker.finance.mask() // '(...9711)'
* faker.finance.mask(3) // '(...342)'
* faker.finance.mask(3, false) // '...236'
* faker.finance.mask(3, false, false) // '298'
* faker.finance.maskNumber() // '(...9711)'
* faker.finance.maskNumber(3) // '(...342)'
* faker.finance.maskNumber({ length: 3 }) // '(...342)'
* faker.finance.maskNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 2.0.1
* @since 8.0.0
*/
mask(
maskNumber(
options:
| number
| {
Expand All @@ -307,20 +324,14 @@ export class FinanceModule {
* @default true
*/
ellipsis?: boolean;
} = {},
legacyParens: boolean = true,
legacyEllipsis: boolean = true
} = {}
): string {
if (typeof options === 'number') {
options = { length: options };
}

// set defaults
const {
ellipsis = legacyEllipsis,
length = 4,
parens = legacyParens,
} = options;
const { ellipsis, length = 4, parens } = options;

// create a template for length
let template = '';
Expand Down Expand Up @@ -1186,7 +1197,7 @@ export class FinanceModule {
const company = this.faker.company.name();
const transactionType = this.transactionType();
const account = this.account();
const card = this.mask();
const card = this.maskNumber();
const currency = this.currencyCode();

return `${transactionType} transaction at ${company} using card ending with ***${card} for ${currency} ${amount} in account ***${account}`;
Expand Down
66 changes: 39 additions & 27 deletions test/__snapshots__/finance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,25 @@ exports[`finance > 42 > iban > with formatted option 1`] = `"GT03 9751 1086 7098

exports[`finance > 42 > litecoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1b"`;

exports[`finance > 42 > mask > noArgs 1`] = `"(...3791)"`;
exports[`finance > 42 > mask > noArgs 1`] = `"3791"`;

exports[`finance > 42 > mask > with legacy ellipsis 1`] = `"(...3791)"`;
exports[`finance > 42 > mask > with ellipsis 1`] = `"...3791"`;

exports[`finance > 42 > mask > with legacy parenthesis 1`] = `"(...3791)"`;
exports[`finance > 42 > mask > with length 1`] = `"37917"`;

exports[`finance > 42 > mask > with length 1`] = `"(...37917)"`;
exports[`finance > 42 > mask > with length, parenthesis and ellipsis 1`] = `"(...37917)"`;

exports[`finance > 42 > mask > with length and parenthesis option 1`] = `"...37917"`;
exports[`finance > 42 > mask > with parenthesis 1`] = `"(3791)"`;

exports[`finance > 42 > mask > with length option 1`] = `"(...37917)"`;
exports[`finance > 42 > maskNumber > noArgs 1`] = `"3791"`;

exports[`finance > 42 > mask > with length, legacy parenthesis, and legacy ellipsis 1`] = `"(...37917)"`;
exports[`finance > 42 > maskNumber > with length 1`] = `"37917"`;

exports[`finance > 42 > mask > with length, parenthesis and ellipsis option 1`] = `"...37917"`;
exports[`finance > 42 > maskNumber > with length and parenthesis option 1`] = `"37917"`;

exports[`finance > 42 > maskNumber > with length option 1`] = `"37917"`;

exports[`finance > 42 > maskNumber > with length, parenthesis and ellipsis option 1`] = `"...37917"`;

exports[`finance > 42 > pin > noArgs 1`] = `"3791"`;

Expand All @@ -96,7 +100,7 @@ exports[`finance > 42 > pin > with length option 1`] = `"3791775514"`;

exports[`finance > 42 > routingNumber 1`] = `"379177554"`;

exports[`finance > 42 > transactionDescription 1`] = `"invoice transaction at Wiegand, Deckow and Reynolds using card ending with ***(...8361) for RSD 374.54 in account ***55141004"`;
exports[`finance > 42 > transactionDescription 1`] = `"invoice transaction at Wiegand, Deckow and Reynolds using card ending with ***8361 for RSD 374.54 in account ***55141004"`;

exports[`finance > 42 > transactionType 1`] = `"withdrawal"`;

Expand Down Expand Up @@ -172,21 +176,25 @@ exports[`finance > 1211 > iban > with formatted option 1`] = `"TN42 8201 6024 17

exports[`finance > 1211 > litecoinAddress 1`] = `"MTMe8Z3EaFdLqmaGKP1LEEJQVriSZRZds"`;

exports[`finance > 1211 > mask > noArgs 1`] = `"(...9487)"`;
exports[`finance > 1211 > mask > noArgs 1`] = `"9487"`;

exports[`finance > 1211 > mask > with ellipsis 1`] = `"...9487"`;

exports[`finance > 1211 > mask > with legacy ellipsis 1`] = `"(...9487)"`;
exports[`finance > 1211 > mask > with length 1`] = `"94872"`;

exports[`finance > 1211 > mask > with legacy parenthesis 1`] = `"(...9487)"`;
exports[`finance > 1211 > mask > with length, parenthesis and ellipsis 1`] = `"(...94872)"`;

exports[`finance > 1211 > mask > with length 1`] = `"(...94872)"`;
exports[`finance > 1211 > mask > with parenthesis 1`] = `"(9487)"`;

exports[`finance > 1211 > mask > with length and parenthesis option 1`] = `"...94872"`;
exports[`finance > 1211 > maskNumber > noArgs 1`] = `"9487"`;

exports[`finance > 1211 > mask > with length option 1`] = `"(...94872)"`;
exports[`finance > 1211 > maskNumber > with length 1`] = `"94872"`;

exports[`finance > 1211 > mask > with length, legacy parenthesis, and legacy ellipsis 1`] = `"(...94872)"`;
exports[`finance > 1211 > maskNumber > with length and parenthesis option 1`] = `"94872"`;

exports[`finance > 1211 > mask > with length, parenthesis and ellipsis option 1`] = `"...94872"`;
exports[`finance > 1211 > maskNumber > with length option 1`] = `"94872"`;

exports[`finance > 1211 > maskNumber > with length, parenthesis and ellipsis option 1`] = `"...94872"`;

exports[`finance > 1211 > pin > noArgs 1`] = `"9487"`;

Expand All @@ -196,7 +204,7 @@ exports[`finance > 1211 > pin > with length option 1`] = `"9487219061"`;

exports[`finance > 1211 > routingNumber 1`] = `"948721904"`;

exports[`finance > 1211 > transactionDescription 1`] = `"deposit transaction at Trantow - Satterfield using card ending with ***(...4316) for SDG 928.52 in account ***19061627"`;
exports[`finance > 1211 > transactionDescription 1`] = `"deposit transaction at Trantow - Satterfield using card ending with ***4316 for SDG 928.52 in account ***19061627"`;

exports[`finance > 1211 > transactionType 1`] = `"invoice"`;

Expand Down Expand Up @@ -272,21 +280,25 @@ exports[`finance > 1337 > iban > with formatted option 1`] = `"FO56 1005 0250 09

exports[`finance > 1337 > litecoinAddress 1`] = `"Madhxs2jewAgkYgJi7No6Cn8JZar"`;

exports[`finance > 1337 > mask > noArgs 1`] = `"(...2512)"`;
exports[`finance > 1337 > mask > noArgs 1`] = `"2512"`;

exports[`finance > 1337 > mask > with ellipsis 1`] = `"...2512"`;

exports[`finance > 1337 > mask > with length 1`] = `"25122"`;

exports[`finance > 1337 > mask > with legacy ellipsis 1`] = `"(...2512)"`;
exports[`finance > 1337 > mask > with length, parenthesis and ellipsis 1`] = `"(...25122)"`;

exports[`finance > 1337 > mask > with legacy parenthesis 1`] = `"(...2512)"`;
exports[`finance > 1337 > mask > with parenthesis 1`] = `"(2512)"`;

exports[`finance > 1337 > mask > with length 1`] = `"(...25122)"`;
exports[`finance > 1337 > maskNumber > noArgs 1`] = `"2512"`;

exports[`finance > 1337 > mask > with length and parenthesis option 1`] = `"...25122"`;
exports[`finance > 1337 > maskNumber > with length 1`] = `"25122"`;

exports[`finance > 1337 > mask > with length option 1`] = `"(...25122)"`;
exports[`finance > 1337 > maskNumber > with length and parenthesis option 1`] = `"25122"`;

exports[`finance > 1337 > mask > with length, legacy parenthesis, and legacy ellipsis 1`] = `"(...25122)"`;
exports[`finance > 1337 > maskNumber > with length option 1`] = `"25122"`;

exports[`finance > 1337 > mask > with length, parenthesis and ellipsis option 1`] = `"...25122"`;
exports[`finance > 1337 > maskNumber > with length, parenthesis and ellipsis option 1`] = `"...25122"`;

exports[`finance > 1337 > pin > noArgs 1`] = `"2512"`;

Expand All @@ -296,6 +308,6 @@ exports[`finance > 1337 > pin > with length option 1`] = `"2512254032"`;

exports[`finance > 1337 > routingNumber 1`] = `"251225401"`;

exports[`finance > 1337 > transactionDescription 1`] = `"withdrawal transaction at Cronin - Effertz using card ending with ***(...3927) for GIP 262.02 in account ***54032552"`;
exports[`finance > 1337 > transactionDescription 1`] = `"withdrawal transaction at Cronin - Effertz using card ending with ***3927 for GIP 262.02 in account ***54032552"`;

exports[`finance > 1337 > transactionType 1`] = `"withdrawal"`;
52 changes: 42 additions & 10 deletions test/finance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ describe('finance', () => {
t.describe('mask', (t) => {
t.it('noArgs')
.it('with length', 5)
.it('with legacy parenthesis', undefined, true)
.it('with legacy ellipsis', undefined, undefined, true)
.it(
'with length, legacy parenthesis, and legacy ellipsis',
5,
true,
true
)
.it('with parenthesis', undefined, true)
.it('with ellipsis', undefined, undefined, true)
.it('with length, parenthesis and ellipsis', 5, true, true);
});

t.describe('maskNumber', (t) => {
t.it('noArgs')
.it('with length', 5)
.it('with length option', { length: 5 })
.it('with length and parenthesis option', { length: 5, parens: false })
.it('with length, parenthesis and ellipsis option', {
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('finance', () => {

describe('mask()', () => {
it('should set a default length', () => {
const expected = 4; //default account mask length
const expected = 4; // default account mask length
const mask = faker.finance.mask(undefined, false, false);

expect(
Expand All @@ -167,7 +167,39 @@ describe('finance', () => {

expected = expected || 4;

const mask = faker.finance.mask(expected, false, false); //the length of mask picks 4 if the random number generator picks 0
const mask = faker.finance.mask(expected, false, false); // the length of mask picks 4 if the random number generator picks 0

expect(
mask,
`The expected default mask length is ${expected} but it was ${mask.length}`
).toHaveLength(expected);
});
});

describe('maskNumber()', () => {
it('should set a default length', () => {
const expected = 4; // default account mask length
const mask = faker.finance.maskNumber({
parens: false,
ellipsis: false,
});

expect(
mask,
`The expected default mask length is ${expected} but it was ${mask.length}`
).toHaveLength(expected);
});

it('should set a specified length', () => {
let expected = faker.number.int(20);

expected = expected || 4;

const mask = faker.finance.maskNumber({
length: expected,
parens: false,
ellipsis: false,
}); // the length of mask picks 4 if the random number generator picks 0

expect(
mask,
Expand Down

0 comments on commit 535e07a

Please sign in to comment.