isLength.js 1.3 KB

123456789101112131415161718192021222324252627
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  2. import assertString from './util/assertString';
  3. /* eslint-disable prefer-rest-params */
  4. export default function isLength(str, options) {
  5. assertString(str);
  6. var min;
  7. var max;
  8. if (_typeof(options) === 'object') {
  9. min = options.min || 0;
  10. max = options.max;
  11. } else {
  12. // backwards compatibility: isLength(str, min [, max])
  13. min = arguments[1] || 0;
  14. max = arguments[2];
  15. }
  16. var presentationSequences = str.match(/[^\uFE0F\uFE0E][\uFE0F\uFE0E]/g) || [];
  17. var surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
  18. var len = str.length - presentationSequences.length - surrogatePairs.length;
  19. var isInsideRange = len >= min && (typeof max === 'undefined' || len <= max);
  20. if (isInsideRange && Array.isArray(options === null || options === void 0 ? void 0 : options.discreteLengths)) {
  21. return options.discreteLengths.some(function (discreteLen) {
  22. return discreteLen === len;
  23. });
  24. }
  25. return isInsideRange;
  26. }