| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('get-intrinsic'); |
| 4 | |
| 5 | var CodePointAt = require('./CodePointAt'); |
| 6 | var IsInteger = require('./IsInteger'); |
| 7 | var Type = require('./Type'); |
| 8 | |
| 9 | var MAX_SAFE_INTEGER = require('../helpers/maxSafeInteger'); |
| 10 | |
| 11 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 12 | |
| 13 | // https://262.ecma-international.org/6.0/#sec-advancestringindex |
| 14 | |
| 15 | module.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 | }; |