UNPKG

1.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.IsNumber = exports.isNumber = exports.IS_NUMBER = void 0;
4const ValidateBy_1 = require("../common/ValidateBy");
5exports.IS_NUMBER = 'isNumber';
6/**
7 * Checks if a given value is a number.
8 */
9function 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}
30exports.isNumber = isNumber;
31/**
32 * Checks if a value is a number.
33 */
34function 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}
44exports.IsNumber = IsNumber;
45//# sourceMappingURL=IsNumber.js.map
\No newline at end of file