Idea: add a .forEach method to each ShellString
See original GitHub issueThis 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:
- Created 8 years ago
- Comments:13 (12 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Bash of course treats the actual separator as
IFS+
(one or more of any combination of values in the IFS set)Here’s what I’m thinking some JS-equivalent behavior would look like:
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)
My $0.02: