-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
ec17fb3
commit 3c62ca3
Showing
19 changed files
with
116 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 28 additions & 26 deletions
54
projects/addon-commerce/utils/test/get-payment-system.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
projects/addon-commerce/utils/test/is-card-length-valid.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters