isByteLength.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  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(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  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;