Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 8f2ae6f

Browse files
feat(rules): Added Decimal rule
1 parent 14413ba commit 8f2ae6f

File tree

5 files changed

+390
-0
lines changed

5 files changed

+390
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
114114
- [Cpf](docs/cpf.md)
115115
- [CreditCard](docs/credit-card.md)
116116
- [CurrencyCode](docs/currency-code.md)
117+
- [Currency](docs/currency.md)
117118
- [DataUri](docs/data-uri.md)
118119
- [DateTime](docs/date-time.md)
119120
- [Decimal](docs/decimal.md)

docs/currency.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Currecy
2+
3+
Validates if input is a currecy.
4+
5+
Valid values:
6+
7+
```js
8+
validator.currecy().validate('-$10,123.45');
9+
validator.currecy().validate('$10,123.45');
10+
validator.currecy().validate('$10123.45');
11+
validator.currecy().validate('$0.10');
12+
```
13+
14+
Invalid values:
15+
16+
```js
17+
validator.currecy().validate('1.234');
18+
validator.currecy().validate('$1.1');
19+
validator.currecy().validate('$ 32.50');
20+
validator.currecy().validate('500$');
21+
```

src/rules/currency.ts

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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;

src/rules/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export * from './country-code';
3232
export * from './cpf';
3333
export * from './credit-card';
3434
export * from './currency-code';
35+
export * from './currency';
3536
export * from './data-uri';
3637
export * from './date-time';
3738
export * from './decimal';

0 commit comments

Comments
 (0)