isJSON.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isJSON;
  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. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  10. var default_json_options = {
  11. allow_primitives: false
  12. };
  13. function isJSON(str, options) {
  14. (0, _assertString.default)(str);
  15. try {
  16. options = (0, _merge.default)(options, default_json_options);
  17. var primitives = [];
  18. if (options.allow_primitives) {
  19. primitives = [null, false, true];
  20. }
  21. var obj = JSON.parse(str);
  22. return primitives.includes(obj) || !!obj && _typeof(obj) === 'object';
  23. } catch (e) {
  24. /* ignore */
  25. }
  26. return false;
  27. }
  28. module.exports = exports.default;
  29. module.exports.default = exports.default;