/ node_modules / qs / test / utils.js
utils.js
  1  'use strict';
  2  
  3  var test = require('tape');
  4  var inspect = require('object-inspect');
  5  var SaferBuffer = require('safer-buffer').Buffer;
  6  var forEach = require('for-each');
  7  var v = require('es-value-fixtures');
  8  
  9  var utils = require('../lib/utils');
 10  
 11  test('merge()', function (t) {
 12      t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
 13  
 14      t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
 15  
 16      t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
 17  
 18      var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
 19      t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
 20  
 21      var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
 22      t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
 23  
 24      var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
 25      t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
 26  
 27      var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
 28      t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
 29  
 30      var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
 31      t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
 32  
 33      var func = function f() {};
 34      t.deepEqual(
 35          utils.merge(func, { foo: 'bar' }),
 36          [func, { foo: 'bar' }],
 37          'functions can not be merged into'
 38      );
 39  
 40      func.bar = 'baz';
 41      t.deepEqual(
 42          utils.merge({ foo: 'bar' }, func),
 43          { foo: 'bar', bar: 'baz' },
 44          'functions can be merge sources'
 45      );
 46  
 47      t.test(
 48          'avoids invoking array setters unnecessarily',
 49          { skip: typeof Object.defineProperty !== 'function' },
 50          function (st) {
 51              var setCount = 0;
 52              var getCount = 0;
 53              var observed = [];
 54              Object.defineProperty(observed, 0, {
 55                  get: function () {
 56                      getCount += 1;
 57                      return { bar: 'baz' };
 58                  },
 59                  set: function () { setCount += 1; }
 60              });
 61              utils.merge(observed, [null]);
 62              st.equal(setCount, 0);
 63              st.equal(getCount, 1);
 64              observed[0] = observed[0]; // eslint-disable-line no-self-assign
 65              st.equal(setCount, 1);
 66              st.equal(getCount, 2);
 67              st.end();
 68          }
 69      );
 70  
 71      t.end();
 72  });
 73  
 74  test('assign()', function (t) {
 75      var target = { a: 1, b: 2 };
 76      var source = { b: 3, c: 4 };
 77      var result = utils.assign(target, source);
 78  
 79      t.equal(result, target, 'returns the target');
 80      t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
 81      t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
 82  
 83      t.end();
 84  });
 85  
 86  test('combine()', function (t) {
 87      t.test('both arrays', function (st) {
 88          var a = [1];
 89          var b = [2];
 90          var combined = utils.combine(a, b);
 91  
 92          st.deepEqual(a, [1], 'a is not mutated');
 93          st.deepEqual(b, [2], 'b is not mutated');
 94          st.notEqual(a, combined, 'a !== combined');
 95          st.notEqual(b, combined, 'b !== combined');
 96          st.deepEqual(combined, [1, 2], 'combined is a + b');
 97  
 98          st.end();
 99      });
100  
101      t.test('one array, one non-array', function (st) {
102          var aN = 1;
103          var a = [aN];
104          var bN = 2;
105          var b = [bN];
106  
107          var combinedAnB = utils.combine(aN, b);
108          st.deepEqual(b, [bN], 'b is not mutated');
109          st.notEqual(aN, combinedAnB, 'aN + b !== aN');
110          st.notEqual(a, combinedAnB, 'aN + b !== a');
111          st.notEqual(bN, combinedAnB, 'aN + b !== bN');
112          st.notEqual(b, combinedAnB, 'aN + b !== b');
113          st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
114  
115          var combinedABn = utils.combine(a, bN);
116          st.deepEqual(a, [aN], 'a is not mutated');
117          st.notEqual(aN, combinedABn, 'a + bN !== aN');
118          st.notEqual(a, combinedABn, 'a + bN !== a');
119          st.notEqual(bN, combinedABn, 'a + bN !== bN');
120          st.notEqual(b, combinedABn, 'a + bN !== b');
121          st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
122  
123          st.end();
124      });
125  
126      t.test('neither is an array', function (st) {
127          var combined = utils.combine(1, 2);
128          st.notEqual(1, combined, '1 + 2 !== 1');
129          st.notEqual(2, combined, '1 + 2 !== 2');
130          st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
131  
132          st.end();
133      });
134  
135      t.end();
136  });
137  
138  test('decode', function (t) {
139      t.equal(
140          utils.decode('a+b'),
141          'a b',
142          'decodes + to space'
143      );
144  
145      t.equal(
146          utils.decode('name%2Eobj'),
147          'name.obj',
148          'decodes a string'
149      );
150      t.equal(
151          utils.decode('name%2Eobj%2Efoo', null, 'iso-8859-1'),
152          'name.obj.foo',
153          'decodes a string in iso-8859-1'
154      );
155  
156      t.end();
157  });
158  
159  test('encode', function (t) {
160      forEach(v.nullPrimitives, function (nullish) {
161          t['throws'](
162              function () { utils.encode(nullish); },
163              TypeError,
164              inspect(nullish) + ' is not a string'
165          );
166      });
167  
168      t.equal(utils.encode(''), '', 'empty string returns itself');
169      t.deepEqual(utils.encode([]), [], 'empty array returns itself');
170      t.deepEqual(utils.encode({ length: 0 }), { length: 0 }, 'empty arraylike returns itself');
171  
172      t.test('symbols', { skip: !v.hasSymbols }, function (st) {
173          st.equal(utils.encode(Symbol('x')), 'Symbol%28x%29', 'symbol is encoded');
174  
175          st.end();
176      });
177  
178      t.equal(
179          utils.encode('(abc)'),
180          '%28abc%29',
181          'encodes parentheses'
182      );
183      t.equal(
184          utils.encode({ toString: function () { return '(abc)'; } }),
185          '%28abc%29',
186          'toStrings and encodes parentheses'
187      );
188  
189      t.equal(
190          utils.encode('abc 123 💩', null, 'iso-8859-1'),
191          'abc%20123%20%26%2355357%3B%26%2356489%3B',
192          'encodes in iso-8859-1'
193      );
194  
195      var longString = '';
196      var expectedString = '';
197      for (var i = 0; i < 1500; i++) {
198          longString += ' ';
199          expectedString += '%20';
200      }
201  
202      t.equal(
203          utils.encode(longString),
204          expectedString,
205          'encodes a long string'
206      );
207  
208      t.equal(
209          utils.encode('\x28\x29'),
210          '%28%29',
211          'encodes parens normally'
212      );
213      t.equal(
214          utils.encode('\x28\x29', null, null, null, 'RFC1738'),
215          '()',
216          'does not encode parens in RFC1738'
217      );
218  
219      // todo RFC1738 format
220  
221      t.equal(
222          utils.encode('Āက豈'),
223          '%C4%80%E1%80%80%EF%A4%80',
224          'encodes multibyte chars'
225      );
226  
227      t.equal(
228          utils.encode('\uD83D \uDCA9'),
229          '%F0%9F%90%A0%F0%BA%90%80',
230          'encodes lone surrogates'
231      );
232  
233      t.end();
234  });
235  
236  test('isBuffer()', function (t) {
237      forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
238          t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
239      });
240  
241      var fakeBuffer = { constructor: Buffer };
242      t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
243  
244      var saferBuffer = SaferBuffer.from('abc');
245      t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
246  
247      var buffer = Buffer.from && Buffer.alloc ? Buffer.from('abc') : new Buffer('abc');
248      t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
249      t.end();
250  });
251  
252  test('isRegExp()', function (t) {
253      t.equal(utils.isRegExp(/a/g), true, 'RegExp is a RegExp');
254      t.equal(utils.isRegExp(new RegExp('a', 'g')), true, 'new RegExp is a RegExp');
255      t.equal(utils.isRegExp(new Date()), false, 'Date is not a RegExp');
256  
257      forEach(v.primitives, function (primitive) {
258          t.equal(utils.isRegExp(primitive), false, inspect(primitive) + ' is not a RegExp');
259      });
260  
261      t.end();
262  });