values.js 4.8 KB

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