source: imaps-frontend/node_modules/es-abstract/2023/QuoteJSONString.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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