source: imaps-frontend/node_modules/es-abstract/2020/QuoteJSONString.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var callBound = require('call-bind/callBound');
6var forEach = require('../helpers/forEach');
7var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
8var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
9
10var $charCodeAt = callBound('String.prototype.charCodeAt');
11
12var UnicodeEscape = require('./UnicodeEscape');
13var UTF16DecodeString = require('./UTF16DecodeString');
14var UTF16Encoding = require('./UTF16Encoding');
15
16var hasOwn = require('hasown');
17
18// https://262.ecma-international.org/11.0/#sec-quotejsonstring
19
20var escapes = {
21 '\u0008': '\\b',
22 '\u0009': '\\t',
23 '\u000A': '\\n',
24 '\u000C': '\\f',
25 '\u000D': '\\r',
26 '\u0022': '\\"',
27 '\u005c': '\\\\'
28};
29
30module.exports = function QuoteJSONString(value) {
31 if (typeof value !== 'string') {
32 throw new $TypeError('Assertion failed: `value` must be a String');
33 }
34 var product = '"';
35 if (value) {
36 forEach(UTF16DecodeString(value), function (C) {
37 if (hasOwn(escapes, C)) {
38 product += escapes[C];
39 } else {
40 var cCharCode = $charCodeAt(C, 0);
41 if (cCharCode < 0x20 || isLeadingSurrogate(C) || isTrailingSurrogate(C)) {
42 product += UnicodeEscape(C);
43 } else {
44 product += UTF16Encoding(cCharCode);
45 }
46 }
47 });
48 }
49 product += '"';
50 return product;
51};
Note: See TracBrowser for help on using the repository browser.