replacer.js 974 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var assert = require('assert');
  3. var stream = require('stream');
  4. var hash = require('../index');
  5. describe('replacer option', function() {
  6. it('should emit information about an object to a stream', function() {
  7. var strm = new stream.PassThrough();
  8. var replacer = function(value) {
  9. try {
  10. return JSON.stringify(value);
  11. } catch (e) {
  12. return value;
  13. }
  14. };
  15. var obj = {foo: 'bar'};
  16. hash.writeToStream(obj, {replacer: replacer}, strm);
  17. var result = strm.read().toString();
  18. assert.strictEqual(typeof result, 'string');
  19. assert.notStrictEqual(result.indexOf(JSON.stringify(obj)), -1);
  20. });
  21. it('should not reach property values when excludeValues = true', function() {
  22. var strm = new stream.PassThrough();
  23. var replacer = function(k) {
  24. assert.notStrictEqual(k, 'bar');
  25. return k;
  26. };
  27. hash.writeToStream({foo: 'bar'}, {excludeValues: true}, strm);
  28. });
  29. });