isJSON.js 892 B

1234567891011121314151617181920
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  2. import assertString from './util/assertString';
  3. import includes from './util/includesArray';
  4. import merge from './util/merge';
  5. var default_json_options = {
  6. allow_primitives: false
  7. };
  8. export default function isJSON(str, options) {
  9. assertString(str);
  10. try {
  11. options = merge(options, default_json_options);
  12. var primitives = [];
  13. if (options.allow_primitives) {
  14. primitives = [null, false, true];
  15. }
  16. var obj = JSON.parse(str);
  17. return includes(primitives, obj) || !!obj && _typeof(obj) === 'object';
  18. } catch (e) {/* ignore */}
  19. return false;
  20. }