1. /**
  2. * Wraps string manipulation logic with a dry-run mode.
  3. *
  4. * @param {function} originalFunction The original string manipulation function.
  5. * @param {boolean} dryRun Whether to run in dry-run mode. If true, logs the actions without executing them.
  6. * @returns {function} A wrapped function that can be called with string input.
  7. */
  8. function wrapStringLogic(originalFunction, dryRun = false) {
  9. return function(inputString) {
  10. if (dryRun) {
  11. console.log("Dry Run Mode: Logging actions...");
  12. }
  13. let result;
  14. try {
  15. const originalResult = originalFunction(inputString);
  16. result = originalResult;
  17. if (dryRun) {
  18. console.log("Dry Run Mode: Result would be:", result);
  19. }
  20. } catch (error) {
  21. if (dryRun) {
  22. console.error("Dry Run Mode: Error would occur:", error.message);
  23. }
  24. throw error; // Re-throw the error if not in dry-run
  25. }
  26. return result;
  27. };
  28. }

Add your comment