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

Use YAMLMap#add in internal map item additions #267

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compose/resolve-block-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function resolveBlockMap(
? composeNode(ctx, value, valueProps, onError)
: composeEmptyNode(ctx, offset, sep, null, valueProps, onError)
offset = valueNode.range[2]
map.items.push(new Pair(keyNode, valueNode))
map.add(new Pair(keyNode, valueNode), ctx.schema.merge)
} else {
// key with no value
if (implicitKey)
Expand All @@ -107,7 +107,7 @@ export function resolveBlockMap(
if (keyNode.comment) keyNode.comment += '\n' + valueProps.comment
else keyNode.comment = valueProps.comment
}
map.items.push(new Pair(keyNode))
map.add(new Pair(keyNode), ctx.schema.merge)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/compose/resolve-flow-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function resolveFlowCollection(
else {
const map = new YAMLMap(ctx.schema)
map.flow = true
map.items.push(pair)
map.add(pair, ctx.schema.merge)
;(coll as YAMLSeq).items.push(map)
}
offset = valueNode ? valueNode.range[2] : valueProps.end
Expand Down
2 changes: 1 addition & 1 deletion src/doc/createNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function createNode(
if (isNode(value)) return value as Node
if (isPair(value)) {
const map = ctx.schema[MAP].createNode?.(ctx.schema, null, ctx) as YAMLMap
map.items.push(value)
map.add(value)
return map
}
if (
Expand Down
2 changes: 1 addition & 1 deletion src/schema/common/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createMap(schema: Schema, obj: unknown, ctx: CreateNodeContext) {
if (typeof replacer === 'function') value = replacer.call(obj, key, value)
else if (Array.isArray(replacer) && !replacer.includes(key)) return
if (value !== undefined || keepUndefined)
map.items.push(createPair(key, value, ctx))
map.add(createPair(key, value, ctx), schema.merge)
}
if (obj instanceof Map) {
for (const [key, value] of obj) add(key, value)
Expand Down
4 changes: 2 additions & 2 deletions tests/doc/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ y:
{ k0: 'v1', k1: 'v1' },
{ k1: 'v2', k2: 'v2' }
],
y: { k0: 'v0', k1: 'v1', k2: 'v2' }
y: { k0: 'v0', k1: 'v2', k2: 'v2' }
}

const expMap = new Map([
Expand All @@ -266,7 +266,7 @@ y:
'y',
new Map([
['k0', 'v0'],
['k1', 'v1'],
['k1', 'v2'],
['k2', 'v2']
])
]
Expand Down
4 changes: 4 additions & 0 deletions tests/doc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ describe('sortMapEntries', () => {
doc.set('bb', 4)
expect(String(doc)).toBe('a: 1\nb: 2\nbb: 4\nc: 3\n')
})
test('parseDocument', () => {
const doc = YAML.parseDocument(YAML.stringify(obj), { sortMapEntries: true })
expect(String(doc)).toBe('a: 1\nb: 2\nc: 3\n')
})
})

describe('custom indent', () => {
Expand Down