git-off

git off handles large files in git repos
git clone https://noulin.net/git/git-off.git
Log | Files | Refs | README

translator.js (2113B)


      1 var util = require('../core').util;
      2 var convert = require('./converter');
      3 
      4 var Translator = function(options) {
      5   options = options || {};
      6   this.attrValue = options.attrValue;
      7 };
      8 
      9 Translator.prototype.translateInput = function(value, shape) {
     10   this.mode = 'input';
     11   return this.translate(value, shape);
     12 };
     13 
     14 Translator.prototype.translateOutput = function(value, shape) {
     15   this.mode = 'output';
     16   return this.translate(value, shape);
     17 };
     18 
     19 Translator.prototype.translate = function(value, shape) {
     20   var self = this;
     21   if (!shape || value === undefined) return undefined;
     22 
     23   if (shape.shape === self.attrValue) {
     24     return convert[self.mode](value);
     25   }
     26   switch (shape.type) {
     27     case 'structure': return self.translateStructure(value, shape);
     28     case 'map': return self.translateMap(value, shape);
     29     case 'list': return self.translateList(value, shape);
     30     default: return self.translateScalar(value, shape);
     31   }
     32 };
     33 
     34 Translator.prototype.translateStructure = function(structure, shape) {
     35   var self = this;
     36   if (structure == null) return undefined;
     37 
     38   var struct = {};
     39   util.each(structure, function(name, value) {
     40     var memberShape = shape.members[name];
     41     if (memberShape) {
     42       var result = self.translate(value, memberShape);
     43       if (result !== undefined) struct[name] = result;
     44     }
     45   });
     46   return struct;
     47 };
     48 
     49 Translator.prototype.translateList = function(list, shape) {
     50   var self = this;
     51   if (list == null) return undefined;
     52 
     53   var out = [];
     54   util.arrayEach(list, function(value) {
     55     var result = self.translate(value, shape.member);
     56     if (result === undefined) out.push(null);
     57     else out.push(result);
     58   });
     59   return out;
     60 };
     61 
     62 Translator.prototype.translateMap = function(map, shape) {
     63   var self = this;
     64   if (map == null) return undefined;
     65 
     66   var out = {};
     67   util.each(map, function(key, value) {
     68     var result = self.translate(value, shape.value);
     69     if (result === undefined) out[key] = null;
     70     else out[key] = result;
     71   });
     72   return out;
     73 };
     74 
     75 Translator.prototype.translateScalar = function(value, shape) {
     76   return shape.toType(value);
     77 };
     78 
     79 module.exports = Translator;