1
|
'use strict';
|
2
|
|
3
|
var GetIntrinsic = require('../GetIntrinsic');
|
4
|
|
5
|
var IsInteger = require('./IsInteger');
|
6
|
var Type = require('./Type');
|
7
|
|
8
|
var MAX_SAFE_INTEGER = require('../helpers/maxSafeInteger');
|
9
|
|
10
|
var $TypeError = GetIntrinsic('%TypeError%');
|
11
|
|
12
|
var $charCodeAt = require('../helpers/callBound')('String.prototype.charCodeAt');
|
13
|
|
14
|
// https://ecma-international.org/ecma-262/6.0/#sec-advancestringindex
|
15
|
|
16
|
module.exports = function AdvanceStringIndex(S, index, unicode) {
|
17
|
if (Type(S) !== 'String') {
|
18
|
throw new $TypeError('Assertion failed: `S` must be a String');
|
19
|
}
|
20
|
if (!IsInteger(index) || index < 0 || index > MAX_SAFE_INTEGER) {
|
21
|
throw new $TypeError('Assertion failed: `length` must be an integer >= 0 and <= 2**53');
|
22
|
}
|
23
|
if (Type(unicode) !== 'Boolean') {
|
24
|
throw new $TypeError('Assertion failed: `unicode` must be a Boolean');
|
25
|
}
|
26
|
if (!unicode) {
|
27
|
return index + 1;
|
28
|
}
|
29
|
var length = S.length;
|
30
|
if ((index + 1) >= length) {
|
31
|
return index + 1;
|
32
|
}
|
33
|
|
34
|
var first = $charCodeAt(S, index);
|
35
|
if (first < 0xD800 || first > 0xDBFF) {
|
36
|
return index + 1;
|
37
|
}
|
38
|
|
39
|
var second = $charCodeAt(S, index + 1);
|
40
|
if (second < 0xDC00 || second > 0xDFFF) {
|
41
|
return index + 1;
|
42
|
}
|
43
|
|
44
|
return index + 2;
|
45
|
};
|