blob: ffadb554441f232669e113e5ca128bd30b5646ba [file] [log] [blame]
Tim van der Lippe2c891972021-07-29 15:22:501'use strict';
2
Tim van der Lippe2c891972021-07-29 15:22:503var callBound = require('call-bind/callBound');
4
Nikolay Vitkovd76576c2024-12-02 14:10:155var $TypeError = require('es-errors/type');
Tim van der Lippe2c891972021-07-29 15:22:506
7var $charAt = callBound('String.prototype.charAt');
8
9var isString = require('is-string');
10var isNegativeZero = require('is-negative-zero');
11var unbox = require('unbox-primitive');
12
13var CanonicalNumericIndexString = require('./CanonicalNumericIndexString');
14var IsInteger = require('./IsInteger');
15var IsPropertyKey = require('./IsPropertyKey');
Tim van der Lippe2c891972021-07-29 15:22:5016
17// https://262.ecma-international.org/6.0/#sec-stringgetindexproperty
18
19module.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 Vitkovd76576c2024-12-02 14:10:1527 if (typeof P !== 'string') {
Tim van der Lippe2c891972021-07-29 15:22:5028 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};