isByteLength.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
  6. exports.default = isByteLength;
  7. var _assertString = require('./util/assertString');
  8. var _assertString2 = _interopRequireDefault(_assertString);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /* eslint-disable prefer-rest-params */
  11. function isByteLength(str, options) {
  12. (0, _assertString2.default)(str);
  13. var min = void 0;
  14. var max = void 0;
  15. if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object') {
  16. min = options.min || 0;
  17. max = options.max;
  18. } else {
  19. // backwards compatibility: isByteLength(str, min [, max])
  20. min = arguments[1];
  21. max = arguments[2];
  22. }
  23. var len = encodeURI(str).split(/%..|./).length - 1;
  24. return len >= min && (typeof max === 'undefined' || len <= max);
  25. }
  26. module.exports = exports['default'];