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(finance): rename mask to maskedNumber #2055

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ The `faker.address.*` methods will continue to work as an alias in v8 and v9, bu

The `faker.finance.account` method has been renamed to `faker.finance.accountNumber` to better reflect the data it returns and not to get confused with a user "Account".

### `faker.finance.mask` changed to `faker.finance.maskedNumber`

The `faker.finance.mask` method has been renamed to `faker.finance.maskedNumber` to better reflect its purpose.

### Number methods of `faker.datatype` moved to new `faker.number` module

The number-related methods previously found in `faker.datatype` have been moved to a new `faker.number` module.
Expand Down
88 changes: 50 additions & 38 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,40 @@ export class FinanceModule {
* @param parens Whether to use surrounding parenthesis. Defaults to `true`.
* @param ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
*
* @see faker.finance.maskedNumber()
*
* @example
* faker.finance.mask() // '(...9711)'
* faker.finance.mask(3) // '(...342)'
* faker.finance.mask(3, false) // '...236'
* faker.finance.mask(3, false, false) // '298'
*
* @since 2.0.1
*
* @deprecated Use `faker.finance.maskedNumber` instead.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
*/
mask(length?: number, parens?: boolean, ellipsis?: boolean): string;
mask(length?: number, parens?: boolean, ellipsis?: boolean): string {
deprecated({
deprecated: 'faker.finance.mask',
proposed: 'faker.finance.maskedNumber',
since: '8.0',
until: '9.0',
});
return this.maskedNumber({ length, parens, ellipsis });
}

/**
* Generates a random masked number.
*
* @param length The length of the unmasked number. Defaults to `4`.
*
* @example
* faker.finance.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber(3) // '(...342)'
*
* @since 8.0.0
*/
maskedNumber(length?: number): string;
/**
* Generates a random masked number.
*
Expand All @@ -235,14 +260,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.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber({ length: 3 }) // '(...342)'
* faker.finance.maskedNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskedNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 2.0.1
* @since 8.0.0
*/
mask(options?: {
maskedNumber(options?: {
length?: number;
parens?: boolean;
ellipsis?: boolean;
Expand All @@ -254,21 +279,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.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber(3) // '(...342)'
* faker.finance.maskedNumber({ length: 3 }) // '(...342)'
* faker.finance.maskedNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskedNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 2.0.1
* @since 8.0.0
*/
mask(
maskedNumber(
optionsOrLength?:
| number
| {
Expand All @@ -290,9 +311,7 @@ export class FinanceModule {
* @default true
*/
ellipsis?: boolean;
},
legacyParens?: boolean,
legacyEllipsis?: boolean
}
): string;
/**
* Generates a random masked number.
Expand All @@ -301,18 +320,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.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber(3) // '(...342)'
* faker.finance.maskedNumber({ length: 3 }) // '(...342)'
* faker.finance.maskedNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskedNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 2.0.1
* @since 8.0.0
*/
mask(
maskedNumber(
options:
| number
| {
Expand All @@ -334,20 +352,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 @@ -1219,7 +1231,7 @@ export class FinanceModule {
const company = this.faker.company.name();
const transactionType = this.transactionType();
const account = this.accountNumber();
const card = this.mask();
const card = this.maskedNumber();
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 @@ -76,21 +76,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 > maskedNumber > noArgs 1`] = `"3791"`;

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

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

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

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

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

Expand All @@ -100,7 +104,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 @@ -180,21 +184,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 > maskedNumber > noArgs 1`] = `"9487"`;

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

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

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

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

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

Expand All @@ -204,7 +212,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 @@ -284,21 +292,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 > maskedNumber > noArgs 1`] = `"2512"`;

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

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

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

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

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

Expand All @@ -308,6 +320,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 @@ -96,14 +96,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('maskedNumber', (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 @@ -189,7 +189,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 @@ -203,7 +203,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('maskedNumber()', () => {
it('should set a default length', () => {
const expected = 4; // default account mask length
const mask = faker.finance.maskedNumber({
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.maskedNumber({
length: expected,
parens: false,
ellipsis: false,
}); // the length of mask picks 4 if the random number generator picks 0

expect(
mask,
Expand Down