Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make resolved ProcessPromise iterable #1088

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Pulkit1822
Copy link

@Pulkit1822 Pulkit1822 commented Jan 29, 2025

Added a synchronous iterator to the ProcessOutput class to allow iterating over stdout lines.

  • src/core.ts

    • Added a synchronous iterator method [Symbol.iterator]() to the ProcessOutput class.
    • Split the stdout string into lines using \r?\n to handle both Unix and Windows line endings.
    • Returned the iterator of the split array.
  • test/core.test.js

    • Added a test case to verify the synchronous iterator method in the ProcessOutput class.
    • Also ensured that the test case covers both Unix and Windows line endings.

Add a synchronous iterator to the `ProcessOutput` class to allow iterating over stdout lines.

* **src/core.ts**
  - Add a synchronous iterator method `[Symbol.iterator]()` to the `ProcessOutput` class.
  - Split the `stdout` string into lines using `\r?\n` to handle both Unix and Windows line endings.
  - Return the iterator of the split array.

* **test/core.test.js**
  - Add a test case to verify the synchronous iterator method in the `ProcessOutput` class.
  - Ensure the test case covers both Unix and Windows line endings.
@antongolub
Copy link
Collaborator

antongolub commented Jan 29, 2025

@Pulkit1822, not so fast)

  1. ProcessOutput has already lines() extractor, it should be used here.
  2. ProcessPromise, if resolved, should apply generator around its internal data.
{
  [Symbol.iterator]: function* () {
    yield* this._output.lines()
  }
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol

@Pulkit1822
Copy link
Author

@antongolub, As per suggestions, I’ve added the iterable protocol in core.ts with this.lines()[Symbol.iterator](), so ProcessOutput can be used in for...of loops. The test in core.test.js checks that ProcessOutput gives the correct lines when spread with [ ... ], making sure iterable operations work.

Is this the right way, or should I use:

*[Symbol.iterator](): Iterator<string> {
  yield* this.lines();
}

This would make it a generator function, which seems clearer. Let me know if you have any advice or suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants