CodeSerializer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @author Kai Salmen / https://kaisalmen.de
  3. * Development repository: https://github.com/kaisalmen/WWOBJLoader
  4. */
  5. const CodeSerializer = {
  6. /**
  7. *
  8. * @param fullName
  9. * @param object
  10. * @returns {string}
  11. */
  12. serializeObject: function ( fullName, object ) {
  13. let objectString = fullName + ' = {\n\n';
  14. let part;
  15. for ( let name in object ) {
  16. part = object[ name ];
  17. if ( typeof( part ) === 'string' || part instanceof String ) {
  18. part = part.replace( '\n', '\\n' );
  19. part = part.replace( '\r', '\\r' );
  20. objectString += '\t' + name + ': "' + part + '",\n';
  21. } else if ( part instanceof Array ) {
  22. objectString += '\t' + name + ': [' + part + '],\n';
  23. } else if ( typeof part === 'object' ) {
  24. // TODO: Short-cut for now. Recursion required?
  25. objectString += '\t' + name + ': {},\n';
  26. } else {
  27. objectString += '\t' + name + ': ' + part + ',\n';
  28. }
  29. }
  30. objectString += '}\n\n';
  31. return objectString;
  32. },
  33. /**
  34. *
  35. * @param fullName
  36. * @param object
  37. * @param basePrototypeName
  38. * @param ignoreFunctions
  39. * @returns {string}
  40. */
  41. serializeClass: function ( fullName, object, constructorName, basePrototypeName, ignoreFunctions, includeFunctions, overrideFunctions ) {
  42. let valueString, objectPart, constructorString, i, funcOverride;
  43. let prototypeFunctions = [];
  44. let objectProperties = [];
  45. let objectFunctions = [];
  46. let isExtended = ( basePrototypeName !== null && basePrototypeName !== undefined );
  47. if ( ! Array.isArray( ignoreFunctions ) ) ignoreFunctions = [];
  48. if ( ! Array.isArray( includeFunctions ) ) includeFunctions = null;
  49. if ( ! Array.isArray( overrideFunctions ) ) overrideFunctions = [];
  50. for ( let name in object.prototype ) {
  51. objectPart = object.prototype[ name ];
  52. valueString = objectPart.toString();
  53. if ( name === 'constructor' ) {
  54. constructorString = fullName + ' = ' + valueString + ';\n\n';
  55. } else if ( typeof objectPart === 'function' ) {
  56. if ( ignoreFunctions.indexOf( name ) < 0 && ( includeFunctions === null || includeFunctions.indexOf( name ) >= 0 ) ) {
  57. funcOverride = overrideFunctions[ name ];
  58. if ( funcOverride && funcOverride.fullName === fullName + '.prototype.' + name ) {
  59. valueString = funcOverride.code;
  60. }
  61. if ( isExtended ) {
  62. prototypeFunctions.push( fullName + '.prototype.' + name + ' = ' + valueString + ';\n\n' );
  63. } else {
  64. prototypeFunctions.push( '\t' + name + ': ' + valueString + ',\n\n' );
  65. }
  66. }
  67. }
  68. }
  69. for ( let name in object ) {
  70. objectPart = object[ name ];
  71. if ( typeof objectPart === 'function' ) {
  72. if ( ignoreFunctions.indexOf( name ) < 0 && ( includeFunctions === null || includeFunctions.indexOf( name ) >= 0 ) ) {
  73. funcOverride = overrideFunctions[ name ];
  74. if ( funcOverride && funcOverride.fullName === fullName + '.' + name ) {
  75. valueString = funcOverride.code;
  76. } else {
  77. valueString = objectPart.toString();
  78. }
  79. objectFunctions.push( fullName + '.' + name + ' = ' + valueString + ';\n\n' );
  80. }
  81. } else {
  82. if ( typeof( objectPart ) === 'string' || objectPart instanceof String) {
  83. valueString = '\"' + objectPart.toString() + '\"';
  84. } else if ( typeof objectPart === 'object' ) {
  85. // TODO: Short-cut for now. Recursion required?
  86. valueString = "{}";
  87. } else {
  88. valueString = objectPart;
  89. }
  90. objectProperties.push( fullName + '.' + name + ' = ' + valueString + ';\n' );
  91. }
  92. }
  93. if ( ( constructorString === undefined || constructorString === null ) && typeof object.prototype.constructor === 'function' ) {
  94. constructorString = fullName + ' = ' + object.prototype.constructor.toString().replace( constructorName, '' );
  95. }
  96. let objectString = constructorString + '\n\n';
  97. if ( isExtended ) {
  98. objectString += fullName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
  99. }
  100. objectString += fullName + '.prototype.constructor = ' + fullName + ';\n';
  101. objectString += '\n\n';
  102. for ( i = 0; i < objectProperties.length; i ++ ) objectString += objectProperties[ i ];
  103. objectString += '\n\n';
  104. for ( i = 0; i < objectFunctions.length; i ++ ) objectString += objectFunctions[ i ];
  105. objectString += '\n\n';
  106. if ( isExtended ) {
  107. for ( i = 0; i < prototypeFunctions.length; i ++ ) objectString += prototypeFunctions[ i ];
  108. } else {
  109. objectString += fullName + '.prototype = {\n\n';
  110. for ( i = 0; i < prototypeFunctions.length; i ++ ) objectString += prototypeFunctions[ i ];
  111. objectString += '\n};';
  112. }
  113. objectString += '\n\n';
  114. return objectString;
  115. },
  116. };
  117. export { CodeSerializer }