租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.0 KiB

6 months ago
2 months ago
6 months ago
2 months ago
6 months ago
2 months ago
  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.end();
  58. });
  59. test('assign()', function (t) {
  60. var target = { a: 1, b: 2 };
  61. var source = { b: 3, c: 4 };
  62. var result = utils.assign(target, source);
  63. t.equal(result, target, 'returns the target');
  64. t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
  65. t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
  66. t.end();
  67. });
  68. test('combine()', function (t) {
  69. t.test('both arrays', function (st) {
  70. var a = [1];
  71. var b = [2];
  72. var combined = utils.combine(a, b);
  73. st.deepEqual(a, [1], 'a is not mutated');
  74. st.deepEqual(b, [2], 'b is not mutated');
  75. st.notEqual(a, combined, 'a !== combined');
  76. st.notEqual(b, combined, 'b !== combined');
  77. st.deepEqual(combined, [1, 2], 'combined is a + b');
  78. st.end();
  79. });
  80. t.test('one array, one non-array', function (st) {
  81. var aN = 1;
  82. var a = [aN];
  83. var bN = 2;
  84. var b = [bN];
  85. var combinedAnB = utils.combine(aN, b);
  86. st.deepEqual(b, [bN], 'b is not mutated');
  87. st.notEqual(aN, combinedAnB, 'aN + b !== aN');
  88. st.notEqual(a, combinedAnB, 'aN + b !== a');
  89. st.notEqual(bN, combinedAnB, 'aN + b !== bN');
  90. st.notEqual(b, combinedAnB, 'aN + b !== b');
  91. st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
  92. var combinedABn = utils.combine(a, bN);
  93. st.deepEqual(a, [aN], 'a is not mutated');
  94. st.notEqual(aN, combinedABn, 'a + bN !== aN');
  95. st.notEqual(a, combinedABn, 'a + bN !== a');
  96. st.notEqual(bN, combinedABn, 'a + bN !== bN');
  97. st.notEqual(b, combinedABn, 'a + bN !== b');
  98. st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
  99. st.end();
  100. });
  101. t.test('neither is an array', function (st) {
  102. var combined = utils.combine(1, 2);
  103. st.notEqual(1, combined, '1 + 2 !== 1');
  104. st.notEqual(2, combined, '1 + 2 !== 2');
  105. st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
  106. st.end();
  107. });
  108. t.end();
  109. });
  110. test('decode', function (t) {
  111. t.equal(
  112. utils.decode('a+b'),
  113. 'a b',
  114. 'decodes + to space'
  115. );
  116. t.equal(
  117. utils.decode('name%2Eobj'),
  118. 'name.obj',
  119. 'decodes a string'
  120. );
  121. t.equal(
  122. utils.decode('name%2Eobj%2Efoo', null, 'iso-8859-1'),
  123. 'name.obj.foo',
  124. 'decodes a string in iso-8859-1'
  125. );
  126. t.end();
  127. });
  128. test('encode', function (t) {
  129. forEach(v.nullPrimitives, function (nullish) {
  130. t['throws'](
  131. function () { utils.encode(nullish); },
  132. TypeError,
  133. inspect(nullish) + ' is not a string'
  134. );
  135. });
  136. t.equal(utils.encode(''), '', 'empty string returns itself');
  137. t.deepEqual(utils.encode([]), [], 'empty array returns itself');
  138. t.deepEqual(utils.encode({ length: 0 }), { length: 0 }, 'empty arraylike returns itself');
  139. t.test('symbols', { skip: !v.hasSymbols }, function (st) {
  140. st.equal(utils.encode(Symbol('x')), 'Symbol%28x%29', 'symbol is encoded');
  141. st.end();
  142. });
  143. t.equal(
  144. utils.encode('(abc)'),
  145. '%28abc%29',
  146. 'encodes parentheses'
  147. );
  148. t.equal(
  149. utils.encode({ toString: function () { return '(abc)'; } }),
  150. '%28abc%29',
  151. 'toStrings and encodes parentheses'
  152. );
  153. t.equal(
  154. utils.encode('abc 123 💩', null, 'iso-8859-1'),
  155. 'abc%20123%20%26%2355357%3B%26%2356489%3B',
  156. 'encodes in iso-8859-1'
  157. );
  158. var longString = '';
  159. var expectedString = '';
  160. for (var i = 0; i < 1500; i++) {
  161. longString += ' ';
  162. expectedString += '%20';
  163. }
  164. t.equal(
  165. utils.encode(longString),
  166. expectedString,
  167. 'encodes a long string'
  168. );
  169. t.equal(
  170. utils.encode('\x28\x29'),
  171. '%28%29',
  172. 'encodes parens normally'
  173. );
  174. t.equal(
  175. utils.encode('\x28\x29', null, null, null, 'RFC1738'),
  176. '()',
  177. 'does not encode parens in RFC1738'
  178. );
  179. // todo RFC1738 format
  180. t.equal(
  181. utils.encode('Āက豈'),
  182. '%C4%80%E1%80%80%EF%A4%80',
  183. 'encodes multibyte chars'
  184. );
  185. t.equal(
  186. utils.encode('\uD83D \uDCA9'),
  187. '%F0%9F%90%A0%F0%BA%90%80',
  188. 'encodes lone surrogates'
  189. );
  190. t.end();
  191. });
  192. test('isBuffer()', function (t) {
  193. forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
  194. t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
  195. });
  196. var fakeBuffer = { constructor: Buffer };
  197. t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
  198. var saferBuffer = SaferBuffer.from('abc');
  199. t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
  200. var buffer = Buffer.from && Buffer.alloc ? Buffer.from('abc') : new Buffer('abc');
  201. t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
  202. t.end();
  203. });
  204. test('isRegExp()', function (t) {
  205. t.equal(utils.isRegExp(/a/g), true, 'RegExp is a RegExp');
  206. t.equal(utils.isRegExp(new RegExp('a', 'g')), true, 'new RegExp is a RegExp');
  207. t.equal(utils.isRegExp(new Date()), false, 'Date is not a RegExp');
  208. forEach(v.primitives, function (primitive) {
  209. t.equal(utils.isRegExp(primitive), false, inspect(primitive) + ' is not a RegExp');
  210. });
  211. t.end();
  212. });