export function convertJsonSnippetToYaml()

in src/yaml/yamlUtils.ts [309:339]


export function convertJsonSnippetToYaml(snippetText: string) {
  // Convert to YAML with indendation of 1 space
  return (
    dump(load(snippetText), { indent: 1 })
      // Remove quotation marks
      .replace(/[']/g, '')
      .split('\n')
      // For each line replace left padding spaces with tabs
      .map((line) => {
        if (line.length) {
          let numOfSpaces = 0

          // Count number of spaces that the line begins with
          for (const char of line) {
            if (char === ' ') {
              numOfSpaces++
            } else {
              break
            }
          }

          // Convert each space to tab character. Even though tab carracters are not valid yaml whitespace characters
          // the vscode will convert them to the correct number of spaces according to user preferences.
          return '\t'.repeat(numOfSpaces) + line.slice(numOfSpaces, line.length)
        } else {
          return line
        }
      })
      .join('\n')
  )
}