keys.js 644 B

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