source: imaps-frontend/node_modules/es-abstract/2016/QuoteJSONString.js@ d565449

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
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var callBound = require('call-bind/callBound');
6var forEach = require('../helpers/forEach');
7
8var $charCodeAt = callBound('String.prototype.charCodeAt');
9var $numberToString = callBound('Number.prototype.toString');
10var $toLowerCase = callBound('String.prototype.toLowerCase');
11var $strSlice = callBound('String.prototype.slice');
12var $strSplit = callBound('String.prototype.split');
13
14// https://262.ecma-international.org/6.0/#sec-quotejsonstring
15
16var escapes = {
17 '\u0008': 'b',
18 '\u000C': 'f',
19 '\u000A': 'n',
20 '\u000D': 'r',
21 '\u0009': 't'
22};
23
24module.exports = function QuoteJSONString(value) {
25 if (typeof value !== 'string') {
26 throw new $TypeError('Assertion failed: `value` must be a String');
27 }
28 var product = '"';
29 if (value) {
30 forEach($strSplit(value), function (C) {
31 if (C === '"' || C === '\\') {
32 product += '\u005C' + C;
33 } else if (C === '\u0008' || C === '\u000C' || C === '\u000A' || C === '\u000D' || C === '\u0009') {
34 var abbrev = escapes[C];
35 product += '\u005C' + abbrev;
36 } else {
37 var cCharCode = $charCodeAt(C, 0);
38 if (cCharCode < 0x20) {
39 product += '\u005Cu' + $toLowerCase($strSlice('0000' + $numberToString(cCharCode, 16), -4));
40 } else {
41 product += C;
42 }
43 }
44 });
45 }
46 product += '"';
47 return product;
48};
Note: See TracBrowser for help on using the repository browser.