Canonicalize.js 854 B

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