swc/bundler/tests/.cache/deno/bd292ad393bc391f9f3f669e7fff1f44413893f1.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

52 lines
944 B
TypeScript

// Loaded from https://deno.land/x/segno@v1.1.0/lib/validations/isBase64.ts
// @ts-ignore allowing typedoc to build
import { assertString } from '../helpers/assertString.ts';
/**
* @ignore
*/
const notBase64 = /[^A-Z0-9+\/=]/i;
/**
* @ignore
*/
const urlSafeBase64 = /^[A-Z0-9_\-]+$/i;
type Base64Options = {
urlSafe?: boolean;
};
/**
* @ignore
*/
const defaultBase64Options: Base64Options = {
urlSafe: false,
};
export const isBase64 = (str: string, options?: Base64Options) => {
assertString(str);
options = {
...defaultBase64Options,
...options,
};
const len = str.length;
if (options.urlSafe) {
return urlSafeBase64.test(str);
}
if (!len || len % 4 !== 0 || notBase64.test(str)) {
return false;
}
const firstPaddingChar = str.indexOf('=');
return (
firstPaddingChar === -1 ||
firstPaddingChar === len - 1 ||
(firstPaddingChar === len - 2 && str[len - 1] === '=')
);
};