-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
129 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,7 @@ module.exports = { | |
'esnext.map.upsert', | ||
'esnext.weak-map.upsert', | ||
], | ||
3.4: [ | ||
'es.json.stringify', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
require('../../modules/es.json.stringify'); | ||
var core = require('../../internals/path'); | ||
var $JSON = core.JSON || (core.JSON = { stringify: JSON.stringify }); | ||
|
||
module.exports = function stringify(it) { // eslint-disable-line no-unused-vars | ||
return $JSON.stringify.apply($JSON, arguments); | ||
if (!core.JSON) core.JSON = { stringify: JSON.stringify }; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
module.exports = function stringify(it, replacer, space) { | ||
return core.JSON.stringify.apply(null, arguments); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var $ = require('../internals/export'); | ||
var getBuiltIn = require('../internals/get-built-in'); | ||
var fails = require('../internals/fails'); | ||
var IndexedObject = require('../internals/indexed-object'); | ||
|
||
var $stringify = getBuiltIn('JSON', 'stringify'); | ||
var reg = /[\uD800-\uDFFF]/g; | ||
var re = /^[\uD800-\uDFFF]$/; | ||
var low = /^[\uD800-\uDBFF]$/; | ||
var hi = /^[\uDC00-\uDFFF]$/; | ||
|
||
var fix = function (string) { | ||
var indexed = IndexedObject(string); | ||
var length = indexed.length; | ||
var result = ''; | ||
var i = 0; | ||
var point, prev, next; | ||
for (; i < length; i++) { | ||
point = indexed[i]; | ||
if (re.test(point)) { | ||
prev = i === 0 ? '' : indexed[i - 1]; | ||
next = i === length - 1 ? '' : indexed[i + 1]; | ||
if ((low.test(point) && !hi.test(next)) || (hi.test(point) && !low.test(prev))) { | ||
result += '\\u' + point.charCodeAt(0).toString(16); | ||
continue; | ||
} | ||
} result += point; | ||
} return result; | ||
}; | ||
|
||
var FORCED = fails(function () { | ||
return $stringify('\uDF06\uD834') !== '"\\udf06\\ud834"' | ||
|| $stringify('\uDEAD') !== '"\\udead"'; | ||
}); | ||
|
||
if ($stringify) { | ||
// https://github.com/tc39/proposal-well-formed-stringify | ||
$({ target: 'JSON', stat: true, forced: FORCED }, { | ||
// eslint-disable-next-line no-unused-vars | ||
stringify: function stringify(it, replacer, space) { | ||
var result = $stringify.apply(null, arguments); | ||
return typeof result == 'string' && reg.test(result) ? fix(result) : result; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { GLOBAL } from '../helpers/constants'; | ||
import stringify from 'core-js/es/json/stringify'; | ||
|
||
if (GLOBAL.JSON) { | ||
QUnit.test('Well‑formed JSON.stringify', assert => { | ||
assert.isFunction(stringify); | ||
assert.arity(stringify, 3); | ||
assert.name(stringify, 'stringify'); | ||
|
||
assert.same(stringify({ foo: 'bar' }), '{"foo":"bar"}', 'basic'); | ||
assert.same(stringify('\uDEAD'), '"\\udead"', 'r1'); | ||
assert.same(stringify('\uDF06\uD834'), '"\\udf06\\ud834"', 'r2'); | ||
assert.same(stringify('\uDF06ab\uD834'), '"\\udf06ab\\ud834"', 'r3'); | ||
assert.same(stringify('𠮷'), '"𠮷"', 'r4'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { GLOBAL } from '../helpers/constants'; | ||
|
||
if (GLOBAL.JSON) { | ||
QUnit.test('Well‑formed JSON.stringify', assert => { | ||
const { stringify } = JSON; | ||
assert.isFunction(stringify); | ||
assert.arity(stringify, 3); | ||
assert.name(stringify, 'stringify'); | ||
assert.looksNative(stringify); | ||
|
||
assert.same(stringify({ foo: 'bar' }), '{"foo":"bar"}', 'basic'); | ||
assert.same(stringify('\uDEAD'), '"\\udead"', 'r1'); | ||
assert.same(stringify('\uDF06\uD834'), '"\\udf06\\ud834"', 'r2'); | ||
assert.same(stringify('\uDF06ab\uD834'), '"\\udf06ab\\ud834"', 'r3'); | ||
assert.same(stringify('𠮷'), '"𠮷"', 'r4'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters