blob: b6f904ec3e667e056a3f776201e57c91e540c673 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = GetIntrinsic('%TypeError%');
6
7var callBound = require('call-bind/callBound');
8var $charAt = callBound('String.prototype.charAt');
9var $stringToString = callBound('String.prototype.toString');
10
11var CanonicalNumericIndexString = require('./CanonicalNumericIndexString');
12var IsInteger = require('./IsInteger');
13var IsPropertyKey = require('./IsPropertyKey');
14var Type = require('./Type');
15
16var isNegativeZero = require('is-negative-zero');
17
18// https://262.ecma-international.org/8.0/#sec-stringgetownproperty
19
20module.exports = function StringGetOwnProperty(S, P) {
21 var str;
22 if (Type(S) === 'Object') {
23 try {
24 str = $stringToString(S);
25 } catch (e) { /**/ }
26 }
27 if (Type(str) !== 'String') {
28 throw new $TypeError('Assertion failed: `S` must be a boxed string object');
29 }
30 if (!IsPropertyKey(P)) {
31 throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
32 }
33 if (Type(P) !== 'String') {
34 return void undefined;
35 }
36 var index = CanonicalNumericIndexString(P);
37 var len = str.length;
38 if (typeof index === 'undefined' || !IsInteger(index) || isNegativeZero(index) || index < 0 || len <= index) {
39 return void undefined;
40 }
41 var resultStr = $charAt(S, index);
42 return {
43 '[[Configurable]]': false,
44 '[[Enumerable]]': true,
45 '[[Value]]': resultStr,
46 '[[Writable]]': false
47 };
48};