utils.js 16 KB

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