isByteLength.js 905 B

12345678910111213141516171819202122
  1. 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); }
  2. import assertString from './util/assertString';
  3. /* eslint-disable prefer-rest-params */
  4. export default function isByteLength(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: isByteLength(str, min [, max])
  13. min = arguments[1];
  14. max = arguments[2];
  15. }
  16. var len = encodeURI(str).split(/%..|./).length - 1;
  17. return len >= min && (typeof max === 'undefined' || len <= max);
  18. }