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