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

Opts object being modified by parsing method #317

Closed
gilles-crealp opened this issue Aug 6, 2018 · 8 comments
Closed

Opts object being modified by parsing method #317

gilles-crealp opened this issue Aug 6, 2018 · 8 comments
Assignees
Labels

Comments

@gilles-crealp
Copy link

gilles-crealp commented Aug 6, 2018

let Json2csvParser = require('json2csv').Parser
let json2csvParserOptions = {
  header: false
}

let json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'b': 2
  },
  {
    'a': 3,
    'b': 4
  }
]))
// Output:
// 1,2
// 3,4

console.log()

json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'x': 2
  },
  {
    'a': 3,
    'x': 4
  }
]))
// Output:
// 1,
// 3,
// Should be:
// 1,2
// 3,4

Versions:

  • json2csv 4.2.0
  • node 10.1.0
@gilles-crealp
Copy link
Author

gilles-crealp commented Aug 6, 2018

Shouldn't be the same if I don't define fields and parse multiple different data? So I can define the options (except fields) only once.

let Json2csvParser = require('json2csv').Parser
let json2csv = new Json2csvParser({
  header: false
})

console.log(json2csv.parse([
  {
    "a": 1,
    "b": 2
  },
  {
    "a": 3,
    "b": 4
  }
]))
// Output:
// 1,2
// 3,4

console.log(json2csv.parse([
  {
    "a": 1,
    "x": 2
  },
  {
    "a": 3,
    "x": 4
  }
]))
// Output:
// 1,
// 3,
// Should be?
// 1,2
// 3,4

@juanjoDiaz
Copy link
Collaborator

It's a bug indeed!! I'll fix it asap. Thanks for reporting with such a clear code sample :)

@juanjoDiaz juanjoDiaz added the bug label Aug 6, 2018
@juanjoDiaz juanjoDiaz self-assigned this Aug 6, 2018
@gilles-crealp gilles-crealp changed the title New parser don't reset automatic fields selection New parser don't reset automatic fields selection when header: false Aug 6, 2018
@gilles-crealp
Copy link
Author

gilles-crealp commented Aug 6, 2018

Not so good: I didn't figure out that the problem was when you use header: false only. ;)

I've change the sample code.

@gilles-crealp
Copy link
Author

And you need to have the same object for the options!

Work:

let Json2csvParser = require('json2csv').Parser

let json2csv = new Json2csvParser({
  header: false
})
console.log(json2csv.parse([
  {
    'a': 1,
    'b': 2
  },
  {
    'a': 3,
    'b': 4
  }
]))
// Output:
// 1,2
// 3,4

console.log()

json2csv = new Json2csvParser({
  header: false
})
console.log(json2csv.parse([
  {
    'a': 1,
    'x': 2
  },
  {
    'a': 3,
    'x': 4
  }
]))
// Output:
// 1,2
// 3,4

Do not work:

let Json2csvParser = require('json2csv').Parser
let json2csvParserOptions = {
  header: false
}

let json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'b': 2
  },
  {
    'a': 3,
    'b': 4
  }
]))
// Output:
// 1,2
// 3,4

console.log()

json2csv = new Json2csvParser(json2csvParserOptions)
console.log(json2csv.parse([
  {
    'a': 1,
    'x': 2
  },
  {
    'a': 3,
    'x': 4
  }
]))
// Output:
// 1,
// 3,

@juanjoDiaz
Copy link
Collaborator

Yeah. The problem is that running any parsing modified the options object.
Can't believe that no one noticed until now... (including myself).

Once again, thanks for reporting. Will be fixed whenever the PR get's merged.

@knownasilya
Copy link
Collaborator

Fix published in v4.2.1

@juanjoDiaz juanjoDiaz changed the title New parser don't reset automatic fields selection when header: false Opts object being modified by parsing method Aug 7, 2018
@gilles-crealp
Copy link
Author

Thx for the reactivity!

@knownasilya
Copy link
Collaborator

Thanks for the detailed issue 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants