Coder Social home page Coder Social logo

Iterative writes about code-block-writer HOT 4 OPEN

dsherret avatar dsherret commented on April 28, 2024
Iterative writes

from code-block-writer.

Comments (4)

lazarljubenovic avatar lazarljubenovic commented on April 28, 2024 1

Sounds good! I'd just change the naming to value, key and iterable/iteratee, to cover each of three big iteratees in JavaScript (Array#forEach, Map#forEach and Set#forEach). This will cover custom iterables, as well.

from code-block-writer.

dsherret avatar dsherret commented on April 28, 2024

I like this (and the other issue you opened), thanks!

It could probably be similar to the signature of Array.prototype.forEach. So currentValue[, index[, array].

from code-block-writer.

lazarljubenovic avatar lazarljubenovic commented on April 28, 2024

I looked around a bit in my code to see where I could apply this and there are only a few exceptions to the rule I've found: I always indent the block where I iterate.

The example I've given is not how you'd probably do it in a real project... Instead, it would be

const indexes = [5, 9, 10, 15, 17]

writer
  .writeLine(`return [`)
  .indentBlock(() => {
      writer.iterateWriteLine(indexes, index => `this.array[${index}],`)
  })
  .writeLine(`]`)

The chain is broken anyway and for such a short line you'd pretty much inline it anyway:

const indexes = [5, 9, 10, 15, 17]

writer
  .writeLine(`return [`)
  .indentBlock(() => {
      indexes.forEach(index => writer.writeLine(`this.array[${index}],`)
  })
  .writeLine(`]`)

🤔


PS. It's funny how iterate and forEach have exactly the same amount of characters. 😂

from code-block-writer.

dsherret avatar dsherret commented on April 28, 2024

Hmmm... good point. The only alternative I can think of is adding a .withEach or something like it:

writer
  .writeLine(`return [`)
  .withEach(indexes, index => writer.indent().writeLine(`this.array[${index}],`))
  .writeLine(`]`)

from code-block-writer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.