source: imaps-frontend/node_modules/es-abstract/2015/Canonicalize.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.2 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var callBound = require('call-bind/callBound');
6var hasOwn = require('hasown');
7
8var $charCodeAt = callBound('String.prototype.charCodeAt');
9var $toUpperCase = callBound('String.prototype.toUpperCase');
10
11var caseFolding = require('../helpers/caseFolding.json');
12
13// https://262.ecma-international.org/6.0/#sec-runtime-semantics-canonicalize-ch
14
15module.exports = function Canonicalize(ch, IgnoreCase, Unicode) {
16 if (typeof ch !== 'string') {
17 throw new $TypeError('Assertion failed: `ch` must be a character');
18 }
19
20 if (typeof IgnoreCase !== 'boolean' || typeof Unicode !== 'boolean') {
21 throw new $TypeError('Assertion failed: `IgnoreCase` and `Unicode` must be Booleans');
22 }
23
24 if (!IgnoreCase) {
25 return ch; // step 1
26 }
27
28 if (Unicode) { // step 2
29 if (hasOwn(caseFolding.C, ch)) {
30 return caseFolding.C[ch];
31 }
32 if (hasOwn(caseFolding.S, ch)) {
33 return caseFolding.S[ch];
34 }
35 return ch; // step 2.b
36 }
37
38 var u = $toUpperCase(ch); // step 2
39
40 if (u.length !== 1) {
41 return ch; // step 3
42 }
43
44 var cu = u; // step 4
45
46 if ($charCodeAt(ch, 0) >= 128 && $charCodeAt(cu, 0) < 128) {
47 return ch; // step 5
48 }
49
50 return cu;
51};
Note: See TracBrowser for help on using the repository browser.