6
6
'use strict'
7
7
8
8
// dependencies
9
- var util = require ( './internal/utils' )
10
- var Ref = require ( './internal/reference' )
9
+ const utils = require ( './internal/utils' )
10
+ const Ref = require ( './internal/reference' )
11
11
12
12
/**
13
13
* serializes an object to javascript
14
14
*
15
15
* @example <caption>serializing regex, date, buffer, ...</caption>
16
- * var serialize = require('serialize-to-js').serialize;
17
- * var obj = {
16
+ * const serialize = require('serialize-to-js').serialize;
17
+ * const obj = {
18
18
* str: '<script>var a = 0 > 1</script>',
19
19
* num: 3.1415,
20
20
* bool: true,
@@ -30,10 +30,10 @@ var Ref = require('./internal/reference')
30
30
* // > {str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E", num: 3.1415, bool: true, nil: null, undef: undefined, obj: {foo: "bar"}, arr: [1, "2"], regexp: /^test?$/, date: new Date("2016-04-15T16:22:52.009Z"), buffer: new Buffer('ZGF0YQ==', 'base64') }
31
31
*
32
32
* @example <caption>serializing while respecting references</caption>
33
- * var serialize = require('serialize-to-js').serialize;
34
- * var obj = { object: { regexp: /^test?$/ } };
33
+ * const serialize = require('serialize-to-js').serialize;
34
+ * const obj = { object: { regexp: /^test?$/ } };
35
35
* obj.reference = obj.object;
36
- * var opts = { reference: true };
36
+ * const opts = { reference: true };
37
37
* console.log(serialize(obj, opts));
38
38
* //> {object: {regexp: /^test?$/}}
39
39
* console.log(opts.references);
@@ -47,84 +47,78 @@ var Ref = require('./internal/reference')
47
47
* @return {String } serialized representation of `source`
48
48
*/
49
49
function serialize ( source , opts ) {
50
- var out = ''
51
- var key
52
- var tmp
53
- var type
54
- var i
50
+ let type
55
51
56
52
opts = opts || { }
57
53
if ( ! opts . _visited ) {
58
54
opts . _visited = [ ]
59
55
}
60
56
if ( ! opts . _refs ) {
61
57
opts . references = [ ]
62
- opts . _refs = new Ref ( opts . references )
58
+ opts . _refs = new Ref ( opts . references , opts )
63
59
}
64
60
65
- if ( util . isNull ( source ) ) {
66
- out += 'null'
67
- } else if ( util . isArray ( source ) ) {
68
- tmp = source . map ( function ( item ) {
69
- return serialize ( item , opts )
70
- } )
71
- out += '[' + tmp . join ( ', ' ) + ']'
72
- } else if ( util . isFunction ( source ) ) {
73
- tmp = source . toString ( )
61
+ if ( utils . isNull ( source ) ) {
62
+ return 'null'
63
+ } else if ( Array . isArray ( source ) ) {
64
+ const tmp = source . map ( item => serialize ( item , opts ) )
65
+ return `[ ${ tmp . join ( ', ' ) } ]`
66
+ } else if ( utils . isFunction ( source ) ) {
67
+ // serializes functions only in unsafe mode!
68
+ const _tmp = source . toString ( )
69
+ const tmp = opts . unsafe ? _tmp : utils . saferFunctionString ( _tmp , opts )
74
70
// append function to es6 function within obj
75
- out += ! / ^ \s * ( f u n c t i o n | \( [ ^ ) ] * \) \s * = > ) / m. test ( tmp ) ? 'function ' + tmp : tmp
76
- } else if ( util . isObject ( source ) ) {
77
- if ( util . isRegExp ( source ) ) {
78
- out += ' new RegExp(' + serialize ( source . source ) + ' , "' + source . flags + '")'
79
- } else if ( util . isDate ( source ) ) {
80
- out += ' new Date("' + source . toJSON ( ) + '")'
81
- } else if ( util . isError ( source ) ) {
82
- out += ' new Error(' + ( source . message ? '"' + source . message + '"' : '' ) + ')'
83
- } else if ( util . isBuffer ( source ) ) {
71
+ return ! / ^ \s * ( f u n c t i o n | \( [ ^ ) ] * ? \) \s * = > ) / m. test ( tmp ) ? 'function ' + tmp : tmp
72
+ } else if ( utils . isObject ( source ) ) {
73
+ if ( utils . isRegExp ( source ) ) {
74
+ return ` new RegExp(${ utils . quote ( source . source , opts ) } , "${ source . flags } ")`
75
+ } else if ( utils . isDate ( source ) ) {
76
+ return ` new Date(${ utils . quote ( source . toJSON ( ) , opts ) } )`
77
+ } else if ( utils . isError ( source ) ) {
78
+ return ` new Error(${ utils . quote ( source . message , opts ) } )`
79
+ } else if ( utils . isBuffer ( source ) ) {
84
80
// check for buffer first otherwise tests fail on node@4.4
85
81
// looks like buffers are accidentially detected as typed arrays
86
- out += " Buffer.from('" + source . toString ( 'base64' ) + " ', 'base64')"
87
- } else if ( ( type = util . isTypedArray ( source ) ) ) {
88
- tmp = [ ]
89
- for ( i = 0 ; i < source . length ; i ++ ) {
82
+ return ` Buffer.from('${ source . toString ( 'base64' ) } ', 'base64')`
83
+ } else if ( ( type = utils . isTypedArray ( source ) ) ) {
84
+ const tmp = [ ]
85
+ for ( let i = 0 ; i < source . length ; i ++ ) {
90
86
tmp . push ( source [ i ] )
91
87
}
92
- out += 'new ' + type + '(' +
93
- '[' + tmp . join ( ', ' ) + ']' +
94
- ')'
88
+ return `new ${ type } ([${ tmp . join ( ', ' ) } ])`
95
89
} else {
96
- tmp = [ ]
90
+ const tmp = [ ]
97
91
// copy properties if not circular
98
92
if ( ! ~ opts . _visited . indexOf ( source ) ) {
99
93
opts . _visited . push ( source )
100
- for ( key in source ) {
101
- if ( source . hasOwnProperty ( key ) ) {
102
- if ( opts . reference && util . isObject ( source [ key ] ) ) {
94
+ for ( const key in source ) {
95
+ if ( Object . prototype . hasOwnProperty . call ( source , key ) ) {
96
+ if ( opts . reference && utils . isObject ( source [ key ] ) ) {
103
97
opts . _refs . push ( key )
104
98
if ( ! opts . _refs . hasReference ( source [ key ] ) ) {
105
- tmp . push ( Ref . wrapkey ( key ) + ': ' + serialize ( source [ key ] , opts ) )
99
+ tmp . push ( Ref . wrapkey ( key , opts ) + ': ' + serialize ( source [ key ] , opts ) )
106
100
}
107
101
opts . _refs . pop ( )
108
102
} else {
109
- tmp . push ( Ref . wrapkey ( key ) + ': ' + serialize ( source [ key ] , opts ) )
103
+ tmp . push ( Ref . wrapkey ( key , opts ) + ': ' + serialize ( source [ key ] , opts ) )
110
104
}
111
105
}
112
106
}
113
- out += '{' + tmp . join ( ', ' ) + '}'
114
107
opts . _visited . pop ( )
108
+ return `{${ tmp . join ( ', ' ) } }`
115
109
} else {
116
110
if ( opts . ignoreCircular ) {
117
- out += '{/*[Circular]*/}'
111
+ return '{/*[Circular]*/}'
118
112
} else {
119
113
throw new Error ( 'can not convert circular structures.' )
120
114
}
121
115
}
122
116
}
123
- } else if ( util . isString ( source ) ) {
124
- out += '"' + ( opts . unsafe ? util . unsafeString ( source ) : util . safeString ( source ) ) + '"'
117
+ } else if ( utils . isString ( source ) ) {
118
+ return utils . quote ( source , opts )
125
119
} else {
126
- out += '' + source
120
+ return '' + source
127
121
}
128
- return out
129
122
}
123
+
130
124
module . exports = serialize
0 commit comments