isLength.js 1006 B

123456789101112131415161718192021
  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)/g) || [];
  17. var surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
  18. var len = str.length - presentationSequences.length - surrogatePairs.length;
  19. return len >= min && (typeof max === 'undefined' || len <= max);
  20. }