UnicodeEscape.js 839 B

123456789101112131415161718192021222324252627
  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 StringPad = require('./StringPad');
  9. // https://262.ecma-international.org/11.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' + StringPad($toLowerCase($numberToString(n, 16)), 4, '0', 'start');
  19. };