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

feat: add the makeObject option for Stringer #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vanyauhalin
Copy link

Hi Eugene! I just wanted to say that your library is awesome.

I've a request. Would it be possible for you to add the makeObject option for the Stringers since we already have the makeArray one? I've a situation where I need to process an array of elements into a map.

Thanks in advance!

[
  { "id": "first" },
  // ...
  { "id": "fifth" }
]
{
  "first": 0,
  // ...
  "fifth": 4
}
let i = 0

new Chain([
  createReadStream(from),
  parser(),
  new StreamArray(),
  new Transform({
    objectMode: true,
    transform(ch, _, cb) {
      pushPair.call(this, ch.value.id, i)
      i += 1
      cb(null)
    }
  }),
  new Transform({
    objectMode: true,
    transform(ch, enc, cb) {
      this.push({ name: "startObject" })
      this._transform = transformPassThrough
      return this._transform(ch, enc, cb)
    },
    flush(cb) {
      if (this._transform === transformPassThrough) {
        this.push({ name: "endObject" })
      }
      cb(null)
    }
  }),
  new Stringer(),
  createWriteStream(to)
])

function transformPassThrough(ch, enc, cb) {
  this.push(ch, enc)
  cb(null)
}

@uhop uhop self-assigned this Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants