Canonicalize.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var has = require('has');
  6. var $charCodeAt = callBound('String.prototype.charCodeAt');
  7. var $toUpperCase = callBound('String.prototype.toUpperCase');
  8. var Type = require('./Type');
  9. var assertRecord = require('../helpers/assertRecord');
  10. var caseFolding = require('../helpers/caseFolding');
  11. // https://262.ecma-international.org/14.0/#sec-runtime-semantics-canonicalize-ch
  12. module.exports = function Canonicalize(rer, ch) {
  13. assertRecord(Type, 'RegExp Record', 'rer', rer);
  14. if (Type(ch) !== 'String') {
  15. throw new $TypeError('Assertion failed: `ch` must be a character');
  16. }
  17. if (rer['[[Unicode]]'] && rer['[[IgnoreCase]]']) { // step 1
  18. if (has(caseFolding.C, ch)) {
  19. return caseFolding.C[ch];
  20. }
  21. if (has(caseFolding.S, ch)) {
  22. return caseFolding.S[ch];
  23. }
  24. return ch; // step 1.b
  25. }
  26. if (!rer['[[IgnoreCase]]']) {
  27. return ch; // step 2
  28. }
  29. var u = $toUpperCase(ch); // step 5
  30. if (u.length !== 1) {
  31. return ch; // step 7
  32. }
  33. var cu = u; // step 8
  34. if ($charCodeAt(ch, 0) >= 128 && $charCodeAt(cu, 0) < 128) {
  35. return ch; // step 9
  36. }
  37. return cu; // step 10
  38. };