StringGetOwnProperty.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var $charAt = callBound('String.prototype.charAt');
  6. var $stringToString = callBound('String.prototype.toString');
  7. var CanonicalNumericIndexString = require('./CanonicalNumericIndexString');
  8. var IsIntegralNumber = require('./IsIntegralNumber');
  9. var IsPropertyKey = require('./IsPropertyKey');
  10. var Type = require('./Type');
  11. var isNegativeZero = require('is-negative-zero');
  12. // https://ecma-international.org/ecma-262/12.0/#sec-stringgetownproperty
  13. module.exports = function StringGetOwnProperty(S, P) {
  14. var str;
  15. if (Type(S) === 'Object') {
  16. try {
  17. str = $stringToString(S);
  18. } catch (e) { /**/ }
  19. }
  20. if (Type(str) !== 'String') {
  21. throw new $TypeError('Assertion failed: `S` must be a boxed string object');
  22. }
  23. if (!IsPropertyKey(P)) {
  24. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  25. }
  26. if (Type(P) !== 'String') {
  27. return void undefined;
  28. }
  29. var index = CanonicalNumericIndexString(P);
  30. var len = str.length;
  31. if (typeof index === 'undefined' || !IsIntegralNumber(index) || isNegativeZero(index) || index < 0 || len <= index) {
  32. return void undefined;
  33. }
  34. var resultStr = $charAt(S, index);
  35. return {
  36. '[[Configurable]]': false,
  37. '[[Enumerable]]': true,
  38. '[[Value]]': resultStr,
  39. '[[Writable]]': false
  40. };
  41. };