utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var SaferBuffer = require('safer-buffer').Buffer;
  5. var forEach = require('for-each');
  6. var utils = require('../lib/utils');
  7. test('merge()', function (t) {
  8. t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
  9. t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
  10. t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
  11. var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
  12. t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
  13. var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
  14. t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
  15. var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
  16. t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
  17. var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
  18. t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
  19. var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
  20. t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
  21. var func = function f() {};
  22. t.deepEqual(
  23. utils.merge(func, { foo: 'bar' }),
  24. [func, { foo: 'bar' }],
  25. 'functions can not be merged into'
  26. );
  27. func.bar = 'baz';
  28. t.deepEqual(
  29. utils.merge({ foo: 'bar' }, func),
  30. { foo: 'bar', bar: 'baz' },
  31. 'functions can be merge sources'
  32. );
  33. t.test(
  34. 'avoids invoking array setters unnecessarily',
  35. { skip: typeof Object.defineProperty !== 'function' },
  36. function (st) {
  37. var setCount = 0;
  38. var getCount = 0;
  39. var observed = [];
  40. Object.defineProperty(observed, 0, {
  41. get: function () {
  42. getCount += 1;
  43. return { bar: 'baz' };
  44. },
  45. set: function () { setCount += 1; }
  46. });
  47. utils.merge(observed, [null]);
  48. st.equal(setCount, 0);
  49. st.equal(getCount, 1);
  50. observed[0] = observed[0]; // eslint-disable-line no-self-assign
  51. st.equal(setCount, 1);
  52. st.equal(getCount, 2);
  53. st.end();
  54. }
  55. );
  56. t.end();
  57. });
  58. test('assign()', function (t) {
  59. var target = { a: 1, b: 2 };
  60. var source = { b: 3, c: 4 };
  61. var result = utils.assign(target, source);
  62. t.equal(result, target, 'returns the target');
  63. t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
  64. t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
  65. t.end();
  66. });
  67. test('combine()', function (t) {
  68. t.test('both arrays', function (st) {
  69. var a = [1];
  70. var b = [2];
  71. var combined = utils.combine(a, b);
  72. st.deepEqual(a, [1], 'a is not mutated');
  73. st.deepEqual(b, [2], 'b is not mutated');
  74. st.notEqual(a, combined, 'a !== combined');
  75. st.notEqual(b, combined, 'b !== combined');
  76. st.deepEqual(combined, [1, 2], 'combined is a + b');
  77. st.end();
  78. });
  79. t.test('one array, one non-array', function (st) {
  80. var aN = 1;
  81. var a = [aN];
  82. var bN = 2;
  83. var b = [bN];
  84. var combinedAnB = utils.combine(aN, b);
  85. st.deepEqual(b, [bN], 'b is not mutated');
  86. st.notEqual(aN, combinedAnB, 'aN + b !== aN');
  87. st.notEqual(a, combinedAnB, 'aN + b !== a');
  88. st.notEqual(bN, combinedAnB, 'aN + b !== bN');
  89. st.notEqual(b, combinedAnB, 'aN + b !== b');
  90. st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
  91. var combinedABn = utils.combine(a, bN);
  92. st.deepEqual(a, [aN], 'a is not mutated');
  93. st.notEqual(aN, combinedABn, 'a + bN !== aN');
  94. st.notEqual(a, combinedABn, 'a + bN !== a');
  95. st.notEqual(bN, combinedABn, 'a + bN !== bN');
  96. st.notEqual(b, combinedABn, 'a + bN !== b');
  97. st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
  98. st.end();
  99. });
  100. t.test('neither is an array', function (st) {
  101. var combined = utils.combine(1, 2);
  102. st.notEqual(1, combined, '1 + 2 !== 1');
  103. st.notEqual(2, combined, '1 + 2 !== 2');
  104. st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
  105. st.end();
  106. });
  107. t.end();
  108. });
  109. test('isBuffer()', function (t) {
  110. forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
  111. t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
  112. });
  113. var fakeBuffer = { constructor: Buffer };
  114. t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
  115. var saferBuffer = SaferBuffer.from('abc');
  116. t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
  117. var buffer = Buffer.from && Buffer.alloc ? Buffer.from('abc') : new Buffer('abc');
  118. t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
  119. t.end();
  120. });