keys.js 781 B

12345678910111213141516171819202122232425262728293031
  1. var test = require('tape');
  2. var traverse = require('../');
  3. test('sort test', function (t) {
  4. var acc = [];
  5. traverse({
  6. a: 30,
  7. b: 22,
  8. id: 9
  9. }).forEach(function (node) {
  10. if ((! Array.isArray(node)) && typeof node === 'object') {
  11. this.before(function(node) {
  12. this.keys = Object.keys(node);
  13. this.keys.sort(function(a, b) {
  14. a = [a === "id" ? 0 : 1, a];
  15. b = [b === "id" ? 0 : 1, b];
  16. return a < b ? -1 : a > b ? 1 : 0;
  17. });
  18. });
  19. }
  20. if (this.isLeaf) acc.push(node);
  21. });
  22. t.equal(
  23. acc.join(' '),
  24. '9 30 22',
  25. 'Traversal in a custom order'
  26. );
  27. t.end();
  28. });