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