utils.js 15 KB

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