swc/bundler/tests/.cache/deno/9c9b37ee49588ac69071cc12105286e561114082.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

35 lines
749 B
TypeScript

// Loaded from https://deno.land/x/segno@v1.1.0/lib/validations/isJSON.ts
// @ts-ignore allowing typedoc to build
import { assertString } from '../helpers/assertString.ts';
type JSONOptions = {
allowPrimitives?: boolean;
};
/**
* @ignore
*/
const defaultJSONOptions = {
allowPrimitives: false,
};
export const isJSON = (str: string, options?: JSONOptions) => {
assertString(str);
try {
options = { ...defaultJSONOptions, ...options };
let primitives: Array<boolean | null> = [];
if (options.allowPrimitives) {
primitives = [null, false, true];
}
const obj = JSON.parse(str);
return primitives.includes(obj) || (!!obj && typeof obj === 'object');
} catch (e) {
/* ignore */
}
return false;
};