Permalink
codeql-action/node_modules/es-abstract/2015/Canonicalize.js
Newer
100644
55 lines (39 sloc)
1.22 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
5
var $TypeError = GetIntrinsic('%TypeError%');
6
7
var callBound = require('call-bind/callBound');
8
var has = require('has');
9
10
var $charCodeAt = callBound('String.prototype.charCodeAt');
11
var $toUpperCase = callBound('String.prototype.toUpperCase');
12
13
var Type = require('./Type');
14
15
var caseFolding = require('../helpers/caseFolding');
16
17
// https://262.ecma-international.org/6.0/#sec-runtime-semantics-canonicalize-ch
18
19
module.exports = function Canonicalize(ch, IgnoreCase, Unicode) {
20
if (Type(ch) !== 'String') {
21
throw new $TypeError('Assertion failed: `ch` must be a character');
22
}
23
24
if (Type(IgnoreCase) !== 'Boolean' || Type(Unicode) !== 'Boolean') {
25
throw new $TypeError('Assertion failed: `IgnoreCase` and `Unicode` must be Booleans');
26
}
27
28
if (!IgnoreCase) {
29
return ch; // step 1
30
}
31
32
if (Unicode) { // step 2
33
if (has(caseFolding.C, ch)) {
34
return caseFolding.C[ch];
35
}
36
if (has(caseFolding.S, ch)) {
37
return caseFolding.S[ch];
38
}
39
return ch; // step 2.b
40
}
41
42
var u = $toUpperCase(ch); // step 2
43
44
if (u.length !== 1) {
45
return ch; // step 3
46
}
47
48
var cu = u; // step 4
49
50
if ($charCodeAt(ch, 0) >= 128 && $charCodeAt(cu, 0) < 128) {
51
return ch; // step 5
52
}
53
54
return cu;
55
};