Skip to content

Commit

Permalink
When creating a mapping from a JS Object, drop undefined values (Closes
Browse files Browse the repository at this point in the history
#173)

BREAKING CHANGE: When creating a YAML mapping from a JS Object, keys
with `undefined` values are not included. This matches the behaviour of
`JSON.stringify()`. In all other cases, the handling of `undefined` is
unchanged, so e.g. `[1, undefined, 2]` will still result in a sequence
of three items, the second of which has a `null` value. This too matches
the behaviour of `JSON.stringify()`.

Previously, `undefined` object values were mapped to `null`.
  • Loading branch information
eemeli committed Aug 23, 2020
1 parent 8c997f1 commit 1568214
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/tags/failsafe/map.js
Expand Up @@ -7,8 +7,10 @@ function createMap(schema, obj, ctx) {
if (obj instanceof Map) {
for (const [key, value] of obj) map.items.push(createPair(key, value, ctx))
} else if (obj && typeof obj === 'object') {
for (const key of Object.keys(obj))
map.items.push(createPair(key, obj[key], ctx))
for (const key of Object.keys(obj)) {
const value = obj[key]
if (value !== undefined) map.items.push(createPair(key, value, ctx))
}
}
if (typeof schema.sortMapEntries === 'function') {
map.items.sort(schema.sortMapEntries)
Expand Down
32 changes: 31 additions & 1 deletion tests/doc/stringify.js
Expand Up @@ -2,7 +2,7 @@

import { source } from 'common-tags'
import YAML from '../../index.js'
import { Pair } from '../../types.js'
import { Pair, Scalar } from '../../types.js'
import { Type, stringifyString } from '../../util.js'

for (const [name, version] of [
Expand Down Expand Up @@ -715,3 +715,33 @@ describe('Document markers in top-level scalars', () => {
expect(YAML.parse(str)).toBe('foo\n%bar\n')
})
})

describe('undefined values', () => {
test('undefined', () => {
expect(YAML.stringify(undefined)).toBe('\n')
})

test('[1, undefined, 2]', () => {
expect(YAML.stringify([1, undefined, 2])).toBe('- 1\n- null\n- 2\n')
})

test("{ a: 'A', b: undefined, c: 'C' }", () => {
expect(YAML.stringify({ a: 'A', b: undefined, c: 'C' })).toBe(
'a: A\nc: C\n'
)
})

test("{ a: 'A', b: Scalar(undefined), c: 'C' }", () => {
const obj = { a: 'A', b: new Scalar(undefined), c: 'C' }
expect(YAML.stringify(obj)).toBe('a: A\nb: null\nc: C\n')
})

test("Map { 'a' => 'A', 'b' => undefined, 'c' => 'C' }", () => {
const map = new Map([
['a', 'A'],
['b', undefined],
['c', 'C']
])
expect(YAML.stringify(map)).toBe('a: A\nb: null\nc: C\n')
})
})

0 comments on commit 1568214

Please sign in to comment.