|
| 1 | +import { AbstractRule } from './abstract-rule'; |
| 2 | +import { Regex } from './regex'; |
| 3 | + |
| 4 | +export interface CurrencyOptions { |
| 5 | + symbol?: string, |
| 6 | + requireSymbol?: boolean, |
| 7 | + allowSpaceAfterSymbol?: boolean, |
| 8 | + symbolAfterDigits?: boolean, |
| 9 | + allowNegatives?: boolean, |
| 10 | + parensForNegatives?: boolean, |
| 11 | + negativeSignBeforeDigits?: boolean, |
| 12 | + negativeSignAfterDigits?: boolean, |
| 13 | + allowNegativeSignPlaceholder?: boolean, |
| 14 | + thousandsSeparator?: string, |
| 15 | + decimalSeparator?: string, |
| 16 | + allowDecimal?: boolean, |
| 17 | + requireDecimal?: boolean, |
| 18 | + digitsAfterDecimal?: number[], |
| 19 | + allowSpaceAfterDigits?: boolean |
| 20 | +} |
| 21 | + |
| 22 | +const defaultOptions: CurrencyOptions = { |
| 23 | + symbol: '$', |
| 24 | + requireSymbol: false, |
| 25 | + allowSpaceAfterSymbol: false, |
| 26 | + symbolAfterDigits: false, |
| 27 | + allowNegatives: true, |
| 28 | + parensForNegatives: false, |
| 29 | + negativeSignBeforeDigits: false, |
| 30 | + negativeSignAfterDigits: false, |
| 31 | + allowNegativeSignPlaceholder: false, |
| 32 | + thousandsSeparator: ',', |
| 33 | + decimalSeparator: '.', |
| 34 | + allowDecimal: true, |
| 35 | + requireDecimal: false, |
| 36 | + digitsAfterDecimal: [2], |
| 37 | + allowSpaceAfterDigits: false |
| 38 | +}; |
| 39 | + |
| 40 | +export class Currency extends AbstractRule { |
| 41 | + |
| 42 | + /** |
| 43 | + * Currency. |
| 44 | + */ |
| 45 | + public constructor(public readonly options?: CurrencyOptions) { |
| 46 | + super(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Validate. |
| 51 | + */ |
| 52 | + public validate(input: any): boolean { |
| 53 | + const o: CurrencyOptions = {...defaultOptions, ...this.options}; |
| 54 | + |
| 55 | + const decimalDigits: string = `\\d{${(o.digitsAfterDecimal || []).join('}|\\d{')}}`; |
| 56 | + const symbol: string = `(\\${String(o.symbol).replace(/\./g, '\\.')})${o.requireSymbol ? '' : '?'}`; |
| 57 | + const negative: string = '-?'; |
| 58 | + const dollarAmount: string = `(0|[1-9]\\d*|[1-9]\\d{0,2}(\\${o.thousandsSeparator}\\d{3})*)?`; |
| 59 | + const decimalAmount: string = `(\\${o.decimalSeparator}(${decimalDigits}))${(o.requireDecimal ? '' : '?')}`; |
| 60 | + let pattern: string = dollarAmount + (o.allowDecimal || o.requireDecimal ? decimalAmount : ''); |
| 61 | + |
| 62 | + if (o.allowNegatives && !o.parensForNegatives) { |
| 63 | + if (o.negativeSignAfterDigits) { |
| 64 | + pattern = pattern + negative; |
| 65 | + } else if (o.negativeSignBeforeDigits) { |
| 66 | + pattern = negative + pattern; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (o.allowNegativeSignPlaceholder) { |
| 71 | + pattern = `( (?!\\-))?${pattern}`; |
| 72 | + } else if (o.allowSpaceAfterSymbol) { |
| 73 | + pattern = ` ?${pattern}`; |
| 74 | + } else if (o.allowSpaceAfterDigits) { |
| 75 | + pattern = `${pattern}( (?!$))?`; |
| 76 | + } |
| 77 | + |
| 78 | + pattern = (o.symbolAfterDigits) ? pattern + symbol : symbol + pattern; |
| 79 | + |
| 80 | + if (o.allowNegatives) { |
| 81 | + if (o.parensForNegatives) { |
| 82 | + pattern = `(\\(${pattern}\\)|${pattern})`; |
| 83 | + } else if (!(o.negativeSignBeforeDigits || o.negativeSignAfterDigits)) { |
| 84 | + pattern = negative + pattern; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return new Regex(`^(?!-? )(?=.*\\d)${pattern}$`).validate(input); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +export default Currency; |
0 commit comments