File tree Expand file tree Collapse file tree 2 files changed +32
-5
lines changed Expand file tree Collapse file tree 2 files changed +32
-5
lines changed Original file line number Diff line number Diff line change @@ -6,10 +6,6 @@ See the accompanying LICENSE file for terms.
66
77'use strict' ;
88
9- // Generate an internal UID to make the regexp pattern harder to guess.
10- var UID = Math . floor ( Math . random ( ) * 0x10000000000 ) . toString ( 16 ) ;
11- var PLACE_HOLDER_REGEXP = new RegExp ( '"@__(F|R|D)-' + UID + '-(\\d+)__@"' , 'g' ) ;
12-
139var IS_NATIVE_CODE_REGEXP = / \{ \s * \[ n a t i v e c o d e \] \s * \} / g;
1410var UNSAFE_CHARS_REGEXP = / [ < > \/ \u2028 \u2029 ] / g;
1511
@@ -67,9 +63,27 @@ module.exports = function serialize(obj, options) {
6763
6864 return value ;
6965 }
66+ // Generate an internal UID, and make sure it won't appear in the object
67+ var UID = Math . random ( ) . toString ( 16 ) ;
68+ var raw = JSON . stringify ( obj , function ( key , value ) {
69+ var type = typeof value ;
70+ // the key could contain a placeholder
71+ if (
72+ type === 'function' ||
73+ ( type === 'object' && ( value instanceof RegExp || value instanceof Date ) )
74+ ) {
75+ return key ;
76+ }
77+ return value ;
78+ } ) ;
79+ if ( typeof raw === 'string' ) {
80+ while ( raw . indexOf ( UID ) !== - 1 ) {
81+ UID += '@' ;
82+ }
83+ }
84+ var PLACE_HOLDER_REGEXP = new RegExp ( '"@__(F|R|D)-' + UID + '-(\\d+)__@"' , 'g' ) ;
7085
7186 var str ;
72-
7387 // Creates a JSON string representation of the value.
7488 // NOTE: Node 0.12 goes into slow mode with extra JSON.stringify() args.
7589 if ( options . isJSON && ! options . space ) {
Original file line number Diff line number Diff line change @@ -252,4 +252,17 @@ describe('serialize( obj )', function () {
252252 expect ( serialize ( [ 1 ] , 2 ) ) . to . equal ( '[\n 1\n]' ) ;
253253 } ) ;
254254 } ) ;
255+
256+ describe ( 'magic placeholder' , function ( ) {
257+ it ( 'should handle magic placeholder' , function ( ) {
258+ var origRand = Math . random
259+ Math . random = function ( ) { return 1 }
260+ var data = {
261+ '@__F-1-0__@' : function ( ) { }
262+ }
263+
264+ expect ( serialize ( data ) ) . to . equal ( '{"@__F-1-0__@":function () {}}' )
265+ Math . random = origRand
266+ } )
267+ } )
255268} ) ;
You can’t perform that action at this time.
0 commit comments