index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. var test = require('tape');
  3. var mockProperty = require('mock-property');
  4. var hasSymbols = require('has-symbols/shams')();
  5. var isConcatSpreadable = hasSymbols && Symbol.isConcatSpreadable;
  6. var species = hasSymbols && Symbol.species;
  7. var boundFnsHaveConfigurableLengths = Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(function () {}.bind(), 'length').configurable;
  8. var safeConcat = require('../');
  9. test('safe-array-concat', function (t) {
  10. t.equal(typeof safeConcat, 'function', 'is a function');
  11. t.equal(
  12. safeConcat.length,
  13. boundFnsHaveConfigurableLengths ? 1 : 0,
  14. 'has a length of ' + (boundFnsHaveConfigurableLengths ? 1 : '0 (function lengths are not configurable)'),
  15. 'length is as expected'
  16. );
  17. t.deepEqual(
  18. safeConcat([1, 2], [3, 4], 'foo', 5, 6, [[7]]),
  19. [1, 2, 3, 4, 'foo', 5, 6, [7]],
  20. 'works with flat and nested arrays'
  21. );
  22. t.deepEqual(
  23. safeConcat(undefined, 1, 2),
  24. [undefined, 1, 2],
  25. 'first item as undefined is not the concat receiver, which would throw via ToObject'
  26. );
  27. t.deepEqual(
  28. safeConcat(null, 1, 2),
  29. [null, 1, 2],
  30. 'first item as null is not the concat receiver, which would throw via ToObject'
  31. );
  32. var arr = [1, 2];
  33. arr.constructor = function C() {
  34. return { args: arguments };
  35. };
  36. t.deepEqual(
  37. safeConcat(arr, 3, 4),
  38. [1, 2, 3, 4],
  39. 'first item as an array with a nonArray .constructor; ignores constructor'
  40. );
  41. t.test('has Symbol.species', { skip: !species }, function (st) {
  42. var speciesArr = [1, 2];
  43. speciesArr.constructor = {};
  44. speciesArr.constructor[species] = function Species() {
  45. return { args: arguments };
  46. };
  47. st.deepEqual(
  48. safeConcat(speciesArr, 3, 4),
  49. [1, 2, 3, 4],
  50. 'first item as an array with a .constructor object with a Symbol.species; ignores constructor and species'
  51. );
  52. st.end();
  53. });
  54. t.test('has isConcatSpreadable', { skip: !isConcatSpreadable }, function (st) {
  55. st.teardown(mockProperty(String.prototype, isConcatSpreadable, { value: true }));
  56. var nonSpreadable = [1, 2];
  57. nonSpreadable[isConcatSpreadable] = false;
  58. st.deepEqual(
  59. safeConcat(nonSpreadable, 3, 4, 'foo', Object('bar')),
  60. [1, 2, 3, 4, 'foo', Object('bar')],
  61. 'a non-concat-spreadable array is spreaded, and a concat-spreadable String is not spreaded'
  62. );
  63. st.teardown(mockProperty(Array.prototype, isConcatSpreadable, { value: false }));
  64. st.deepEqual(
  65. safeConcat([1, 2], 3, 4, 'foo', Object('bar')),
  66. [1, 2, 3, 4, 'foo', Object('bar')],
  67. 'all arrays marked non-concat-spreadable are still spreaded, and a concat-spreadable String is not spreaded'
  68. );
  69. st.end();
  70. });
  71. t.end();
  72. });