Skip to content

Commit

Permalink
chore: return TuiPaymentSystem enum
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Jul 12, 2022
1 parent 67dc598 commit 4f063d5
Show file tree
Hide file tree
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';

Expand All @@ -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({
Expand All @@ -40,7 +40,7 @@ export class TuiCardComponent {

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

@Input()
@HostBinding('attr.data-size')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});
});

Expand Down
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 === '') {
Expand All @@ -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;
Expand Down
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;
Expand All @@ -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);
});
});
});
Loading

0 comments on commit 4f063d5

Please sign in to comment.