blob: db7163f77ee22a384534802b61c8f54879507124 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = GetIntrinsic('%TypeError%');
6
7var callBound = require('call-bind/callBound');
8
9var $charCodeAt = callBound('String.prototype.charCodeAt');
10var $numberToString = callBound('Number.prototype.toString');
11var $toLowerCase = callBound('String.prototype.toLowerCase');
12var $strSlice = callBound('String.prototype.slice');
13
14// https://262.ecma-international.org/9.0/#sec-unicodeescape
15
16module.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};