main
Last change
on this file since d565449 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
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var callBound = require('call-bind/callBound');
|
---|
| 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 |
|
---|
| 12 | var StringToCodePoints = require('./StringToCodePoints');
|
---|
| 13 | var UnicodeEscape = require('./UnicodeEscape');
|
---|
| 14 | var UTF16EncodeCodePoint = require('./UTF16EncodeCodePoint');
|
---|
| 15 |
|
---|
| 16 | var hasOwn = require('hasown');
|
---|
| 17 |
|
---|
| 18 | // https://262.ecma-international.org/12.0/#sec-quotejsonstring
|
---|
| 19 |
|
---|
| 20 | var escapes = {
|
---|
| 21 | '\u0008': '\\b',
|
---|
| 22 | '\u0009': '\\t',
|
---|
| 23 | '\u000A': '\\n',
|
---|
| 24 | '\u000C': '\\f',
|
---|
| 25 | '\u000D': '\\r',
|
---|
| 26 | '\u0022': '\\"',
|
---|
| 27 | '\u005c': '\\\\'
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | module.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(StringToCodePoints(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 += UTF16EncodeCodePoint(cCharCode);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | });
|
---|
| 48 | }
|
---|
| 49 | product += '"';
|
---|
| 50 | return product;
|
---|
| 51 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.