utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 v = require('es-value-fixtures');
  7. var utils = require('../lib/utils');
  8. test('merge()', function (t) {
  9. t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
  10. t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
  11. t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
  12. var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
  13. t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
  14. var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
  15. t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
  16. var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
  17. t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
  18. var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
  19. t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
  20. var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
  21. t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
  22. var func = function f() {};
  23. t.deepEqual(
  24. utils.merge(func, { foo: 'bar' }),
  25. [func, { foo: 'bar' }],
  26. 'functions can not be merged into'
  27. );
  28. func.bar = 'baz';
  29. t.deepEqual(
  30. utils.merge({ foo: 'bar' }, func),
  31. { foo: 'bar', bar: 'baz' },
  32. 'functions can be merge sources'
  33. );
  34. t.test(
  35. 'avoids invoking array setters unnecessarily',
  36. { skip: typeof Object.defineProperty !== 'function' },
  37. function (st) {
  38. var setCount = 0;
  39. var getCount = 0;
  40. var observed = [];
  41. Object.defineProperty(observed, 0, {
  42. get: function () {
  43. getCount += 1;
  44. return { bar: 'baz' };
  45. },
  46. set: function () { setCount += 1; }
  47. });
  48. utils.merge(observed, [null]);
  49. st.equal(setCount, 0);
  50. st.equal(getCount, 1);
  51. observed[0] = observed[0]; // eslint-disable-line no-self-assign
  52. st.equal(setCount, 1);
  53. st.equal(getCount, 2);
  54. st.end();
  55. }
  56. );
  57. t.test('with overflow objects (from arrayLimit)', function (st) {
  58. st.test('merges primitive into overflow object at next index', function (s2t) {
  59. // Create an overflow object via combine
  60. var overflow = utils.combine(['a'], 'b', 1, false);
  61. s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
  62. var merged = utils.merge(overflow, 'c');
  63. s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'adds primitive at next numeric index');
  64. s2t.end();
  65. });
  66. st.test('merges primitive into regular object with numeric keys normally', function (s2t) {
  67. var obj = { 0: 'a', 1: 'b' };
  68. s2t.notOk(utils.isOverflow(obj), 'plain object is not marked as overflow');
  69. var merged = utils.merge(obj, 'c');
  70. s2t.deepEqual(merged, { 0: 'a', 1: 'b', c: true }, 'adds primitive as key (not at next index)');
  71. s2t.end();
  72. });
  73. st.test('merges primitive into object with non-numeric keys normally', function (s2t) {
  74. var obj = { foo: 'bar' };
  75. var merged = utils.merge(obj, 'baz');
  76. s2t.deepEqual(merged, { foo: 'bar', baz: true }, 'adds primitive as key with value true');
  77. s2t.end();
  78. });
  79. st.test('merges overflow object into primitive', function (s2t) {
  80. // Create an overflow object via combine
  81. var overflow = utils.combine([], 'b', 0, false);
  82. s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
  83. var merged = utils.merge('a', overflow);
  84. s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
  85. s2t.deepEqual(merged, { 0: 'a', 1: 'b' }, 'creates object with primitive at 0, source values shifted');
  86. s2t.end();
  87. });
  88. st.test('merges overflow object with multiple values into primitive', function (s2t) {
  89. // Create an overflow object via combine
  90. var overflow = utils.combine(['b'], 'c', 1, false);
  91. s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
  92. var merged = utils.merge('a', overflow);
  93. s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'shifts all source indices by 1');
  94. s2t.end();
  95. });
  96. st.test('merges regular object into primitive as array', function (s2t) {
  97. var obj = { foo: 'bar' };
  98. var merged = utils.merge('a', obj);
  99. s2t.deepEqual(merged, ['a', { foo: 'bar' }], 'creates array with primitive and object');
  100. s2t.end();
  101. });
  102. st.end();
  103. });
  104. t.end();
  105. });
  106. test('assign()', function (t) {
  107. var target = { a: 1, b: 2 };
  108. var source = { b: 3, c: 4 };
  109. var result = utils.assign(target, source);
  110. t.equal(result, target, 'returns the target');
  111. t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
  112. t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
  113. t.end();
  114. });
  115. test('combine()', function (t) {
  116. t.test('both arrays', function (st) {
  117. var a = [1];
  118. var b = [2];
  119. var combined = utils.combine(a, b);
  120. st.deepEqual(a, [1], 'a is not mutated');
  121. st.deepEqual(b, [2], 'b is not mutated');
  122. st.notEqual(a, combined, 'a !== combined');
  123. st.notEqual(b, combined, 'b !== combined');
  124. st.deepEqual(combined, [1, 2], 'combined is a + b');
  125. st.end();
  126. });
  127. t.test('one array, one non-array', function (st) {
  128. var aN = 1;
  129. var a = [aN];
  130. var bN = 2;
  131. var b = [bN];
  132. var combinedAnB = utils.combine(aN, b);
  133. st.deepEqual(b, [bN], 'b is not mutated');
  134. st.notEqual(aN, combinedAnB, 'aN + b !== aN');
  135. st.notEqual(a, combinedAnB, 'aN + b !== a');
  136. st.notEqual(bN, combinedAnB, 'aN + b !== bN');
  137. st.notEqual(b, combinedAnB, 'aN + b !== b');
  138. st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
  139. var combinedABn = utils.combine(a, bN);
  140. st.deepEqual(a, [aN], 'a is not mutated');
  141. st.notEqual(aN, combinedABn, 'a + bN !== aN');
  142. st.notEqual(a, combinedABn, 'a + bN !== a');
  143. st.notEqual(bN, combinedABn, 'a + bN !== bN');
  144. st.notEqual(b, combinedABn, 'a + bN !== b');
  145. st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
  146. st.end();
  147. });
  148. t.test('neither is an array', function (st) {
  149. var combined = utils.combine(1, 2);
  150. st.notEqual(1, combined, '1 + 2 !== 1');
  151. st.notEqual(2, combined, '1 + 2 !== 2');
  152. st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
  153. st.end();
  154. });
  155. t.test('with arrayLimit', function (st) {
  156. st.test('under the limit', function (s2t) {
  157. var combined = utils.combine(['a', 'b'], 'c', 10, false);
  158. s2t.deepEqual(combined, ['a', 'b', 'c'], 'returns array when under limit');
  159. s2t.ok(Array.isArray(combined), 'result is an array');
  160. s2t.end();
  161. });
  162. st.test('exactly at the limit stays as array', function (s2t) {
  163. var combined = utils.combine(['a', 'b'], 'c', 3, false);
  164. s2t.deepEqual(combined, ['a', 'b', 'c'], 'stays as array when exactly at limit');
  165. s2t.ok(Array.isArray(combined), 'result is an array');
  166. s2t.end();
  167. });
  168. st.test('over the limit', function (s2t) {
  169. var combined = utils.combine(['a', 'b', 'c'], 'd', 3, false);
  170. s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to object when over limit');
  171. s2t.notOk(Array.isArray(combined), 'result is not an array');
  172. s2t.end();
  173. });
  174. st.test('with arrayLimit 0', function (s2t) {
  175. var combined = utils.combine([], 'a', 0, false);
  176. s2t.deepEqual(combined, { 0: 'a' }, 'converts single element to object with arrayLimit 0');
  177. s2t.notOk(Array.isArray(combined), 'result is not an array');
  178. s2t.end();
  179. });
  180. st.test('with plainObjects option', function (s2t) {
  181. var combined = utils.combine(['a'], 'b', 1, true);
  182. var expected = { __proto__: null, 0: 'a', 1: 'b' };
  183. s2t.deepEqual(combined, expected, 'converts to object with null prototype');
  184. s2t.equal(Object.getPrototypeOf(combined), null, 'result has null prototype when plainObjects is true');
  185. s2t.end();
  186. });
  187. st.end();
  188. });
  189. t.test('with existing overflow object', function (st) {
  190. st.test('adds to existing overflow object at next index', function (s2t) {
  191. // Create overflow object first via combine
  192. var overflow = utils.combine(['a'], 'b', 1, false);
  193. s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');
  194. var combined = utils.combine(overflow, 'c', 10, false);
  195. s2t.equal(combined, overflow, 'returns the same object (mutated)');
  196. s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c' }, 'adds value at next numeric index');
  197. s2t.end();
  198. });
  199. st.test('does not treat plain object with numeric keys as overflow', function (s2t) {
  200. var plainObj = { 0: 'a', 1: 'b' };
  201. s2t.notOk(utils.isOverflow(plainObj), 'plain object is not marked as overflow');
  202. // combine treats this as a regular value, not an overflow object to append to
  203. var combined = utils.combine(plainObj, 'c', 10, false);
  204. s2t.deepEqual(combined, [{ 0: 'a', 1: 'b' }, 'c'], 'concatenates as regular values');
  205. s2t.end();
  206. });
  207. st.end();
  208. });
  209. t.end();
  210. });
  211. test('decode', function (t) {
  212. t.equal(
  213. utils.decode('a+b'),
  214. 'a b',
  215. 'decodes + to space'
  216. );
  217. t.equal(
  218. utils.decode('name%2Eobj'),
  219. 'name.obj',
  220. 'decodes a string'
  221. );
  222. t.equal(
  223. utils.decode('name%2Eobj%2Efoo', null, 'iso-8859-1'),
  224. 'name.obj.foo',
  225. 'decodes a string in iso-8859-1'
  226. );
  227. t.end();
  228. });
  229. test('encode', function (t) {
  230. forEach(v.nullPrimitives, function (nullish) {
  231. t['throws'](
  232. function () { utils.encode(nullish); },
  233. TypeError,
  234. inspect(nullish) + ' is not a string'
  235. );
  236. });
  237. t.equal(utils.encode(''), '', 'empty string returns itself');
  238. t.deepEqual(utils.encode([]), [], 'empty array returns itself');
  239. t.deepEqual(utils.encode({ length: 0 }), { length: 0 }, 'empty arraylike returns itself');
  240. t.test('symbols', { skip: !v.hasSymbols }, function (st) {
  241. st.equal(utils.encode(Symbol('x')), 'Symbol%28x%29', 'symbol is encoded');
  242. st.end();
  243. });
  244. t.equal(
  245. utils.encode('(abc)'),
  246. '%28abc%29',
  247. 'encodes parentheses'
  248. );
  249. t.equal(
  250. utils.encode({ toString: function () { return '(abc)'; } }),
  251. '%28abc%29',
  252. 'toStrings and encodes parentheses'
  253. );
  254. t.equal(
  255. utils.encode('abc 123 💩', null, 'iso-8859-1'),
  256. 'abc%20123%20%26%2355357%3B%26%2356489%3B',
  257. 'encodes in iso-8859-1'
  258. );
  259. var longString = '';
  260. var expectedString = '';
  261. for (var i = 0; i < 1500; i++) {
  262. longString += ' ';
  263. expectedString += '%20';
  264. }
  265. t.equal(
  266. utils.encode(longString),
  267. expectedString,
  268. 'encodes a long string'
  269. );
  270. t.equal(
  271. utils.encode('\x28\x29'),
  272. '%28%29',
  273. 'encodes parens normally'
  274. );
  275. t.equal(
  276. utils.encode('\x28\x29', null, null, null, 'RFC1738'),
  277. '()',
  278. 'does not encode parens in RFC1738'
  279. );
  280. // todo RFC1738 format
  281. t.equal(
  282. utils.encode('Āက豈'),
  283. '%C4%80%E1%80%80%EF%A4%80',
  284. 'encodes multibyte chars'
  285. );
  286. t.equal(
  287. utils.encode('\uD83D \uDCA9'),
  288. '%F0%9F%90%A0%F0%BA%90%80',
  289. 'encodes lone surrogates'
  290. );
  291. t.end();
  292. });
  293. test('isBuffer()', function (t) {
  294. forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
  295. t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
  296. });
  297. var fakeBuffer = { constructor: Buffer };
  298. t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
  299. var saferBuffer = SaferBuffer.from('abc');
  300. t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
  301. var buffer = Buffer.from && Buffer.alloc ? Buffer.from('abc') : new Buffer('abc');
  302. t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
  303. t.end();
  304. });
  305. test('isRegExp()', function (t) {
  306. t.equal(utils.isRegExp(/a/g), true, 'RegExp is a RegExp');
  307. t.equal(utils.isRegExp(new RegExp('a', 'g')), true, 'new RegExp is a RegExp');
  308. t.equal(utils.isRegExp(new Date()), false, 'Date is not a RegExp');
  309. forEach(v.primitives, function (primitive) {
  310. t.equal(utils.isRegExp(primitive), false, inspect(primitive) + ' is not a RegExp');
  311. });
  312. t.end();
  313. });