packages/idyll-ast/src/converters/index.js
function inverseConvertHelper(arrayElement, id) {
  let elementJson = new Object();
  elementJson.id = ++id;

  if (typeof arrayElement === 'string') {
    elementJson.type = 'textnode';
    elementJson.value = arrayElement;
  } else if (['var', 'derived', 'data', 'meta'].indexOf(arrayElement[0]) > -1) {
    elementJson.type = arrayElement[0];
    elementJson.properties = {};
    arrayElement[1].forEach(property => {
      elementJson.properties[property[0]] = {
        type: property[1][0],
        value: property[1][1]
      };
    });
  } else {
    elementJson.type = 'component';
    elementJson.name = arrayElement[0];
    if (arrayElement[1].length !== 0) {
      elementJson.properties = {};
      arrayElement[1].forEach(property => {
        elementJson.properties[property[0]] = {
          type: property[1][0],
          value: property[1][1]
        };
      });
    }
    if (arrayElement[2]) {
      let children = [];
      arrayElement[2].forEach(element => {
        let childData = inverseConvertHelper(element, id);
        id = childData.id;
        children.push(childData.data);
      });
      elementJson.children = children;
    }
  }
  let result = new Object();
  result.id = id;
  result.data = elementJson;
  return result;
}
function inverseConvertHelper(arrayElement, id) {
  let elementJson = new Object();
  elementJson.id = ++id;

  if (typeof arrayElement === 'string') {
    elementJson.type = 'textnode';
    elementJson.value = arrayElement;
  } else if (['var', 'derived', 'data', 'meta'].indexOf(arrayElement[0]) > -1) {
    elementJson.type = arrayElement[0];
    elementJson.properties = {};
    arrayElement[1].forEach(property => {
      elementJson.properties[property[0]] = {
        type: property[1][0],
        value: property[1][1]
      };
    });
  } else {
    elementJson.type = 'component';
    elementJson.name = arrayElement[0];
    if (arrayElement[1].length !== 0) {
      elementJson.properties = {};
      arrayElement[1].forEach(property => {
        elementJson.properties[property[0]] = {
          type: property[1][0],
          value: property[1][1]
        };
      });
    }
    if (arrayElement[2]) {
      let children = [];
      arrayElement[2].forEach(element => {
        let childData = inverseConvertHelper(element, id);
        id = childData.id;
        children.push(childData.data);
      });
      elementJson.children = children;
    }
  }
  let result = new Object();
  result.id = id;
  result.data = elementJson;
  return result;
}
function inverseConvertHelper ( arrayElement , id ) { let elementJson = new Object ( ) ; elementJson . id = ++ id ; if ( typeof arrayElement === 'string' ) { elementJson . type = 'textnode' ; elementJson . value = arrayElement ; } else if ( [ 'var' , 'derived' , 'data' , 'meta' ] . indexOf ( arrayElement [ 0 ] ) > - 1 ) { elementJson . type = arrayElement [ 0 ] ; elementJson . properties = { } ; arrayElement [ 1 ] . forEach ( property => { elementJson . properties [ property [ 0 ] ] = { type : property [ 1 ] [ 0 ] , value : property [ 1 ] [ 1 ] } ; } ) ; } else { elementJson . type = 'component' ; elementJson . name = arrayElement [ 0 ] ; if ( arrayElement [ 1 ] . length !== 0 ) { elementJson . properties = { } ; arrayElement [ 1 ] . forEach ( property => { elementJson . properties [ property [ 0 ] ] = { type : property [ 1 ] [ 0 ] , value : property [ 1 ] [ 1 ] } ; } ) ; } if ( arrayElement [ 2 ] ) { let children = [ ] ; arrayElement [ 2 ] . forEach ( element => { let childData = inverseConvertHelper ( element , id ) ; id = childData . id ; children . push ( childData . data ) ; } ) ; elementJson . children = children ; } } let result = new Object ( ) ; result . id = id ; result . data = elementJson ; return result ; }
Helper function for inverseConvert
@param {*} arrayElement
@return JSON representation of the corresponding arrayElement
Helper function for inverseConvert