-1

I'm using the js-yaml package. It has a function named dump() that will write out a JavaScript data structure in YAML format. I want to specify the order of the keys when it dumps out an object. Here is what their docs say:

sortKeys (default: false) - if true, sort keys when dumping YAML. If a function, use the function to sort the keys.

But there's absolutely no mention of how to use a function to sort the keys. What parameters does it accept? What return values does it expect? It's exactly what I need, but there's no documentation on how to use a function with the sortKeys option.

FYI, I don't want to sort the keys alphabetically. I don't want them to appear in a random order. I don't want them to appear in the order the keys were added. I want total control over which order they appear.

John Deighan
  • 4,329
  • 4
  • 18
  • 19

3 Answers3

0

It appears that when you supply a function as the sortKeys option, that function behaves the same way as the function that you can use with the Perl sort() function. I.e. the function receives 2 arguments, call them a and b. If you want key a to appear before key b, return -1 from the function. If you want key a to appear after key b, return 1 from the function. If keys a and b should be considered equal, return 0 from the function. Using this method, I developed a function named toTAML that accepts a javascript data structure and an array of key names. When displaying an object, it will display the keys in the same order as in the supplied array. Keys that don't appear in the array will be put at the end, ordered alphabetically. I will post that function in CoffeeScript, then in the equivalent JavaScript.

John Deighan
  • 4,329
  • 4
  • 18
  • 19
0
import yaml from 'js-yaml'

compareFunc = (a, b) =>

    if (a < b)
        return -1
    else if (a > b)
        return 1
    else
        return 0

toTAML = (obj, lKeys) ->

    h = {}
    for key,i in lKeys
        h[key] = i+1
    sortKeys = (a, b) =>
        if h.hasOwnProperty(a)
            if h.hasOwnProperty(b)
                return compareFunc(h[a], h[b])
            else
                return -1
        else
            if h.hasOwnProperty(b)
                return 1
            else
                # --- compare keys alphabetically
                return compareFunc(a, b)
    return yaml.dump(obj, {
        skipInvalid: true
        indent: 3
        sortKeys
        lineWidth: -1
        })

console.log toTAML({e:5,d:4,c:3,b:2,a:1}, ['c','b','a'])

Output:

c: 3
b: 2
a: 1
d: 4
e: 5
John Deighan
  • 4,329
  • 4
  • 18
  • 19
0
// Generated by CoffeeScript 2.7.0
var compareFunc, toTAML;

import yaml from 'js-yaml';

compareFunc = (a, b) => {
  if (a < b) {
    return -1;
  } else if (a > b) {
    return 1;
  } else {
    return 0;
  }
};

toTAML = function(obj, lKeys) {
  var h, i, j, key, len, sortKeys;
  h = {};
  for (i = j = 0, len = lKeys.length; j < len; i = ++j) {
    key = lKeys[i];
    h[key] = i + 1;
  }
  sortKeys = (a, b) => {
    if (h.hasOwnProperty(a)) {
      if (h.hasOwnProperty(b)) {
        return compareFunc(h[a], h[b]);
      } else {
        return -1;
      }
    } else {
      if (h.hasOwnProperty(b)) {
        return 1;
      } else {
        // --- compare keys alphabetically
        return compareFunc(a, b);
      }
    }
  };
  return yaml.dump(obj, {
    skipInvalid: true,
    indent: 3,
    sortKeys,
    lineWidth: -1
  });
};

console.log(toTAML({
  e: 5,
  d: 4,
  c: 3,
  b: 2,
  a: 1
}, ['c', 'b', 'a']));
John Deighan
  • 4,329
  • 4
  • 18
  • 19