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
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
[79a0317] | 5 | var callBound = require('call-bound');
|
---|
[d565449] | 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');
|
---|
[79a0317] | 11 | var $strSplit = callBound('String.prototype.split');
|
---|
[d565449] | 12 |
|
---|
| 13 | var StringToCodePoints = require('./StringToCodePoints');
|
---|
| 14 | var UnicodeEscape = require('./UnicodeEscape');
|
---|
| 15 | var UTF16EncodeCodePoint = require('./UTF16EncodeCodePoint');
|
---|
| 16 |
|
---|
| 17 | var hasOwn = require('hasown');
|
---|
| 18 |
|
---|
| 19 | // https://262.ecma-international.org/12.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) {
|
---|
[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.