{"size": 3284, "ext": "js", "max_stars_repo_path": "studio/node_modules/@sanity/schema/lib/core/traverseSchema.js", "max_stars_repo_name": "liammews/Briefrr", "max_stars_repo_stars_event_min_datetime": "2018-03-25T02:20:11.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-15T07:30:27.000Z", "max_issues_repo_path": "studio/node_modules/@sanity/schema/lib/core/traverseSchema.js", "max_issues_repo_name": "liammews/Briefrr", "max_issues_repo_issues_event_min_datetime": "2018-03-30T19:12:20.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-28T07:25:07.000Z", "max_forks_repo_path": "studio/node_modules/@sanity/schema/lib/core/traverseSchema.js", "max_forks_repo_name": "liammews/Briefrr", "max_forks_repo_forks_event_min_datetime": "2018-03-30T19:09:35.000Z", "max_forks_repo_forks_event_max_datetime": "2018-03-30T19:09:35.000Z"}
{"max_stars_count": 3, "max_issues_count": 38, "max_forks_count": 1, "avg_line_length": 27.8305084746, "max_line_length": 209, "alphanum_fraction": 0.6607795371}
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = traverseSchema;
exports.UnknownType = void 0;

var _uniq2 = _interopRequireDefault(require("lodash/uniq"));

var _flatten2 = _interopRequireDefault(require("lodash/flatten"));

var _getDupes = require("../sanity/validation/utils/getDupes");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var NOOP_VISITOR = typeDef => typeDef;

class UnknownType {
  constructor(name) {
    _defineProperty(this, "name", void 0);

    this.name = name;
  }

}

exports.UnknownType = UnknownType;
var TYPE_TYPE = {
  name: 'type',
  type: null
};
var FUTURE_RESERVED = ['any', 'time', 'date'];

function traverseSchema() {
  var types = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var coreTypes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var visitor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NOOP_VISITOR;
  var coreTypesRegistry = Object.create(null);
  var registry = Object.create(null);
  var coreTypeNames = coreTypes.map(typeDef => typeDef.name);
  var reservedTypeNames = FUTURE_RESERVED.concat(coreTypeNames);
  var typeNames = types.map(typeDef => typeDef && typeDef.name).filter(Boolean);
  coreTypes.forEach(coreType => {
    coreTypesRegistry[coreType.name] = coreType;
  });
  types.forEach((type, i) => {
    // Allocate a placeholder for each type
    registry[type && type.name || "__unnamed_".concat(i)] = {};
  });

  function getType(typeName) {
    return typeName === 'type' ? TYPE_TYPE : coreTypesRegistry[typeName] || registry[typeName] || null;
  }

  var duplicateNames = (0, _uniq2.default)((0, _flatten2.default)((0, _getDupes.getDupes)(typeNames)));

  function isDuplicate(typeName) {
    return duplicateNames.includes(typeName);
  }

  function getTypeNames() {
    return typeNames.concat(coreTypeNames);
  }

  function isReserved(typeName) {
    return typeName === 'type' || reservedTypeNames.includes(typeName);
  }

  var visitType = isRoot => (typeDef, index) => {
    return visitor(typeDef, {
      visit: visitType(false),
      isRoot,
      getType,
      getTypeNames,
      isReserved,
      isDuplicate,
      index
    });
  };

  coreTypes.forEach(coreTypeDef => {
    Object.assign(coreTypesRegistry[coreTypeDef.name], visitType(coreTypeDef));
  });
  types.forEach((typeDef, i) => {
    Object.assign(registry[typeDef && typeDef.name || "__unnamed_".concat(i)], visitType(true)(typeDef, i));
  });
  return {
    get(typeName) {
      var res = registry[typeName] || coreTypesRegistry[typeName];

      if (res) {
        return res;
      }

      throw new Error("No such type: ".concat(typeName));
    },

    has(typeName) {
      return typeName in registry || typeName in coreTypesRegistry;
    },

    getTypeNames() {
      return Object.keys(registry);
    },

    getTypes() {
      return this.getTypeNames().map(this.get);
    },

    toJSON() {
      return this.getTypes();
    }

  };
}