Files
codeql-action/node_modules/es-abstract/2018/QuoteJSONString.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1007 B
JavaScript
Raw Normal View History

2020-09-14 10:42:37 +01:00
'use strict';
2024-09-16 17:29:58 +00:00
var $TypeError = require('es-errors/type');
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
var callBound = require('call-bind/callBound');
2020-09-14 10:42:37 +01:00
var forEach = require('../helpers/forEach');
var $charCodeAt = callBound('String.prototype.charCodeAt');
var $strSplit = callBound('String.prototype.split');
var UnicodeEscape = require('./UnicodeEscape');
2024-09-16 17:29:58 +00:00
var hasOwn = require('hasown');
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
// https://262.ecma-international.org/9.0/#sec-quotejsonstring
2020-09-14 10:42:37 +01:00
var escapes = {
'\u0008': '\\b',
'\u0009': '\\t',
'\u000A': '\\n',
'\u000C': '\\f',
'\u000D': '\\r',
'\u0022': '\\"',
'\u005c': '\\\\'
};
module.exports = function QuoteJSONString(value) {
2024-09-16 17:29:58 +00:00
if (typeof value !== 'string') {
2020-09-14 10:42:37 +01:00
throw new $TypeError('Assertion failed: `value` must be a String');
}
var product = '"';
if (value) {
forEach($strSplit(value), function (C) {
2024-09-16 17:29:58 +00:00
if (hasOwn(escapes, C)) {
2020-09-14 10:42:37 +01:00
product += escapes[C];
} else if ($charCodeAt(C, 0) < 0x20) {
product += UnicodeEscape(C);
} else {
product += C;
}
});
}
product += '"';
return product;
};