[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 | var callBound = require('call-bind/callBound');
|
---|
| 5 | var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
|
---|
| 6 | var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
|
---|
| 7 |
|
---|
| 8 | var UTF16DecodeSurrogatePair = require('./UTF16DecodeSurrogatePair');
|
---|
| 9 |
|
---|
| 10 | var $charAt = callBound('String.prototype.charAt');
|
---|
| 11 | var $charCodeAt = callBound('String.prototype.charCodeAt');
|
---|
| 12 |
|
---|
| 13 | // https://262.ecma-international.org/11.0/#sec-codepointat
|
---|
| 14 |
|
---|
| 15 | module.exports = function CodePointAt(string, position) {
|
---|
| 16 | if (typeof string !== 'string') {
|
---|
| 17 | throw new $TypeError('Assertion failed: `string` must be a String');
|
---|
| 18 | }
|
---|
| 19 | var size = string.length;
|
---|
| 20 | if (position < 0 || position >= size) {
|
---|
| 21 | throw new $TypeError('Assertion failed: `position` must be >= 0, and < the length of `string`');
|
---|
| 22 | }
|
---|
| 23 | var first = $charCodeAt(string, position);
|
---|
| 24 | var cp = $charAt(string, position);
|
---|
| 25 | var firstIsLeading = isLeadingSurrogate(first);
|
---|
| 26 | var firstIsTrailing = isTrailingSurrogate(first);
|
---|
| 27 | if (!firstIsLeading && !firstIsTrailing) {
|
---|
| 28 | return {
|
---|
| 29 | '[[CodePoint]]': cp,
|
---|
| 30 | '[[CodeUnitCount]]': 1,
|
---|
| 31 | '[[IsUnpairedSurrogate]]': false
|
---|
| 32 | };
|
---|
| 33 | }
|
---|
| 34 | if (firstIsTrailing || (position + 1 === size)) {
|
---|
| 35 | return {
|
---|
| 36 | '[[CodePoint]]': cp,
|
---|
| 37 | '[[CodeUnitCount]]': 1,
|
---|
| 38 | '[[IsUnpairedSurrogate]]': true
|
---|
| 39 | };
|
---|
| 40 | }
|
---|
| 41 | var second = $charCodeAt(string, position + 1);
|
---|
| 42 | if (!isTrailingSurrogate(second)) {
|
---|
| 43 | return {
|
---|
| 44 | '[[CodePoint]]': cp,
|
---|
| 45 | '[[CodeUnitCount]]': 1,
|
---|
| 46 | '[[IsUnpairedSurrogate]]': true
|
---|
| 47 | };
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return {
|
---|
| 51 | '[[CodePoint]]': UTF16DecodeSurrogatePair(first, second),
|
---|
| 52 | '[[CodeUnitCount]]': 2,
|
---|
| 53 | '[[IsUnpairedSurrogate]]': false
|
---|
| 54 | };
|
---|
| 55 | };
|
---|