-
Notifications
You must be signed in to change notification settings - Fork 664
Open
Description
Is your feature request related to a problem? Please describe.
> import * as YAML from "@std/yaml/unstable-stringify"
undefined
> YAML.stringify({ a: 1, b: 2, c: { dd: [1,2,3], ee: 3 } }, { sortKeys: (a, b) => { console.log({a, b}); return a < b }})
{ a: "b", b: "a" }
{ a: "c", b: "b" }
{ a: "ee", b: "dd" }
"a: 1\nb: 2\nc:\n dd:\n - 1\n - 2\n - 3\n ee: 3\n"
sortKeys option in YAML.stringify does not take depth into account. this makes sorting only the first depth (for controlling merge conflict) not possible:
const order = new Map([
["schema_version", 0],
["id", 1],
["display_name", 2],
["short_description", 3],
["author", 4],
["license", 5],
["version", 6],
["homepage", 7],
["icon_url", 8],
["parent", 9],
["source", 10],
["dependencies", 11],
["categories", 12],
["autoupdate", 13],
["last_updated", 14],
])
export const stringifyManifest = (manifest: unknown) =>
YAML.stringify(manifest, {
quoteStyle: '"',
sortKeys: (a, b) => {
const aOrder = order.get(a)
const bOrder = order.get(b)
if (aOrder !== undefined && bOrder !== undefined) {
return aOrder - bOrder
} else if (aOrder !== undefined) {
return -1
} else if (bOrder !== undefined) {
return 1
} else {
return a.localeCompare(b)
}
},
})Describe the solution you'd like
change signature to also pass depth: sortKeys: (a: string, b: string, depth: number) => number
Describe alternatives you've considered
maybe pass in dot-separated path like foo.bar.baz?
Metadata
Metadata
Assignees
Labels
No labels