StringGetIndexProperty.js 1.3 KB

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