source: imaps-frontend/node_modules/es-abstract/2020/CodePointAt.js

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4var callBound = require('call-bind/callBound');
5var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
6var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
7
8var UTF16DecodeSurrogatePair = require('./UTF16DecodeSurrogatePair');
9
10var $charAt = callBound('String.prototype.charAt');
11var $charCodeAt = callBound('String.prototype.charCodeAt');
12
13// https://262.ecma-international.org/11.0/#sec-codepointat
14
15module.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};
Note: See TracBrowser for help on using the repository browser.