Skip to content

@std/yaml: sortKeys doesn't take depth into account #6898

@scarf005

Description

@scarf005

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions