blob: b708c90df0a05e9250f3dbbb9ac1044bb88b5d42 [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');
12
13var StringPad = require('./StringPad');
14
15// https://262.ecma-international.org/11.0/#sec-unicodeescape
16
17module.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};