UnicodeEscape.js 798 B

123456789101112131415161718192021222324
  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 $numberToString = callBound('Number.prototype.toString');
  6. var $toLowerCase = callBound('String.prototype.toLowerCase');
  7. var $strSlice = callBound('String.prototype.slice');
  8. // https://262.ecma-international.org/9.0/#sec-unicodeescape
  9. module.exports = function UnicodeEscape(C) {
  10. if (typeof C !== 'string' || C.length !== 1) {
  11. throw new $TypeError('Assertion failed: `C` must be a single code unit');
  12. }
  13. var n = $charCodeAt(C, 0);
  14. if (n > 0xFFFF) {
  15. throw new $TypeError('`Assertion failed: numeric value of `C` must be <= 0xFFFF');
  16. }
  17. return '\\u' + $strSlice('0000' + $toLowerCase($numberToString(n, 16)), -4);
  18. };