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

Stringify method remove Date Objects with empty object #63

Closed
seiya-git opened this issue Dec 22, 2018 · 2 comments
Closed

Stringify method remove Date Objects with empty object #63

seiya-git opened this issue Dec 22, 2018 · 2 comments

Comments

@seiya-git
Copy link

seiya-git commented Dec 22, 2018

Stringify method remove Date Objects with empty object

const yaml = require('yaml');
const myDate = new Date();
const yamlStr = yaml.stringify(myDate);
console.log(myDate);
console.log(yamlStr);

output:

2018-12-22T07:29:51.234Z
{}

@Hamper
Copy link

Hamper commented Dec 22, 2018

With timestamp tag also don't work:

const yaml = require('yaml')
const { timestamp } = require ('yaml/types/timestamp')

yaml.defaultOptions.tags = [timestamp]

let x = yaml.parse('2018-12-22 08:02:52')
// 2018-12-22T08:02:52.000Z
yaml.stringify(x)
// '{}\n'

@eemeli
Copy link
Owner

eemeli commented Dec 25, 2018

This turned out to be two different but overlapping issues.

The original posted by @seiya-git was due to the internally-called createNode function not looking for a toJSON method on the input value (as provided e.g. by Date). Adding support for this should also help in other cases, such as Immutable.js collections.

In the case posted by @Hamper the mistake was in calling the schema-free createNode in the first place, as the right thing to do here is to keep the value as a Date and to get the !!timestamp tag to handle it directly. That should now happen thanks to an instanceof tagObj.class lookup, and it's even further customisable by defining a tagObj.createNode(value) function (required for non-scalar custom tags).

As these technically count as new features, I'll include them in the next minor release, probably at the same time as adding blank-line support. I'm also tempted to add support for !!set using the new tagObj.createNode functionality.

Once released, this is how it'll work:

const date = new Date('2018-12-22T08:02:52Z')

YAML.stringify(date)
  // '2018-12-22T08:02:52.000Z\n'

YAML.stringify(date, { version: '1.1' })
  // '2018-12-22T08:02:52\n'

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

No branches or pull requests

3 participants