isLength.js 1.3 KB

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isLength;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. 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); }
  9. /* eslint-disable prefer-rest-params */
  10. function isLength(str, options) {
  11. (0, _assertString.default)(str);
  12. var min;
  13. var max;
  14. if (_typeof(options) === 'object') {
  15. min = options.min || 0;
  16. max = options.max;
  17. } else {
  18. // backwards compatibility: isLength(str, min [, max])
  19. min = arguments[1] || 0;
  20. max = arguments[2];
  21. }
  22. var presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || [];
  23. var surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
  24. var len = str.length - presentationSequences.length - surrogatePairs.length;
  25. return len >= min && (typeof max === 'undefined' || len <= max);
  26. }
  27. module.exports = exports.default;
  28. module.exports.default = exports.default;