source: imaps-frontend/node_modules/es-abstract/2021/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
RevLine 
[d565449]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 UTF16SurrogatePairToCodePoint = require('./UTF16SurrogatePairToCodePoint');
9
10var $charAt = callBound('String.prototype.charAt');
11var $charCodeAt = callBound('String.prototype.charCodeAt');
12
13// https://262.ecma-international.org/12.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]]': UTF16SurrogatePairToCodePoint(first, second),
52 '[[CodeUnitCount]]': 2,
53 '[[IsUnpairedSurrogate]]': false
54 };
55};
Note: See TracBrowser for help on using the repository browser.