[79a0317] | 1 | 'use strict';
|
---|
| 2 | var $ = require('../internals/export');
|
---|
| 3 | var getBuiltIn = require('../internals/get-built-in');
|
---|
| 4 | var apply = require('../internals/function-apply');
|
---|
| 5 | var call = require('../internals/function-call');
|
---|
| 6 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
| 7 | var fails = require('../internals/fails');
|
---|
| 8 | var isCallable = require('../internals/is-callable');
|
---|
| 9 | var isSymbol = require('../internals/is-symbol');
|
---|
| 10 | var arraySlice = require('../internals/array-slice');
|
---|
| 11 | var getReplacerFunction = require('../internals/get-json-replacer-function');
|
---|
| 12 | var NATIVE_SYMBOL = require('../internals/symbol-constructor-detection');
|
---|
| 13 |
|
---|
| 14 | var $String = String;
|
---|
| 15 | var $stringify = getBuiltIn('JSON', 'stringify');
|
---|
| 16 | var exec = uncurryThis(/./.exec);
|
---|
| 17 | var charAt = uncurryThis(''.charAt);
|
---|
| 18 | var charCodeAt = uncurryThis(''.charCodeAt);
|
---|
| 19 | var replace = uncurryThis(''.replace);
|
---|
| 20 | var numberToString = uncurryThis(1.0.toString);
|
---|
| 21 |
|
---|
| 22 | var tester = /[\uD800-\uDFFF]/g;
|
---|
| 23 | var low = /^[\uD800-\uDBFF]$/;
|
---|
| 24 | var hi = /^[\uDC00-\uDFFF]$/;
|
---|
| 25 |
|
---|
| 26 | var WRONG_SYMBOLS_CONVERSION = !NATIVE_SYMBOL || fails(function () {
|
---|
| 27 | var symbol = getBuiltIn('Symbol')('stringify detection');
|
---|
| 28 | // MS Edge converts symbol values to JSON as {}
|
---|
| 29 | return $stringify([symbol]) !== '[null]'
|
---|
| 30 | // WebKit converts symbol values to JSON as null
|
---|
| 31 | || $stringify({ a: symbol }) !== '{}'
|
---|
| 32 | // V8 throws on boxed symbols
|
---|
| 33 | || $stringify(Object(symbol)) !== '{}';
|
---|
| 34 | });
|
---|
| 35 |
|
---|
| 36 | // https://github.com/tc39/proposal-well-formed-stringify
|
---|
| 37 | var ILL_FORMED_UNICODE = fails(function () {
|
---|
| 38 | return $stringify('\uDF06\uD834') !== '"\\udf06\\ud834"'
|
---|
| 39 | || $stringify('\uDEAD') !== '"\\udead"';
|
---|
| 40 | });
|
---|
| 41 |
|
---|
| 42 | var stringifyWithSymbolsFix = function (it, replacer) {
|
---|
| 43 | var args = arraySlice(arguments);
|
---|
| 44 | var $replacer = getReplacerFunction(replacer);
|
---|
| 45 | if (!isCallable($replacer) && (it === undefined || isSymbol(it))) return; // IE8 returns string on undefined
|
---|
| 46 | args[1] = function (key, value) {
|
---|
| 47 | // some old implementations (like WebKit) could pass numbers as keys
|
---|
| 48 | if (isCallable($replacer)) value = call($replacer, this, $String(key), value);
|
---|
| 49 | if (!isSymbol(value)) return value;
|
---|
| 50 | };
|
---|
| 51 | return apply($stringify, null, args);
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | var fixIllFormed = function (match, offset, string) {
|
---|
| 55 | var prev = charAt(string, offset - 1);
|
---|
| 56 | var next = charAt(string, offset + 1);
|
---|
| 57 | if ((exec(low, match) && !exec(hi, next)) || (exec(hi, match) && !exec(low, prev))) {
|
---|
| 58 | return '\\u' + numberToString(charCodeAt(match, 0), 16);
|
---|
| 59 | } return match;
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | if ($stringify) {
|
---|
| 63 | // `JSON.stringify` method
|
---|
| 64 | // https://tc39.es/ecma262/#sec-json.stringify
|
---|
| 65 | $({ target: 'JSON', stat: true, arity: 3, forced: WRONG_SYMBOLS_CONVERSION || ILL_FORMED_UNICODE }, {
|
---|
| 66 | // eslint-disable-next-line no-unused-vars -- required for `.length`
|
---|
| 67 | stringify: function stringify(it, replacer, space) {
|
---|
| 68 | var args = arraySlice(arguments);
|
---|
| 69 | var result = apply(WRONG_SYMBOLS_CONVERSION ? stringifyWithSymbolsFix : $stringify, null, args);
|
---|
| 70 | return ILL_FORMED_UNICODE && typeof result == 'string' ? replace(result, tester, fixIllFormed) : result;
|
---|
| 71 | }
|
---|
| 72 | });
|
---|
| 73 | }
|
---|