values.js 4.4 KB

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