SplitNode.js 856 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Node from '../core/Node.js';
  2. class SplitNode extends Node {
  3. constructor( node, components = 'x' ) {
  4. super();
  5. this.node = node;
  6. this.components = components;
  7. }
  8. getNodeType( builder ) {
  9. return builder.getTypeFromLength( this.components.length );
  10. }
  11. generate( builder ) {
  12. const node = this.node;
  13. const nodeTypeLength = builder.getTypeLength( node.getNodeType( builder ) );
  14. if ( nodeTypeLength > 1 ) {
  15. const components = this.components;
  16. let type = null;
  17. if ( components.length >= nodeTypeLength ) {
  18. // need expand the input node
  19. type = this.getNodeType( builder );
  20. }
  21. const nodeSnippet = node.build( builder, type );
  22. return `${nodeSnippet}.${this.components}`;
  23. } else {
  24. // ignore components if node is a float
  25. return node.build( builder );
  26. }
  27. }
  28. }
  29. export default SplitNode;