git-off

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

builder.js (1519B)


      1 var util = require('../util');
      2 
      3 function JsonBuilder() { }
      4 
      5 JsonBuilder.prototype.build = function(value, shape) {
      6   return JSON.stringify(translate(value, shape));
      7 };
      8 
      9 function translate(value, shape) {
     10   if (!shape || value === undefined || value === null) return undefined;
     11 
     12   switch (shape.type) {
     13     case 'structure': return translateStructure(value, shape);
     14     case 'map': return translateMap(value, shape);
     15     case 'list': return translateList(value, shape);
     16     default: return translateScalar(value, shape);
     17   }
     18 }
     19 
     20 function translateStructure(structure, shape) {
     21   var struct = {};
     22   util.each(structure, function(name, value) {
     23     var memberShape = shape.members[name];
     24     if (memberShape) {
     25       if (memberShape.location !== 'body') return;
     26       var locationName = memberShape.isLocationName ? memberShape.name : name;
     27       var result = translate(value, memberShape);
     28       if (result !== undefined) struct[locationName] = result;
     29     }
     30   });
     31   return struct;
     32 }
     33 
     34 function translateList(list, shape) {
     35   var out = [];
     36   util.arrayEach(list, function(value) {
     37     var result = translate(value, shape.member);
     38     if (result !== undefined) out.push(result);
     39   });
     40   return out;
     41 }
     42 
     43 function translateMap(map, shape) {
     44   var out = {};
     45   util.each(map, function(key, value) {
     46     var result = translate(value, shape.value);
     47     if (result !== undefined) out[key] = result;
     48   });
     49   return out;
     50 }
     51 
     52 function translateScalar(value, shape) {
     53   return shape.toWireFormat(value);
     54 }
     55 
     56 module.exports = JsonBuilder;