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.

121 lines
4.5 KiB

4 years ago
  1. 'use strict';
  2. var assign = require('../../helpers/assign');
  3. var hasSymbols = require('has-symbols')();
  4. var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } };
  5. var coercibleFnObject = {
  6. valueOf: function () { return function valueOfFn() {}; },
  7. toString: function () { return 42; }
  8. };
  9. var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
  10. var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
  11. var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
  12. var uncoercibleFnObject = {
  13. valueOf: function () { return function valueOfFn() {}; },
  14. toString: function () { return function toStrFn() {}; }
  15. };
  16. var objects = [{}, coercibleObject, coercibleFnObject, toStringOnlyObject, valueOfOnlyObject];
  17. var nullPrimitives = [undefined, null];
  18. var nonIntegerNumbers = [-1.3, 0.2, 1.8, 1 / 3];
  19. var zeroes = [0, -0];
  20. var infinities = [Infinity, -Infinity];
  21. var numbers = zeroes.concat([42], infinities, nonIntegerNumbers);
  22. var strings = ['', 'foo', 'a\uD83D\uDCA9c'];
  23. var booleans = [true, false];
  24. var symbols = hasSymbols ? [Symbol.iterator, Symbol('foo')] : [];
  25. var nonSymbolPrimitives = [].concat(nullPrimitives, booleans, strings, numbers);
  26. var nonNumberPrimitives = [].concat(nullPrimitives, booleans, strings, symbols);
  27. var nonNullPrimitives = [].concat(booleans, strings, numbers, symbols);
  28. var nonUndefinedPrimitives = [].concat(null, nonNullPrimitives);
  29. var nonStrings = [].concat(nullPrimitives, booleans, numbers, symbols, objects);
  30. var primitives = [].concat(nullPrimitives, nonNullPrimitives);
  31. var nonPropertyKeys = [].concat(nullPrimitives, booleans, numbers, objects);
  32. var propertyKeys = [].concat(strings, symbols);
  33. var nonBooleans = [].concat(nullPrimitives, strings, symbols, numbers, objects);
  34. var falsies = [].concat(nullPrimitives, false, '', 0, -0, NaN);
  35. var truthies = [].concat(true, 'foo', 42, symbols, objects);
  36. var timestamps = [].concat(0, 946713600000, 1546329600000);
  37. var nonFunctions = [].concat(primitives, objects, [42]);
  38. var nonArrays = [].concat(nonFunctions);
  39. var descriptors = {
  40. configurable: function (descriptor) {
  41. return assign(assign({}, descriptor), { '[[Configurable]]': true });
  42. },
  43. nonConfigurable: function (descriptor) {
  44. return assign(assign({}, descriptor), { '[[Configurable]]': false });
  45. },
  46. enumerable: function (descriptor) {
  47. return assign(assign({}, descriptor), { '[[Enumerable]]': true });
  48. },
  49. nonEnumerable: function (descriptor) {
  50. return assign(assign({}, descriptor), { '[[Enumerable]]': false });
  51. },
  52. writable: function (descriptor) {
  53. return assign(assign({}, descriptor), { '[[Writable]]': true });
  54. },
  55. nonWritable: function (descriptor) {
  56. return assign(assign({}, descriptor), { '[[Writable]]': false });
  57. }
  58. };
  59. module.exports = {
  60. coercibleObject: coercibleObject,
  61. coercibleFnObject: coercibleFnObject,
  62. valueOfOnlyObject: valueOfOnlyObject,
  63. toStringOnlyObject: toStringOnlyObject,
  64. uncoercibleObject: uncoercibleObject,
  65. uncoercibleFnObject: uncoercibleFnObject,
  66. objects: objects,
  67. nonFunctions: nonFunctions,
  68. nonArrays: nonArrays,
  69. nullPrimitives: nullPrimitives,
  70. numbers: numbers,
  71. zeroes: zeroes,
  72. infinities: infinities,
  73. strings: strings,
  74. booleans: booleans,
  75. symbols: symbols,
  76. hasSymbols: hasSymbols,
  77. nonSymbolPrimitives: nonSymbolPrimitives,
  78. nonNumberPrimitives: nonNumberPrimitives,
  79. nonNullPrimitives: nonNullPrimitives,
  80. nonUndefinedPrimitives: nonUndefinedPrimitives,
  81. nonStrings: nonStrings,
  82. nonNumbers: nonNumberPrimitives.concat(objects),
  83. nonIntegerNumbers: nonIntegerNumbers,
  84. primitives: primitives,
  85. nonPropertyKeys: nonPropertyKeys,
  86. propertyKeys: propertyKeys,
  87. nonBooleans: nonBooleans,
  88. falsies: falsies,
  89. truthies: truthies,
  90. timestamps: timestamps,
  91. bothDescriptor: function () {
  92. return { '[[Get]]': function () {}, '[[Value]]': true };
  93. },
  94. bothDescriptorWritable: function () {
  95. return descriptors.writable({ '[[Get]]': function () {} });
  96. },
  97. accessorDescriptor: function (value) {
  98. return descriptors.enumerable(descriptors.configurable({
  99. '[[Get]]': function get() { return value; }
  100. }));
  101. },
  102. mutatorDescriptor: function () {
  103. return descriptors.enumerable(descriptors.configurable({
  104. '[[Set]]': function () {}
  105. }));
  106. },
  107. dataDescriptor: function (value) {
  108. return descriptors.nonWritable({
  109. '[[Value]]': arguments.length > 0 ? value : 42
  110. });
  111. },
  112. genericDescriptor: function () {
  113. return descriptors.configurable(descriptors.nonEnumerable());
  114. },
  115. descriptors: descriptors
  116. };