blob: 0e62c4a4a4f4c2e97a4e5e6b439c4fb0bd57b45a [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var CodePointAt = require('./CodePointAt');
6var IsInteger = require('./IsInteger');
7var Type = require('./Type');
8
9var MAX_SAFE_INTEGER = require('../helpers/maxSafeInteger');
10
11var $TypeError = GetIntrinsic('%TypeError%');
12
13// https://262.ecma-international.org/6.0/#sec-advancestringindex
14
15module.exports = function AdvanceStringIndex(S, index, unicode) {
16 if (Type(S) !== 'String') {
17 throw new $TypeError('Assertion failed: `S` must be a String');
18 }
19 if (!IsInteger(index) || index < 0 || index > MAX_SAFE_INTEGER) {
20 throw new $TypeError('Assertion failed: `length` must be an integer >= 0 and <= 2**53');
21 }
22 if (Type(unicode) !== 'Boolean') {
23 throw new $TypeError('Assertion failed: `unicode` must be a Boolean');
24 }
25 if (!unicode) {
26 return index + 1;
27 }
28 var length = S.length;
29 if ((index + 1) >= length) {
30 return index + 1;
31 }
32 var cp = CodePointAt(S, index);
33 return index + cp['[[CodeUnitCount]]'];
34};