1 | var $ = require('../internals/export');
|
---|
2 | var getBuiltIn = require('../internals/get-built-in');
|
---|
3 | var fails = require('../internals/fails');
|
---|
4 |
|
---|
5 | var $stringify = getBuiltIn('JSON', 'stringify');
|
---|
6 | var re = /[\uD800-\uDFFF]/g;
|
---|
7 | var low = /^[\uD800-\uDBFF]$/;
|
---|
8 | var hi = /^[\uDC00-\uDFFF]$/;
|
---|
9 |
|
---|
10 | var fix = function (match, offset, string) {
|
---|
11 | var prev = string.charAt(offset - 1);
|
---|
12 | var next = string.charAt(offset + 1);
|
---|
13 | if ((low.test(match) && !hi.test(next)) || (hi.test(match) && !low.test(prev))) {
|
---|
14 | return '\\u' + match.charCodeAt(0).toString(16);
|
---|
15 | } return match;
|
---|
16 | };
|
---|
17 |
|
---|
18 | var FORCED = fails(function () {
|
---|
19 | return $stringify('\uDF06\uD834') !== '"\\udf06\\ud834"'
|
---|
20 | || $stringify('\uDEAD') !== '"\\udead"';
|
---|
21 | });
|
---|
22 |
|
---|
23 | if ($stringify) {
|
---|
24 | // `JSON.stringify` method
|
---|
25 | // https://tc39.es/ecma262/#sec-json.stringify
|
---|
26 | // https://github.com/tc39/proposal-well-formed-stringify
|
---|
27 | $({ target: 'JSON', stat: true, forced: FORCED }, {
|
---|
28 | // eslint-disable-next-line no-unused-vars -- required for `.length`
|
---|
29 | stringify: function stringify(it, replacer, space) {
|
---|
30 | var result = $stringify.apply(null, arguments);
|
---|
31 | return typeof result == 'string' ? result.replace(re, fix) : result;
|
---|
32 | }
|
---|
33 | });
|
---|
34 | }
|
---|