UnicodeEscape.js 791 B

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