Skip to content

Commit

Permalink
fix: Stringify a Document as a Document (fixes #576)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Oct 13, 2024
1 parent baaabd0 commit 438688f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Reviver } from './doc/applyReviver.js'
import { Document, Replacer } from './doc/Document.js'
import { prettifyError, YAMLParseError } from './errors.js'
import { warn } from './log.js'
import { isDocument } from './nodes/identity.js'
import type { Node, ParsedNode } from './nodes/Node.js'
import type {
CreateNodeOptions,
Expand Down Expand Up @@ -225,5 +226,6 @@ export function stringify(
const { keepUndefined } = options ?? (replacer as CreateNodeOptions) ?? {}
if (!keepUndefined) return undefined
}
if (isDocument(value) && !_replacer) return value.toString(options)
return new Document(value, _replacer, options).toString(options)
}
6 changes: 6 additions & 0 deletions tests/doc/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,12 @@ describe('YAML.stringify on ast Document', () => {
const doc = YAML.parseDocument('null')
expect(YAML.stringify(doc)).toBe('null\n')
})

test('Document with comments', () => {
const src = 'a: 1\n\n# c\n'
const doc = YAML.parseDocument(src)
expect(YAML.stringify(doc)).toBe(src)
})
})

describe('flow collection padding', () => {
Expand Down

0 comments on commit 438688f

Please sign in to comment.