l0bsterssg

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

omap.js (1023B)


      1 'use strict';
      2 
      3 var Type = require('../type');
      4 
      5 var _hasOwnProperty = Object.prototype.hasOwnProperty;
      6 var _toString       = Object.prototype.toString;
      7 
      8 function resolveYamlOmap(data) {
      9   if (data === null) return true;
     10 
     11   var objectKeys = [], index, length, pair, pairKey, pairHasKey,
     12       object = data;
     13 
     14   for (index = 0, length = object.length; index < length; index += 1) {
     15     pair = object[index];
     16     pairHasKey = false;
     17 
     18     if (_toString.call(pair) !== '[object Object]') return false;
     19 
     20     for (pairKey in pair) {
     21       if (_hasOwnProperty.call(pair, pairKey)) {
     22         if (!pairHasKey) pairHasKey = true;
     23         else return false;
     24       }
     25     }
     26 
     27     if (!pairHasKey) return false;
     28 
     29     if (objectKeys.indexOf(pairKey) === -1) objectKeys.push(pairKey);
     30     else return false;
     31   }
     32 
     33   return true;
     34 }
     35 
     36 function constructYamlOmap(data) {
     37   return data !== null ? data : [];
     38 }
     39 
     40 module.exports = new Type('tag:yaml.org,2002:omap', {
     41   kind: 'sequence',
     42   resolve: resolveYamlOmap,
     43   construct: constructYamlOmap
     44 });