StringGetOwnProperty.js 1.3 KB

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