Canonicalize.js 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var $charCodeAt = callBound('String.prototype.charCodeAt');
  6. var $toUpperCase = callBound('String.prototype.toUpperCase');
  7. var Type = require('./Type');
  8. // https://262.ecma-international.org/5.1/#sec-15.10.2.8
  9. module.exports = function Canonicalize(ch, IgnoreCase) {
  10. if (Type(ch) !== 'String' || ch.length !== 1) {
  11. throw new $TypeError('Assertion failed: `ch` must be a character');
  12. }
  13. if (Type(IgnoreCase) !== 'Boolean') {
  14. throw new $TypeError('Assertion failed: `IgnoreCase` must be a Boolean');
  15. }
  16. if (!IgnoreCase) {
  17. return ch; // step 1
  18. }
  19. var u = $toUpperCase(ch); // step 2
  20. if (u.length !== 1) {
  21. return ch; // step 3
  22. }
  23. var cu = u; // step 4
  24. if ($charCodeAt(ch, 0) >= 128 && $charCodeAt(cu, 0) < 128) {
  25. return ch; // step 5
  26. }
  27. return cu;
  28. };