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