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