UnicodeEscape.js 846 B

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