shubhamgoyal

Solution.js

Sep 3rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. import { h, Component } from "preact";
  2. import SolutionStep from "./SolutionStep";
  3. import { REGEX_NO_NEW_LINE_CHARS } from "../utils/utils";
  4. import {
  5. RGX_SPLIT_STEPS,
  6. } from './../utils/regex';
  7. import SolutionSubpart from "./SolutionSubpart";
  8. import {
  9. separateConfigStringIntoFactStrings,
  10. extractPredicateNameAndArgumentsFromFactString,
  11. extraString,
  12. } from "../utils/utils";
  13. import { getTablesFromFactStrings, getListOfTableComponents } from "./Config";
  14. import { getTableInfoFromFactString } from "../utils/tableParser";
  15. import { extractEvaluationExpression } from "../utils/evaluation-expression";
  16. import LHS_RHS from "./LHS_RHS/LHS_RHS";
  17. import Table from "./Table";
  18. import {formatGraphData} from './../utils/graph';
  19. import Graph from './../components/Geometry/Graph';
  20. import NumberLine from './../components/Geometry/Numberline';
  21. class Solution extends Component {
  22. splitSolutionStringIntoSteps(rawString) {
  23. const lines = [];
  24. const arrayOfLines = rawString.match(REGEX_NO_NEW_LINE_CHARS);
  25. if (!arrayOfLines || !arrayOfLines.length) {
  26. return [];
  27. }
  28. lines.push(arrayOfLines[0] + "\n");
  29. let index = 0;
  30. for (let i = 1; i < arrayOfLines.length; i++) {
  31. const lineElement = arrayOfLines[i].replace(/^\s+|\s+$/g, "");
  32. if (lineElement.startsWith("=")) {
  33. lines[index] = lines[index] + lineElement + "\n";
  34. } else {
  35. lines[index] = lines[index].replace(/^\s+|\s+$/g, "");
  36. index++;
  37. lines[index] = lineElement;
  38. }
  39. }
  40. lines[index] = lines[index].replace(/^\s+|\s+$/g, "");
  41. return lines;
  42. }
  43. getSolutionStepComponents(rawStringSteps) {
  44. const { subpartIndexString } = this.props;
  45. return rawStringSteps.map((stepString, index) => {
  46. return (
  47. <SolutionStep
  48. stringWithLines={stepString}
  49. subpartIndexString={subpartIndexString}
  50. stepNumber={index + 1}
  51. />
  52. );
  53. });
  54. }
  55. getSolutionSubparts() {
  56. const { subparts } = this.props;
  57. if (!subparts) {
  58. return [];
  59. }
  60. return subparts.map((subpart, index) => (
  61. <SolutionSubpart index={index} text={subpart.solutionString} />
  62. ));
  63. }
  64. getSolutionStepFromFactString(factString) {
  65. const {
  66. predicate,
  67. argumentsString
  68. } = extractPredicateNameAndArgumentsFromFactString(factString);
  69. switch(predicate){
  70. case "matrix_solution":
  71. const rawStringSteps = this.splitSolutionStringIntoSteps(
  72. argumentsString
  73. );
  74. const solutionSteps = this.getSolutionStepComponents(
  75. rawStringSteps
  76. );
  77. return solutionSteps;
  78. case "table":
  79. const tableInfo = getTableInfoFromFactString(factString);
  80. return <Table table={tableInfo} />;
  81. case "evaluation_expression":
  82. const lhs_rhs = extractEvaluationExpression(factString);
  83. return <LHS_RHS lhs_rhs={lhs_rhs} />;
  84. case "string":
  85. const str = extraString(`string(${argumentsString})`)
  86. return <span>{str}</span>
  87. case "graph":
  88. const graphSteps = argumentsString.split(RGX_SPLIT_STEPS);
  89. const formattedData = formatGraphData(graphSteps);
  90. return <Graph data={formattedData.data}/>
  91. case "numberline":
  92. return <NumberLine argumentsString={argumentsString}/>
  93. case "sequence_difference_underneath":
  94. return <SequenceWithDifferenceUnderneath argumentsString = {argumentsString} />
  95. default :
  96. return <div />;
  97. }
  98. }
  99. splitSolutionStringIntoParts() {
  100. const { rawString } = this.props;
  101. if (!rawString) {
  102. return "";
  103. }
  104. const factStrings = separateConfigStringIntoFactStrings(rawString);
  105. return factStrings.map(this.getSolutionStepFromFactString.bind(this));
  106. }
  107. render() {
  108. const solutionParts = this.splitSolutionStringIntoParts();
  109. return (
  110. <div>
  111. <div className="solution">{solutionParts}</div>
  112. <div>{this.getSolutionSubparts()}</div>
  113. </div>
  114. );
  115. }
  116. }
  117.  
  118. export default Solution;
Add Comment
Please, Sign In to add comment