StringGetIndexProperty.js 1.3 KB

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