|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Rev | Line | |
|---|
| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 |
|
|---|
| 5 | var callBound = require('call-bound');
|
|---|
| 6 | var forEach = require('../helpers/forEach');
|
|---|
| 7 | var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
|
|---|
| 8 | var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
|
|---|
| 9 |
|
|---|
| 10 | var $charCodeAt = callBound('String.prototype.charCodeAt');
|
|---|
| 11 | var $strSplit = callBound('String.prototype.split');
|
|---|
| 12 |
|
|---|
| 13 | var UnicodeEscape = require('./UnicodeEscape');
|
|---|
| 14 | var UTF16DecodeString = require('./UTF16DecodeString');
|
|---|
| 15 | var UTF16Encoding = require('./UTF16Encoding');
|
|---|
| 16 |
|
|---|
| 17 | var hasOwn = require('hasown');
|
|---|
| 18 |
|
|---|
| 19 | // https://262.ecma-international.org/11.0/#sec-quotejsonstring
|
|---|
| 20 |
|
|---|
| 21 | var escapes = {
|
|---|
| 22 | '\u0008': '\\b',
|
|---|
| 23 | '\u0009': '\\t',
|
|---|
| 24 | '\u000A': '\\n',
|
|---|
| 25 | '\u000C': '\\f',
|
|---|
| 26 | '\u000D': '\\r',
|
|---|
| 27 | '\u0022': '\\"',
|
|---|
| 28 | '\u005c': '\\\\'
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | module.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) {
|
|---|
| 37 | forEach($strSplit(UTF16DecodeString(value), ''), function (C) {
|
|---|
| 38 | if (hasOwn(escapes, C)) {
|
|---|
| 39 | product += escapes[C];
|
|---|
| 40 | } else {
|
|---|
| 41 | var cCharCode = $charCodeAt(C, 0);
|
|---|
| 42 | if (cCharCode < 0x20 || isLeadingSurrogate(cCharCode) || isTrailingSurrogate(cCharCode)) {
|
|---|
| 43 | product += UnicodeEscape(C);
|
|---|
| 44 | } else {
|
|---|
| 45 | product += UTF16Encoding(cCharCode);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | });
|
|---|
| 49 | }
|
|---|
| 50 | product += '"';
|
|---|
| 51 | return product;
|
|---|
| 52 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.