isDecimal.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isDecimal;
  6. var _merge = _interopRequireDefault(require("./util/merge"));
  7. var _assertString = _interopRequireDefault(require("./util/assertString"));
  8. var _includes = _interopRequireDefault(require("./util/includes"));
  9. var _alpha = require("./alpha");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function decimalRegExp(options) {
  12. var regExp = new RegExp("^[-+]?([0-9]+)?(\\".concat(_alpha.decimal[options.locale], "[0-9]{").concat(options.decimal_digits, "})").concat(options.force_decimal ? '' : '?', "$"));
  13. return regExp;
  14. }
  15. var default_decimal_options = {
  16. force_decimal: false,
  17. decimal_digits: '1,',
  18. locale: 'en-US'
  19. };
  20. var blacklist = ['', '-', '+'];
  21. function isDecimal(str, options) {
  22. (0, _assertString.default)(str);
  23. options = (0, _merge.default)(options, default_decimal_options);
  24. if (options.locale in _alpha.decimal) {
  25. return !(0, _includes.default)(blacklist, str.replace(/ /g, '')) && decimalRegExp(options).test(str);
  26. }
  27. throw new Error("Invalid locale '".concat(options.locale, "'"));
  28. }
  29. module.exports = exports.default;
  30. module.exports.default = exports.default;