Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: return TuiPaymentSystem enum
Browse files Browse the repository at this point in the history
vladimirpotekhin committed Jul 11, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ec17fb3 commit 3c62ca3
Showing 19 changed files with 116 additions and 115 deletions.
16 changes: 8 additions & 8 deletions projects/addon-commerce/components/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ChangeDetectionStrategy, Component, HostBinding, Input} from '@angular/core';
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/types';
import {TuiPaymentSystem, TuiPaymentSystemT} from '@taiga-ui/addon-commerce/enums';
import {tuiDefaultProp} from '@taiga-ui/cdk';
import {TuiSizeS} from '@taiga-ui/core';

@@ -10,12 +10,12 @@ export function cardNumberAssertion({length}: string): boolean {

export const cardNumberAssertionMessage = 'cardNumber should contain 4 symbols';

const icons: Record<TuiPaymentSystem, string> = {
mir: 'tuiIconMirMono',
visa: 'tuiIconVisaMono',
electron: 'tuiIconElectronMono',
mastercard: 'tuiIconMastercard',
maestro: 'tuiIconMaestro',
const icons = {
[TuiPaymentSystem.Mir]: 'tuiIconMirMono',
[TuiPaymentSystem.Visa]: 'tuiIconVisaMono',
[TuiPaymentSystem.Electron]: 'tuiIconElectronMono',
[TuiPaymentSystem.Mastercard]: 'tuiIconMastercard',
[TuiPaymentSystem.Maestro]: 'tuiIconMaestro',
};

@Component({
@@ -40,7 +40,7 @@ export class TuiCardComponent {

@Input()
@tuiDefaultProp()
paymentSystem: TuiPaymentSystem | null = null;
paymentSystem: TuiPaymentSystem | TuiPaymentSystemT | null = null;

@Input()
@HostBinding('attr.data-size')
Original file line number Diff line number Diff line change
@@ -17,8 +17,9 @@ import {
} from '@angular/core';
import {NgControl} from '@angular/forms';
import {TUI_CARD_MASK, tuiDefaultCardValidator} from '@taiga-ui/addon-commerce/constants';
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/enums';
import {TuiCard} from '@taiga-ui/addon-commerce/interfaces';
import {TuiCodeCVCLength, TuiPaymentSystem} from '@taiga-ui/addon-commerce/types';
import {TuiCodeCVCLength} from '@taiga-ui/addon-commerce/types';
import {
tuiCreateAutoCorrectedExpirePipe,
tuiGetPaymentSystem,
@@ -63,12 +64,12 @@ const STUB: TuiCard = {
expire: '',
cvc: '',
};
const ICONS: Record<TuiPaymentSystem, string> = {
mir: 'tuiIconMir',
visa: 'tuiIconVisa',
electron: 'tuiIconElectron',
mastercard: 'tuiIconMastercard',
maestro: 'tuiIconMaestro',
const ICONS = {
[TuiPaymentSystem.Mir]: 'tuiIconMir',
[TuiPaymentSystem.Visa]: 'tuiIconVisa',
[TuiPaymentSystem.Electron]: 'tuiIconElectron',
[TuiPaymentSystem.Mastercard]: 'tuiIconMastercard',
[TuiPaymentSystem.Maestro]: 'tuiIconMaestro',
};

// @dynamic
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import {
} from '@angular/core';
import {NgControl} from '@angular/forms';
import {TUI_CARD_MASK} from '@taiga-ui/addon-commerce/constants';
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/types';
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/enums';
import {tuiGetPaymentSystem} from '@taiga-ui/addon-commerce/utils';
import {
AbstractTuiControl,
@@ -29,12 +29,12 @@ import {
} from '@taiga-ui/core';
import {TextMaskConfig} from 'angular2-text-mask';

const icons: Record<TuiPaymentSystem, string> = {
mir: 'tuiIconMir',
visa: 'tuiIconVisa',
electron: 'tuiIconElectron',
mastercard: 'tuiIconMastercard',
maestro: 'tuiIconMaestro',
const icons = {
[TuiPaymentSystem.Mir]: 'tuiIconMir',
[TuiPaymentSystem.Visa]: 'tuiIconVisa',
[TuiPaymentSystem.Electron]: 'tuiIconElectron',
[TuiPaymentSystem.Mastercard]: 'tuiIconMastercard',
[TuiPaymentSystem.Maestro]: 'tuiIconMaestro',
};

// eslint-disable-next-line @typescript-eslint/naming-convention
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FormControl, ReactiveFormsModule} from '@angular/forms';
import {configureTestSuite} from '@taiga-ui/testing';

import {TuiPaymentSystem} from '../../../enums/payment-system';
import {TuiInputCardComponent} from '../input-card.component';
import {TuiInputCardModule} from '../input-card.module';

@@ -82,31 +83,33 @@ describe('InputCard', () => {
it('visa', () => {
testComponent.control.setValue('4111 1111 1111 1111');

expect(testComponent.component.paymentSystem).toBe('visa');
expect(testComponent.component.paymentSystem).toBe(TuiPaymentSystem.Visa);
});

it('electron', () => {
testComponent.control.setValue('4917300800000000');

expect(testComponent.component.paymentSystem).toBe('electron');
expect(testComponent.component.paymentSystem).toBe(TuiPaymentSystem.Electron);
});

it('mir', () => {
testComponent.control.setValue('2200654321000000');

expect(testComponent.component.paymentSystem).toBe('mir');
expect(testComponent.component.paymentSystem).toBe(TuiPaymentSystem.Mir);
});

it('mastercard', () => {
testComponent.control.setValue('5500 0000 0000 0004');

expect(testComponent.component.paymentSystem).toBe('mastercard');
expect(testComponent.component.paymentSystem).toBe(
TuiPaymentSystem.Mastercard,
);
});

it('maestro', () => {
testComponent.control.setValue('6759649826438453');

expect(testComponent.component.paymentSystem).toBe('maestro');
expect(testComponent.component.paymentSystem).toBe(TuiPaymentSystem.Maestro);
});
});

4 changes: 2 additions & 2 deletions projects/addon-commerce/constants/default-card-validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isCardLengthValid, isCardNumberValid} from '@taiga-ui/addon-commerce/utils';
import {tuiIsCardLengthValid, tuiIsCardNumberValid} from '@taiga-ui/addon-commerce/utils';
import {TuiBooleanHandler} from '@taiga-ui/cdk';

export const tuiDefaultCardValidator: TuiBooleanHandler<string> = card =>
card.length > 11 && isCardLengthValid(card) && isCardNumberValid(card);
card.length > 11 && tuiIsCardLengthValid(card) && tuiIsCardNumberValid(card);
1 change: 1 addition & 0 deletions projects/addon-commerce/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './currency';
export * from './currency-code';
export * from './payment-system';
9 changes: 9 additions & 0 deletions projects/addon-commerce/enums/payment-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const enum TuiPaymentSystem {
Visa = 'visa',
Electron = 'electron',
Mastercard = 'mastercard',
Maestro = 'maestro',
Mir = 'mir',
}

export type TuiPaymentSystemT = 'visa' | 'electron' | 'mastercard' | 'maestro' | 'mir';
1 change: 0 additions & 1 deletion projects/addon-commerce/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './code-length';
export * from './currency-variants';
export * from './money-sign';
export * from './payment-system';
1 change: 0 additions & 1 deletion projects/addon-commerce/types/payment-system.ts

This file was deleted.

12 changes: 6 additions & 6 deletions projects/addon-commerce/utils/get-payment-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/types';
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/enums';

export function tuiGetPaymentSystem(cardNumber: string): TuiPaymentSystem | null {
if (cardNumber === '') {
@@ -11,23 +11,23 @@ export function tuiGetPaymentSystem(cardNumber: string): TuiPaymentSystem | null
const four = Number.parseInt(cardNumber.slice(0, 4), 10);

if (tuiIsMaestro(three, two, one)) {
return 'maestro';
return TuiPaymentSystem.Maestro;
}

if (tuiIsMastercard(four, two, one)) {
return 'mastercard';
return TuiPaymentSystem.Mastercard;
}

if (tuiIsMir(four)) {
return 'mir';
return TuiPaymentSystem.Mir;
}

if (tuiIsElectron(four)) {
return 'electron';
return TuiPaymentSystem.Electron;
}

if (tuiIsVisa(one)) {
return 'visa';
return TuiPaymentSystem.Visa;
}

return null;
17 changes: 8 additions & 9 deletions projects/addon-commerce/utils/is-card-length-valid.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce/enums';

import {tuiGetPaymentSystem} from './get-payment-system';

/**
* @deprecated: use {@link tuiIsCardLengthValid} instead
* Validates card number length using payment system dictionary
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export function isCardLengthValid(cardNumber: string): boolean {
export function tuiIsCardLengthValid(cardNumber: string): boolean {
const {length} = cardNumber;
const paymentSystem = tuiGetPaymentSystem(cardNumber);

switch (paymentSystem) {
case 'electron':
case TuiPaymentSystem.Electron:
return length === 16;
case 'maestro':
case TuiPaymentSystem.Maestro:
return length > 11 && length < 20;
case 'mastercard':
case 'mir':
case TuiPaymentSystem.Mastercard:
case TuiPaymentSystem.Mir:
return length > 15 && length < 20;
case 'visa':
case TuiPaymentSystem.Visa:
return length > 12 && length < 20;
default:
return length > 8 && length < 20;
}
}

export const tuiIsCardLengthValid = isCardLengthValid;
5 changes: 1 addition & 4 deletions projects/addon-commerce/utils/is-card-number-valid.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {TUI_NON_DIGITS_REGEXP} from '@taiga-ui/core';

/**
* @deprecated: use {@link tuiIsCardNumberValid} instead
* Validates card number using Luhn algorithm
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export function isCardNumberValid(value: string | number): boolean {
export function tuiIsCardNumberValid(value: string | number): boolean {
const cardNumber = String(value).replace(TUI_NON_DIGITS_REGEXP, '');

const {length} = cardNumber;
@@ -23,5 +22,3 @@ export function isCardNumberValid(value: string | number): boolean {

return !(arr.reduce((a, b) => a + b, 0) % 10);
}

export const tuiIsCardNumberValid = isCardNumberValid;
54 changes: 28 additions & 26 deletions projects/addon-commerce/utils/test/get-payment-system.spec.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,107 @@
import {TuiPaymentSystem} from '@taiga-ui/addon-commerce';

import {tuiGetPaymentSystem} from '../get-payment-system';

describe('getPaymentSystem', () => {
describe('Visa', () => {
it('4', () => {
expect(tuiGetPaymentSystem('4000')).toBe('visa');
expect(tuiGetPaymentSystem('4000')).toBe(TuiPaymentSystem.Visa);
});
});

describe('Electron', () => {
describe(TuiPaymentSystem.Electron, () => {
it('4026', () => {
expect(tuiGetPaymentSystem('4026')).toBe('electron');
expect(tuiGetPaymentSystem('4026')).toBe(TuiPaymentSystem.Electron);
});

it('4175', () => {
expect(tuiGetPaymentSystem('4175')).toBe('electron');
expect(tuiGetPaymentSystem('4175')).toBe(TuiPaymentSystem.Electron);
});

it('4405', () => {
expect(tuiGetPaymentSystem('4405')).toBe('electron');
expect(tuiGetPaymentSystem('4405')).toBe(TuiPaymentSystem.Electron);
});

it('4508', () => {
expect(tuiGetPaymentSystem('4508')).toBe('electron');
expect(tuiGetPaymentSystem('4508')).toBe(TuiPaymentSystem.Electron);
});

it('4844', () => {
expect(tuiGetPaymentSystem('4844')).toBe('electron');
expect(tuiGetPaymentSystem('4844')).toBe(TuiPaymentSystem.Electron);
});

it('4913', () => {
expect(tuiGetPaymentSystem('4913')).toBe('electron');
expect(tuiGetPaymentSystem('4913')).toBe(TuiPaymentSystem.Electron);
});

it('4917', () => {
expect(tuiGetPaymentSystem('4917')).toBe('electron');
expect(tuiGetPaymentSystem('4917')).toBe(TuiPaymentSystem.Electron);
});
});

describe('Mastercard', () => {
describe(TuiPaymentSystem.Mastercard, () => {
it('2221', () => {
expect(tuiGetPaymentSystem('2221')).toBe('mastercard');
expect(tuiGetPaymentSystem('2221')).toBe(TuiPaymentSystem.Mastercard);
});

it('2720', () => {
expect(tuiGetPaymentSystem('2720')).toBe('mastercard');
expect(tuiGetPaymentSystem('2720')).toBe(TuiPaymentSystem.Mastercard);
});

it('5100', () => {
expect(tuiGetPaymentSystem('5100')).toBe('mastercard');
expect(tuiGetPaymentSystem('5100')).toBe(TuiPaymentSystem.Mastercard);
});

it('5500', () => {
expect(tuiGetPaymentSystem('5500')).toBe('mastercard');
expect(tuiGetPaymentSystem('5500')).toBe(TuiPaymentSystem.Mastercard);
});

it('5', () => {
expect(tuiGetPaymentSystem('5')).toBe('mastercard');
expect(tuiGetPaymentSystem('5')).toBe(TuiPaymentSystem.Mastercard);
});
});

describe('Maestro', () => {
describe(TuiPaymentSystem.Maestro, () => {
it('5000', () => {
expect(tuiGetPaymentSystem('5000')).toBe('maestro');
expect(tuiGetPaymentSystem('5000')).toBe(TuiPaymentSystem.Maestro);
});

it('5090', () => {
expect(tuiGetPaymentSystem('5090')).toBe('maestro');
expect(tuiGetPaymentSystem('5090')).toBe(TuiPaymentSystem.Maestro);
});

it('5600', () => {
expect(tuiGetPaymentSystem('5600')).toBe('maestro');
expect(tuiGetPaymentSystem('5600')).toBe(TuiPaymentSystem.Maestro);
});

it('5890', () => {
expect(tuiGetPaymentSystem('5890')).toBe('maestro');
expect(tuiGetPaymentSystem('5890')).toBe(TuiPaymentSystem.Maestro);
});

it('6000', () => {
expect(tuiGetPaymentSystem('6000')).toBe('maestro');
expect(tuiGetPaymentSystem('6000')).toBe(TuiPaymentSystem.Maestro);
});

it('50', () => {
expect(tuiGetPaymentSystem('50')).toBe('maestro');
expect(tuiGetPaymentSystem('50')).toBe(TuiPaymentSystem.Maestro);
});

it('56', () => {
expect(tuiGetPaymentSystem('56')).toBe('maestro');
expect(tuiGetPaymentSystem('56')).toBe(TuiPaymentSystem.Maestro);
});

it('58', () => {
expect(tuiGetPaymentSystem('58')).toBe('maestro');
expect(tuiGetPaymentSystem('58')).toBe(TuiPaymentSystem.Maestro);
});
});

describe('Mir', () => {
it('2200', () => {
expect(tuiGetPaymentSystem('2200')).toBe('mir');
expect(tuiGetPaymentSystem('2200')).toBe(TuiPaymentSystem.Mir);
});

it('2204', () => {
expect(tuiGetPaymentSystem('2204')).toBe('mir');
expect(tuiGetPaymentSystem('2204')).toBe(TuiPaymentSystem.Mir);
});
});
});
38 changes: 19 additions & 19 deletions projects/addon-commerce/utils/test/is-card-length-valid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
import {isCardLengthValid} from '../is-card-length-valid';
import {tuiIsCardLengthValid} from '../is-card-length-valid';

describe('isCardLengthValid', () => {
describe('Visa', () => {
it('cannot be 12 digits long', () => {
expect(isCardLengthValid('400000000000')).toBe(false);
expect(tuiIsCardLengthValid('400000000000')).toBe(false);
});

it('can be 13 digits long', () => {
expect(isCardLengthValid('4000000000000')).toBe(true);
expect(tuiIsCardLengthValid('4000000000000')).toBe(true);
});

it('can be 19 digits long', () => {
expect(isCardLengthValid('4000000000000000000')).toBe(true);
expect(tuiIsCardLengthValid('4000000000000000000')).toBe(true);
});
});

describe('Electron', () => {
it('cannot be 15 digits long', () => {
expect(isCardLengthValid('402600000000000')).toBe(false);
expect(tuiIsCardLengthValid('402600000000000')).toBe(false);
});

it('can only be 16 digits long', () => {
expect(isCardLengthValid('4026000000000000')).toBe(true);
expect(tuiIsCardLengthValid('4026000000000000')).toBe(true);
});

it('cannot be 17 digits long', () => {
expect(isCardLengthValid('40260000000000000')).toBe(false);
expect(tuiIsCardLengthValid('40260000000000000')).toBe(false);
});
});

describe('Mastercard', () => {
it('cannot be 15 digits long', () => {
expect(isCardLengthValid('510000000000000')).toBe(false);
expect(tuiIsCardLengthValid('510000000000000')).toBe(false);
});

it('can be 16 digits long', () => {
expect(isCardLengthValid('5100000000000000')).toBe(true);
expect(tuiIsCardLengthValid('5100000000000000')).toBe(true);
});

it('can be 19 digits long', () => {
expect(isCardLengthValid('5100000000000000000')).toBe(true);
expect(tuiIsCardLengthValid('5100000000000000000')).toBe(true);
});
});

describe('Maestro', () => {
it('can be 12 digits long', () => {
expect(isCardLengthValid('560000000000')).toBe(true);
expect(tuiIsCardLengthValid('560000000000')).toBe(true);
});

it('can be 19 digits long', () => {
expect(isCardLengthValid('5600000000000000000')).toBe(true);
expect(tuiIsCardLengthValid('5600000000000000000')).toBe(true);
});
});

describe('Mir', () => {
it('cannot be 15 digits long', () => {
expect(isCardLengthValid('220000000000000')).toBe(false);
expect(tuiIsCardLengthValid('220000000000000')).toBe(false);
});

it('can be 16 digits long', () => {
expect(isCardLengthValid('2200000000000000')).toBe(true);
expect(tuiIsCardLengthValid('2200000000000000')).toBe(true);
});

it('can be 19 digits long', () => {
expect(isCardLengthValid('2200000000000000000')).toBe(true);
expect(tuiIsCardLengthValid('2200000000000000000')).toBe(true);
});
});

describe('Unknown', () => {
it('cannot be 7 digits long', () => {
expect(isCardLengthValid('3566002')).toBe(false);
expect(tuiIsCardLengthValid('3566002')).toBe(false);
});

it('can be 16 digits long', () => {
expect(isCardLengthValid('3566002020360505')).toBe(true);
expect(tuiIsCardLengthValid('3566002020360505')).toBe(true);
});

it('can be 19 digits long', () => {
expect(isCardLengthValid('3566002020360505000')).toBe(true);
expect(tuiIsCardLengthValid('3566002020360505000')).toBe(true);
});

it('cannot be 20 digits long', () => {
expect(isCardLengthValid('35660020203605050000')).toBe(false);
expect(tuiIsCardLengthValid('35660020203605050000')).toBe(false);
});
});
});
4 changes: 2 additions & 2 deletions projects/addon-commerce/validators/card-number.validator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {AbstractControl, ValidationErrors} from '@angular/forms';
import {isCardNumberValid} from '@taiga-ui/addon-commerce/utils';
import {tuiIsCardNumberValid} from '@taiga-ui/addon-commerce/utils';
import {TuiValidationError} from '@taiga-ui/cdk';

export function tuiCardNumberValidator({
value,
}: AbstractControl): ValidationErrors | null {
return value?.card && !isCardNumberValid(value.card)
return value?.card && !tuiIsCardNumberValid(value.card)
? {card: new TuiValidationError('Invalid card number')}
: null;
}
6 changes: 4 additions & 2 deletions projects/addon-commerce/validators/luhn.validator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {AbstractControl, ValidatorFn} from '@angular/forms';
import {isCardNumberValid} from '@taiga-ui/addon-commerce/utils';
import {tuiIsCardNumberValid} from '@taiga-ui/addon-commerce/utils';
import {TuiValidationError} from '@taiga-ui/cdk';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';

export function tuiCreateLuhnValidator(message: PolymorpheusContent): ValidatorFn {
return ({value}: AbstractControl) => {
return isCardNumberValid(value) ? null : {luhn: new TuiValidationError(message)};
return tuiIsCardNumberValid(value)
? null
: {luhn: new TuiValidationError(message)};
};
}
11 changes: 0 additions & 11 deletions projects/cdk/schematics/ng-update/constants/enums.ts
Original file line number Diff line number Diff line change
@@ -5,17 +5,6 @@ interface ReplacementEnum {
}

export const ENUMS_TO_REPLACE: ReplacementEnum[] = [
{
name: 'TuiPaymentSystem',
replaceValues: {
Visa: 'visa',
Electron: 'electron',
Mastercard: 'mastercard',
Maestro: 'maestro',
Mir: 'mir',
},
keepAsType: true,
},
{
name: 'TuiTextAlign',
replaceValues: {
8 changes: 4 additions & 4 deletions projects/demo/src/modules/components/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -45,10 +45,10 @@ export class ExampleTuiCardComponent {
};

paymentSystemVariants: readonly TuiPaymentSystem[] = [
'visa',
'maestro',
'mastercard',
'mir',
TuiPaymentSystem.Visa,
TuiPaymentSystem.Maestro,
TuiPaymentSystem.Mastercard,
TuiPaymentSystem.Mir,
];

brandLogoVariants = ['', ...BRAND_LOGOS];
Original file line number Diff line number Diff line change
@@ -11,6 +11,6 @@ import {TuiPaymentSystem} from '@taiga-ui/addon-commerce';
encapsulation,
})
export class TuiCardExample3 {
readonly paymentSystem: TuiPaymentSystem = 'mir';
readonly paymentSystem = TuiPaymentSystem.Mir;
readonly brandLogo = 'https://ng-web-apis.github.io/dist/assets/images/web-api.svg';
}

0 comments on commit 3c62ca3

Please sign in to comment.