flowGraphPathConverterComponent.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { RichTypeFlowGraphInteger } from "./flowGraphRichTypes.js";
  2. const pathHasTemplatesRegex = new RegExp(/\{(\w+)\}/g);
  3. /**
  4. * @experimental
  5. * A component that converts a path to an object accessor.
  6. */
  7. export class FlowGraphPathConverterComponent {
  8. constructor(path, ownerBlock) {
  9. this.path = path;
  10. this.ownerBlock = ownerBlock;
  11. /**
  12. * The templated inputs for the provided path.
  13. */
  14. this.templatedInputs = [];
  15. let match = pathHasTemplatesRegex.exec(path);
  16. while (match) {
  17. const [, matchGroup] = match;
  18. this.templatedInputs.push(ownerBlock.registerDataInput(matchGroup, RichTypeFlowGraphInteger));
  19. match = pathHasTemplatesRegex.exec(path);
  20. }
  21. }
  22. getAccessor(pathConverter, context) {
  23. let finalPath = this.path;
  24. for (const templatedInput of this.templatedInputs) {
  25. const valueToReplace = templatedInput.getValue(context).value;
  26. finalPath = finalPath.replace(`{${templatedInput.name}}`, valueToReplace.toString());
  27. }
  28. return pathConverter.convert(finalPath);
  29. }
  30. }
  31. //# sourceMappingURL=flowGraphPathConverterComponent.js.map