l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

schema.js (2753B)


      1 'use strict';
      2 
      3 /*eslint-disable max-len*/
      4 
      5 var common        = require('./common');
      6 var YAMLException = require('./exception');
      7 var Type          = require('./type');
      8 
      9 
     10 function compileList(schema, name, result) {
     11   var exclude = [];
     12 
     13   schema.include.forEach(function (includedSchema) {
     14     result = compileList(includedSchema, name, result);
     15   });
     16 
     17   schema[name].forEach(function (currentType) {
     18     result.forEach(function (previousType, previousIndex) {
     19       if (previousType.tag === currentType.tag && previousType.kind === currentType.kind) {
     20         exclude.push(previousIndex);
     21       }
     22     });
     23 
     24     result.push(currentType);
     25   });
     26 
     27   return result.filter(function (type, index) {
     28     return exclude.indexOf(index) === -1;
     29   });
     30 }
     31 
     32 
     33 function compileMap(/* lists... */) {
     34   var result = {
     35         scalar: {},
     36         sequence: {},
     37         mapping: {},
     38         fallback: {}
     39       }, index, length;
     40 
     41   function collectType(type) {
     42     result[type.kind][type.tag] = result['fallback'][type.tag] = type;
     43   }
     44 
     45   for (index = 0, length = arguments.length; index < length; index += 1) {
     46     arguments[index].forEach(collectType);
     47   }
     48   return result;
     49 }
     50 
     51 
     52 function Schema(definition) {
     53   this.include  = definition.include  || [];
     54   this.implicit = definition.implicit || [];
     55   this.explicit = definition.explicit || [];
     56 
     57   this.implicit.forEach(function (type) {
     58     if (type.loadKind && type.loadKind !== 'scalar') {
     59       throw new YAMLException('There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported.');
     60     }
     61   });
     62 
     63   this.compiledImplicit = compileList(this, 'implicit', []);
     64   this.compiledExplicit = compileList(this, 'explicit', []);
     65   this.compiledTypeMap  = compileMap(this.compiledImplicit, this.compiledExplicit);
     66 }
     67 
     68 
     69 Schema.DEFAULT = null;
     70 
     71 
     72 Schema.create = function createSchema() {
     73   var schemas, types;
     74 
     75   switch (arguments.length) {
     76     case 1:
     77       schemas = Schema.DEFAULT;
     78       types = arguments[0];
     79       break;
     80 
     81     case 2:
     82       schemas = arguments[0];
     83       types = arguments[1];
     84       break;
     85 
     86     default:
     87       throw new YAMLException('Wrong number of arguments for Schema.create function');
     88   }
     89 
     90   schemas = common.toArray(schemas);
     91   types = common.toArray(types);
     92 
     93   if (!schemas.every(function (schema) { return schema instanceof Schema; })) {
     94     throw new YAMLException('Specified list of super schemas (or a single Schema object) contains a non-Schema object.');
     95   }
     96 
     97   if (!types.every(function (type) { return type instanceof Type; })) {
     98     throw new YAMLException('Specified list of YAML types (or a single Type object) contains a non-Type object.');
     99   }
    100 
    101   return new Schema({
    102     include: schemas,
    103     explicit: types
    104   });
    105 };
    106 
    107 
    108 module.exports = Schema;