packages/idyll-ast/src/converters/index.js
function convertHelper(jsonElement) {
  let elementArray = [];
  if (jsonElement.type === 'textnode') {
    return jsonElement.value;
  } else if (jsonElement.type === 'var' || jsonElement.type === 'derived') {
    elementArray = [jsonElement.type];
    elementArray.push([
      ['name', ['value', jsonElement.name]],
      ['value', ['value', jsonElement.value]]
    ]);
    elementArray.push([]);
  } else if (jsonElement.type === 'data') {
    elementArray = ['data'];
    elementArray.push([
      ['name', ['value', jsonElement.name]],
      ['source', ['value', jsonElement.source]]
    ]);
    elementArray.push([]);
  } else {
    elementArray.push(jsonElement.name);
    let propertiesArray = [];
    if ('properties' in jsonElement) {
      Object.keys(jsonElement.properties).forEach(key => {
        let propertyArray = [key];
        propertyArray.push([
          jsonElement.properties[key].type,
          jsonElement.properties[key].value
        ]);
        propertiesArray.push(propertyArray);
      });
    }
    elementArray.push(propertiesArray);
    if ('children' in jsonElement) {
      let childArray = [];
      jsonElement.children.forEach(children => {
        childArray.push(convertHelper(children));
      });
      elementArray.push(childArray);
    }
  }
  return elementArray;
}
function convertHelper(jsonElement) {
  let elementArray = [];
  if (jsonElement.type === 'textnode') {
    return jsonElement.value;
  } else if (jsonElement.type === 'var' || jsonElement.type === 'derived') {
    elementArray = [jsonElement.type];
    elementArray.push([
      ['name', ['value', jsonElement.name]],
      ['value', ['value', jsonElement.value]]
    ]);
    elementArray.push([]);
  } else if (jsonElement.type === 'data') {
    elementArray = ['data'];
    elementArray.push([
      ['name', ['value', jsonElement.name]],
      ['source', ['value', jsonElement.source]]
    ]);
    elementArray.push([]);
  } else {
    elementArray.push(jsonElement.name);
    let propertiesArray = [];
    if ('properties' in jsonElement) {
      Object.keys(jsonElement.properties).forEach(key => {
        let propertyArray = [key];
        propertyArray.push([
          jsonElement.properties[key].type,
          jsonElement.properties[key].value
        ]);
        propertiesArray.push(propertyArray);
      });
    }
    elementArray.push(propertiesArray);
    if ('children' in jsonElement) {
      let childArray = [];
      jsonElement.children.forEach(children => {
        childArray.push(convertHelper(children));
      });
      elementArray.push(childArray);
    }
  }
  return elementArray;
}
function convertHelper ( jsonElement ) { let elementArray = [ ] ; if ( jsonElement . type === 'textnode' ) { return jsonElement . value ; } else if ( jsonElement . type === 'var' || jsonElement . type === 'derived' ) { elementArray = [ jsonElement . type ] ; elementArray . push ( [ [ 'name' , [ 'value' , jsonElement . name ] ] , [ 'value' , [ 'value' , jsonElement . value ] ] ] ) ; elementArray . push ( [ ] ) ; } else if ( jsonElement . type === 'data' ) { elementArray = [ 'data' ] ; elementArray . push ( [ [ 'name' , [ 'value' , jsonElement . name ] ] , [ 'source' , [ 'value' , jsonElement . source ] ] ] ) ; elementArray . push ( [ ] ) ; } else { elementArray . push ( jsonElement . name ) ; let propertiesArray = [ ] ; if ( 'properties' in jsonElement ) { Object . keys ( jsonElement . properties ) . forEach ( key => { let propertyArray = [ key ] ; propertyArray . push ( [ jsonElement . properties [ key ] . type , jsonElement . properties [ key ] . value ] ) ; propertiesArray . push ( propertyArray ) ; } ) ; } elementArray . push ( propertiesArray ) ; if ( 'children' in jsonElement ) { let childArray = [ ] ; jsonElement . children . forEach ( children => { childArray . push ( convertHelper ( children ) ) ; } ) ; elementArray . push ( childArray ) ; } } return elementArray ; }
Helper function for convert
@param {*} jsonElement
@return array representation of the corresponding jsonElement
Helper function for convert