question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Idea: add a .forEach method to each ShellString

See original GitHub issue

This would be good to add, allowing users to iterate over the stdout from a command.

This can be done in Bash by running:

for k in $(cat package.json); do # wrap each word in <> brackets and print on a new line
  echo "<$k>"
done

# Or you can change IFS (internal field separator) to break on newlines
IFS='
'
for k in $(cat package.json); do # indent each line of the output
  echo "  $k"
done

A possible API would look like:

ls().forEach(function (file) { // this already works for ls()
  echo('This is a file: ' + file);
});

// something similar could work for cat(), grep(), etc.
cat('file1.txt').forEach(function (line) {
  echo('> ' + line); // put '> ' in front of each line
});

Alternatives would be to iterate over each word (delimited by whitespace) (this is similar to bash’s default behavior, which is sometimes useful).

We could also consider adding the ability to pass an option to .forEach that specifies how the output is split:

var wordCount = 0;
cat('file.txt').forEach(function (a) { wordCount++; }, {split: 'word'});
console.log(wordCount);

TODO:

  • Decide if this feature is worth adding
  • Decide if we should allow the {split: ...} option
  • Figure out a sensible default behavior when the {split: ...} option is not specified (I think “iterate over lines” and “iterate over words” are the two most sensible choices, but I’m open to other suggestions).

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
nfischercommented, Mar 26, 2016

I like the option at the end, just because it’s more consistent with things like exec().

Also, in the interest of making things more bash-like, it might be better to have things like:

// split is every value you want to count as a separator, similar to Bash's IFS var
cat('file.txt').forEach(function (a) { wordCount++; }, { split: ' ' });
cat('file.txt').forEach(function (a) { lineCount++; }, { split: '\n' });

Bash of course treats the actual separator as IFS+ (one or more of any combination of values in the IFS set)

# mixing \n, <SP>, and \t still count as 1 separator
$ for k in $(echo -e 'foo\n \tbar'); do echo "$k"; done
foo
bar

Here’s what I’m thinking some JS-equivalent behavior would look like:

> 'foo\n \tbar'.split(/[\n \t]+/) // we probably want behavior similar to this
[ 'foo', 'bar' ]
> 'foo\n \tbar'.split(/[\n \t]/) // this isn't what we would want by default
[ 'foo', '', '', 'bar' ]

Then we can add in options to get non-bash behavior that people may find useful (like the ability to iterate over empty lines, and stuff like that)

1reaction
ariporadcommented, Mar 26, 2016

My $0.02:

  1. Great Idea
  2. Yes, allow a split option
  3. Default to lines
Read more comments on GitHub >

github_iconTop Results From Across the Web

Code Inspection: For-loop can be converted into foreach-loop
The foreach loop supports any type that implements the IEnumerable<T> interface (in fact, any type that has the GetEnumerator method).
Read more >
IntelliJ IDEA suggests replacing for loops with foreach method ...
forEach () runs a conventional, int-based for-loop over the array containing the list elements, and calls the lambda once per iteration. There's ...
Read more >
Array.prototype.forEach() - JavaScript - MDN Web Docs
A function to execute for each element in the array. Its return value is discarded. The function is called with the following arguments:...
Read more >
Iterable forEach() method in Java with Examples
One of them is forEach Method in java.lang.Iterable Interface. Whenever we need to traverse over a collection we have to create an Iterator ......
Read more >
3 things you didn't know about the forEach loop in JS - Medium
The reason is that we are passing a callback function in our forEach function, which behaves just like a normal function and is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found