react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / lib / js-yaml / schema.js
80698 views'use strict';12/*eslint-disable max-len*/34var common = require('./common');5var YAMLException = require('./exception');6var Type = require('./type');789function compileList(schema, name, result) {10var exclude = [];1112schema.include.forEach(function (includedSchema) {13result = compileList(includedSchema, name, result);14});1516schema[name].forEach(function (currentType) {17result.forEach(function (previousType, previousIndex) {18if (previousType.tag === currentType.tag) {19exclude.push(previousIndex);20}21});2223result.push(currentType);24});2526return result.filter(function (type, index) {27return -1 === exclude.indexOf(index);28});29}303132function compileMap(/* lists... */) {33var result = {}, index, length;3435function collectType(type) {36result[type.tag] = type;37}3839for (index = 0, length = arguments.length; index < length; index += 1) {40arguments[index].forEach(collectType);41}4243return result;44}454647function Schema(definition) {48this.include = definition.include || [];49this.implicit = definition.implicit || [];50this.explicit = definition.explicit || [];5152this.implicit.forEach(function (type) {53if (type.loadKind && 'scalar' !== type.loadKind) {54throw new YAMLException('There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported.');55}56});5758this.compiledImplicit = compileList(this, 'implicit', []);59this.compiledExplicit = compileList(this, 'explicit', []);60this.compiledTypeMap = compileMap(this.compiledImplicit, this.compiledExplicit);61}626364Schema.DEFAULT = null;656667Schema.create = function createSchema() {68var schemas, types;6970switch (arguments.length) {71case 1:72schemas = Schema.DEFAULT;73types = arguments[0];74break;7576case 2:77schemas = arguments[0];78types = arguments[1];79break;8081default:82throw new YAMLException('Wrong number of arguments for Schema.create function');83}8485schemas = common.toArray(schemas);86types = common.toArray(types);8788if (!schemas.every(function (schema) { return schema instanceof Schema; })) {89throw new YAMLException('Specified list of super schemas (or a single Schema object) contains a non-Schema object.');90}9192if (!types.every(function (type) { return type instanceof Type; })) {93throw new YAMLException('Specified list of YAML types (or a single Type object) contains a non-Type object.');94}9596return new Schema({97include: schemas,98explicit: types99});100};101102103module.exports = Schema;104105106