isBase64.js 1005 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isBase64;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. var _merge = _interopRequireDefault(require("./util/merge"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var notBase64 = /[^A-Z0-9+\/=]/i;
  10. var urlSafeBase64 = /^[A-Z0-9_\-]*$/i;
  11. var defaultBase64Options = {
  12. urlSafe: false
  13. };
  14. function isBase64(str, options) {
  15. (0, _assertString.default)(str);
  16. options = (0, _merge.default)(options, defaultBase64Options);
  17. var len = str.length;
  18. if (options.urlSafe) {
  19. return urlSafeBase64.test(str);
  20. }
  21. if (len % 4 !== 0 || notBase64.test(str)) {
  22. return false;
  23. }
  24. var firstPaddingChar = str.indexOf('=');
  25. return firstPaddingChar === -1 || firstPaddingChar === len - 1 || firstPaddingChar === len - 2 && str[len - 1] === '=';
  26. }
  27. module.exports = exports.default;
  28. module.exports.default = exports.default;