date.js 780 B

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