putout v23.1.0
Inception of Array.entries()
Along the riverbank under the trees,
I discover footprints.
Even under the fragrant grass,
I see his prints.
Deep in remote mountains they are found.
These traces can no more be hidden
than one's nose, looking heavenward.
(c) Zen Ten Bulls
Todays 🐊Putout minor release notes will be about Array.entries, that can help you to get rid of for loops even when you need to use index. In good old days we used to use:
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
console.log(i, element);
}But now we are using for-of most of the time, because it's simpler and does only what's needed:
for (const element of elements) {
console.log(i, element); // <-- we have no i 🍄
}But hold up, what with i variable? Where we should get it from?
Worry not 😄, array.entries() to the rescue!
for (const [i, element] of elements.entries()) {
console.log(i, element);
}convert-for-to-for-of
Of course such transformation already waiting for you in convert-for-to-for-of 🎈 .
remove-useless-array-entries
But what if you used array.entries() for some time with index but then stoped using index and your code became looking like this:
for (const [, element] of elements.entries()) {
console.log(element);
}Worry not! remove-useless-array-entries will convert code to usual for-of:
for (const element of elements) {
console.log(element);
}Guys have fun and always use for-of (with the exception of hot code that requires a lot of speed ☝️!) 🦉.
🐞 fix
- (@putout/plugin-convert-for-to-for-of) entries: count i references (should be more then 3)
🔥 feature
- (@putout/plugin-remove-useless-array-entries) add
- (@putout/plugin-convert-for-to-for-of) length: add support of destructuring
- (@putout/plugin-convert-for-to-for-of) entries: add support of destructuring
- (@putout/plugin-convert-for-to-for-of) add support of entries with declared n
- (@putout/plugin-convert-for-to-for-of) add support of entries
- (eslint-plugin-putout) no-unresolved: add support of export declarations
- (@putout/plugin-remove-useless-escape) set RegExp raw
- (@putout/plugin-remove-useless-escape) improve support of handling "," in RegExp
- (@putout/plugin-regexp) remove-useless-group: add support of Alternative
