| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('get-intrinsic'); |
| 4 | |
| 5 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 6 | |
| 7 | var callBound = require('call-bind/callBound'); |
| 8 | |
| 9 | var $charCodeAt = callBound('String.prototype.charCodeAt'); |
| 10 | var $numberToString = callBound('Number.prototype.toString'); |
| 11 | var $toLowerCase = callBound('String.prototype.toLowerCase'); |
| 12 | |
| 13 | var StringPad = require('./StringPad'); |
| 14 | |
| 15 | // https://262.ecma-international.org/11.0/#sec-unicodeescape |
| 16 | |
| 17 | module.exports = function UnicodeEscape(C) { |
| 18 | if (typeof C !== 'string' || C.length !== 1) { |
| 19 | throw new $TypeError('Assertion failed: `C` must be a single code unit'); |
| 20 | } |
| 21 | var n = $charCodeAt(C, 0); |
| 22 | if (n > 0xFFFF) { |
| 23 | throw new $TypeError('`Assertion failed: numeric value of `C` must be <= 0xFFFF'); |
| 24 | } |
| 25 | |
| 26 | return '\\u' + StringPad($toLowerCase($numberToString(n, 16)), 4, '0', 'start'); |
| 27 | }; |