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:
1007 bytes
|
Line | |
---|
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 |
|
---|
8 | var $charCodeAt = callBound('String.prototype.charCodeAt');
|
---|
9 | var $strSplit = callBound('String.prototype.split');
|
---|
10 |
|
---|
11 | var UnicodeEscape = require('./UnicodeEscape');
|
---|
12 |
|
---|
13 | var hasOwn = require('hasown');
|
---|
14 |
|
---|
15 | // https://262.ecma-international.org/9.0/#sec-quotejsonstring
|
---|
16 |
|
---|
17 | var escapes = {
|
---|
18 | '\u0008': '\\b',
|
---|
19 | '\u0009': '\\t',
|
---|
20 | '\u000A': '\\n',
|
---|
21 | '\u000C': '\\f',
|
---|
22 | '\u000D': '\\r',
|
---|
23 | '\u0022': '\\"',
|
---|
24 | '\u005c': '\\\\'
|
---|
25 | };
|
---|
26 |
|
---|
27 | module.exports = function QuoteJSONString(value) {
|
---|
28 | if (typeof value !== 'string') {
|
---|
29 | throw new $TypeError('Assertion failed: `value` must be a String');
|
---|
30 | }
|
---|
31 | var product = '"';
|
---|
32 | if (value) {
|
---|
33 | forEach($strSplit(value), function (C) {
|
---|
34 | if (hasOwn(escapes, C)) {
|
---|
35 | product += escapes[C];
|
---|
36 | } else if ($charCodeAt(C, 0) < 0x20) {
|
---|
37 | product += UnicodeEscape(C);
|
---|
38 | } else {
|
---|
39 | product += C;
|
---|
40 | }
|
---|
41 | });
|
---|
42 | }
|
---|
43 | product += '"';
|
---|
44 | return product;
|
---|
45 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.