isByteLength.js 1.1 KB

123456789101112131415161718192021222324252627
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isByteLength;
  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 isByteLength(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: isByteLength(str, min [, max])
  19. min = arguments[1];
  20. max = arguments[2];
  21. }
  22. var len = encodeURI(str).split(/%..|./).length - 1;
  23. return len >= min && (typeof max === 'undefined' || len <= max);
  24. }
  25. module.exports = exports.default;
  26. module.exports.default = exports.default;