source: imaps-frontend/node_modules/es-abstract/2019/AdvanceStringIndex.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
Line 
1'use strict';
2
3var isInteger = require('../helpers/isInteger');
4var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
5var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
6var MAX_SAFE_INTEGER = require('../helpers/maxSafeInteger');
7
8var $TypeError = require('es-errors/type');
9
10var $charCodeAt = require('call-bind/callBound')('String.prototype.charCodeAt');
11
12// https://262.ecma-international.org/6.0/#sec-advancestringindex
13
14module.exports = function AdvanceStringIndex(S, index, unicode) {
15 if (typeof S !== 'string') {
16 throw new $TypeError('Assertion failed: `S` must be a String');
17 }
18 if (!isInteger(index) || index < 0 || index > MAX_SAFE_INTEGER) {
19 throw new $TypeError('Assertion failed: `length` must be an integer >= 0 and <= 2**53');
20 }
21 if (typeof unicode !== 'boolean') {
22 throw new $TypeError('Assertion failed: `unicode` must be a Boolean');
23 }
24 if (!unicode) {
25 return index + 1;
26 }
27 var length = S.length;
28 if ((index + 1) >= length) {
29 return index + 1;
30 }
31
32 var first = $charCodeAt(S, index);
33 if (!isLeadingSurrogate(first)) {
34 return index + 1;
35 }
36
37 var second = $charCodeAt(S, index + 1);
38 if (!isTrailingSurrogate(second)) {
39 return index + 1;
40 }
41
42 return index + 2;
43};
Note: See TracBrowser for help on using the repository browser.