es2015.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
  3. var isPrimitive = require('./helpers/isPrimitive');
  4. var isCallable = require('is-callable');
  5. var isDate = require('is-date-object');
  6. var isSymbol = require('is-symbol');
  7. /** @type {(O: { valueOf?: () => unknown, toString?: () => unknown }, hint: 'number' | 'string' | 'default') => null | undefined | string | symbol | number | boolean | bigint} */
  8. var ordinaryToPrimitive = function OrdinaryToPrimitive(O, hint) {
  9. if (typeof O === 'undefined' || O === null) {
  10. throw new TypeError('Cannot call method on ' + O);
  11. }
  12. if (typeof hint !== 'string' || (hint !== 'number' && hint !== 'string')) {
  13. throw new TypeError('hint must be "string" or "number"');
  14. }
  15. /** @type {('toString' | 'valueOf')[]} */
  16. var methodNames = hint === 'string' ? ['toString', 'valueOf'] : ['valueOf', 'toString'];
  17. var method, result, i;
  18. for (i = 0; i < methodNames.length; ++i) {
  19. method = O[methodNames[i]];
  20. if (isCallable(method)) {
  21. result = method.call(O);
  22. if (isPrimitive(result)) {
  23. return result;
  24. }
  25. }
  26. }
  27. throw new TypeError('No default value');
  28. };
  29. /** @type {<K extends PropertyKey>(O: Record<K, unknown>, P: K) => Function | undefined} */
  30. var GetMethod = function GetMethod(O, P) {
  31. var func = O[P];
  32. if (func !== null && typeof func !== 'undefined') {
  33. if (!isCallable(func)) {
  34. throw new TypeError(func + ' returned for property ' + String(P) + ' of object ' + O + ' is not a function');
  35. }
  36. return func;
  37. }
  38. return void 0;
  39. };
  40. /** @type {import('./es2015')} */
  41. // http://www.ecma-international.org/ecma-262/6.0/#sec-toprimitive
  42. module.exports = function ToPrimitive(input) {
  43. if (isPrimitive(input)) {
  44. return input;
  45. }
  46. /** @type {'default' | 'string' | 'number'} */
  47. var hint = 'default';
  48. if (arguments.length > 1) {
  49. if (arguments[1] === String) {
  50. hint = 'string';
  51. } else if (arguments[1] === Number) {
  52. hint = 'number';
  53. }
  54. }
  55. var exoticToPrim;
  56. if (hasSymbols) {
  57. if (Symbol.toPrimitive) {
  58. // eslint-disable-next-line no-extra-parens
  59. exoticToPrim = GetMethod(/** @type {Record<PropertyKey, unknown>} */ (input), Symbol.toPrimitive);
  60. } else if (isSymbol(input)) {
  61. exoticToPrim = Symbol.prototype.valueOf;
  62. }
  63. }
  64. if (typeof exoticToPrim !== 'undefined') {
  65. var result = exoticToPrim.call(input, hint);
  66. if (isPrimitive(result)) {
  67. return result;
  68. }
  69. throw new TypeError('unable to convert exotic object to primitive');
  70. }
  71. if (hint === 'default' && (isDate(input) || isSymbol(input))) {
  72. hint = 'string';
  73. }
  74. // eslint-disable-next-line no-extra-parens
  75. return ordinaryToPrimitive(/** @type {object} */ (input), hint === 'default' ? 'number' : hint);
  76. };