date.js 676 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var test = require('tape');
  3. var traverse = require('../');
  4. test('dateEach', function (t) {
  5. var obj = { x: new Date(), y: 10, z: 5 };
  6. var counts = {};
  7. traverse(obj).forEach(function (node) {
  8. var type = (node instanceof Date && 'Date') || typeof node;
  9. counts[type] = (counts[type] || 0) + 1;
  10. });
  11. t.same(counts, {
  12. object: 1,
  13. Date: 1,
  14. number: 2,
  15. });
  16. t.end();
  17. });
  18. test('dateMap', function (t) {
  19. var obj = { x: new Date(), y: 10, z: 5 };
  20. var res = traverse(obj).map(function (node) {
  21. if (typeof node === 'number') { this.update(node + 100); }
  22. });
  23. t.ok(obj.x !== res.x);
  24. t.same(res, {
  25. x: obj.x,
  26. y: 110,
  27. z: 105,
  28. });
  29. t.end();
  30. });