source: frontend/node_modules/es-abstract/helpers/CharSet.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.3 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var GetIntrinsic = require('get-intrinsic');
6var callBound = require('call-bound');
7var hasOwn = require('hasown');
8
9var caseFolding = require('./caseFolding.json');
10var IsArray = require('./IsArray');
11var isLeadingSurrogate = require('./isLeadingSurrogate');
12var isTrailingSurrogate = require('./isTrailingSurrogate');
13
14var $charCodeAt = callBound('%String.prototype.charCodeAt%');
15var $fromCharCode = GetIntrinsic('%String.fromCharCode%');
16
17/* eslint func-style: 0 */
18
19function CharSet(test, yieldCh) {
20 if (typeof test !== 'function') {
21 throw new $TypeError('Assertion failed: `test` must be a function');
22 }
23 if (typeof yieldCh !== 'function') {
24 throw new $TypeError('Assertion failed: `yield` must be a function');
25 }
26 this.test = test;
27 this.yield = yieldCh;
28}
29CharSet.prototype.count = function () {
30 var count = 0;
31 this.yield(function () { count += 1; });
32 return count;
33};
34
35function testCodeUnits(CharSetElement) {
36 if (typeof CharSetElement !== 'string') {
37 throw new $TypeError('Assertion failed: `CharSetElement` must be a string');
38 }
39 return CharSetElement.length !== 1;
40}
41function yieldCodeUnits(emit) {
42 for (var i = 0; i <= 0xDFFF; i += 1) {
43 emit($fromCharCode(i));
44 }
45}
46
47function testCodePoints(CharSetElement) {
48 if (typeof CharSetElement !== 'string') {
49 throw new $TypeError('Assertion failed: `CharSetElement` must be a string');
50 }
51
52 if (CharSetElement.length === 1) {
53 return true;
54 }
55 if (CharSetElement.length === 2) {
56 var hi = $charCodeAt(CharSetElement, 0);
57 var lo = $charCodeAt(CharSetElement, 1);
58 return isLeadingSurrogate(hi) && isTrailingSurrogate(lo);
59 }
60
61 return false;
62}
63
64function yieldCodePoints(emit) {
65 for (var i = 0; i <= 0xDFFF; i += 1) {
66 emit($fromCharCode(i));
67 }
68 for (var u = 0x10000; u <= 0x10FFFF; u += 1) {
69 var cp = u - 0x10000;
70 var high = (cp >> 10) + 0xD800;
71 var low = (cp & 0x3FF) + 0xDC00;
72 emit($fromCharCode(high, low));
73 }
74}
75
76function charsToMap(chars) {
77 if (!IsArray(chars)) {
78 throw new $TypeError('Assertion failed: `chars` must be an array');
79 }
80
81 var map = { __proto__: null };
82 for (var i = 0; i < chars.length; i += 1) {
83 var char = chars[i];
84 if (typeof char !== 'string' || (char.length !== 1 && char.length !== 2)) {
85 throw new $TypeError('Assertion failed: `chars` must be an array of strings of length 1');
86 }
87 map[char] = true;
88 }
89 return map;
90}
91
92module.exports = {
93 CharSet: CharSet,
94 from: function from(chars) {
95 var map = charsToMap(chars);
96 return new CharSet(
97 function test(CharSetElement) {
98 return hasOwn(map, CharSetElement);
99 },
100 function yieldChar(emit) {
101 // eslint-disable-next-line no-restricted-syntax
102 for (var k in map) {
103 if (hasOwn(map, k)) {
104 emit(k);
105 }
106 }
107 }
108 );
109 },
110 getCodeUnits: function () {
111 return new CharSet(testCodeUnits, yieldCodeUnits);
112 },
113 getCodePoints: function () {
114 return new CharSet(testCodePoints, yieldCodePoints);
115 },
116 getNonSimpleCaseFoldingCodePoints: function () {
117 return new CharSet(
118 function test(CharSetElement) {
119 return testCodePoints(CharSetElement) && !hasOwn(caseFolding.S, CharSetElement);
120 },
121 function yieldChar(emit) {
122 yieldCodePoints(function (CharSetElement) {
123 if (!hasOwn(caseFolding.S, CharSetElement)) {
124 emit(CharSetElement);
125 }
126 });
127 }
128 );
129 }
130};
Note: See TracBrowser for help on using the repository browser.