blob: 73043ddad82b822bb0a86689e9edf2b641290e83 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4var callBound = require('call-bind/callBound');
5
6var $TypeError = GetIntrinsic('%TypeError%');
7
8var $charAt = callBound('String.prototype.charAt');
9
10var isString = require('is-string');
11var isNegativeZero = require('is-negative-zero');
12var unbox = require('unbox-primitive');
13
14var CanonicalNumericIndexString = require('./CanonicalNumericIndexString');
15var IsInteger = require('./IsInteger');
16var IsPropertyKey = require('./IsPropertyKey');
17var Type = require('./Type');
18
19// https://262.ecma-international.org/6.0/#sec-stringgetindexproperty
20
21module.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};