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

Improve default value of objectMode option #866

Closed
ehmicky opened this issue Feb 27, 2024 · 0 comments · Fixed by #867
Closed

Improve default value of objectMode option #866

ehmicky opened this issue Feb 27, 2024 · 0 comments · Fixed by #867

Comments

@ehmicky
Copy link
Collaborator

ehmicky commented Feb 27, 2024

Transforms can return objects by setting objectMode: true.

const transform = function * (line) {
	yield JSON.parse(line);
};

const {stdout} = await execa('./jsonlines-output.js', {stdout: {transform, objectMode: true}});
for (const data of stdout) {
	console.log(stdout); // {...}
}

The default value for objectMode is false. However, when multiple transforms are used, and the previous transform uses objectMode: true, the next one should default to true instead. For example:

await execa('./jsonlines-output.js', {stdout: [
  {transform, objectMode: true}, 
  otherTransform,
]});

otherTransform should be in objectMode since the previous does. It can still revert to non-objectMode by doing:

await execa('./jsonlines-output.js', {stdout: [
  {transform, objectMode: true}, 
  {transform: otherTransform, objectMode: false},
]});

This enables creating re-usable transforms that work regardless of whether the previous transform is in objectMode or not. We already use internally some of those objectMode-agnostic transforms, which work regardless of whether chunks are objects or not, and that should keep the objectMode as is. Without the above behavior, every transform must choose either true or false for objectMode and cannot be truly agnostic to it.

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 a pull request may close this issue.

1 participant