1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.IsNumber = exports.isNumber = exports.IS_NUMBER = void 0;
|
4 | const ValidateBy_1 = require("../common/ValidateBy");
|
5 | exports.IS_NUMBER = 'isNumber';
|
6 |
|
7 |
|
8 |
|
9 | function isNumber(value, options = {}) {
|
10 | if (typeof value !== 'number') {
|
11 | return false;
|
12 | }
|
13 | if (value === Infinity || value === -Infinity) {
|
14 | return options.allowInfinity;
|
15 | }
|
16 | if (Number.isNaN(value)) {
|
17 | return options.allowNaN;
|
18 | }
|
19 | if (options.maxDecimalPlaces !== undefined) {
|
20 | let decimalPlaces = 0;
|
21 | if (value % 1 !== 0) {
|
22 | decimalPlaces = value.toString().split('.')[1].length;
|
23 | }
|
24 | if (decimalPlaces > options.maxDecimalPlaces) {
|
25 | return false;
|
26 | }
|
27 | }
|
28 | return Number.isFinite(value);
|
29 | }
|
30 | exports.isNumber = isNumber;
|
31 |
|
32 |
|
33 |
|
34 | function IsNumber(options = {}, validationOptions) {
|
35 | return (0, ValidateBy_1.ValidateBy)({
|
36 | name: exports.IS_NUMBER,
|
37 | constraints: [options],
|
38 | validator: {
|
39 | validate: (value, args) => isNumber(value, args.constraints[0]),
|
40 | defaultMessage: (0, ValidateBy_1.buildMessage)(eachPrefix => eachPrefix + '$property must be a number conforming to the specified constraints', validationOptions),
|
41 | },
|
42 | }, validationOptions);
|
43 | }
|
44 | exports.IsNumber = IsNumber;
|
45 |
|
\ | No newline at end of file |