Skip to content

Commit be10116

Browse files
committed
fix(client): add proxy support to stringify
1 parent cb5c0bb commit be10116

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

common/stringify.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ var stringify = function stringify (obj, depth) {
1919
case 'undefined':
2020
return 'undefined'
2121
case 'function':
22-
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
22+
try {
23+
// function abc(a, b, c) { /* code goes here */ }
24+
// -> function abc(a, b, c) { ... }
25+
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
26+
} catch (err) {
27+
if (err instanceof TypeError) {
28+
// Proxy(function abc(...) { ... })
29+
return 'Proxy(function ' + (obj.name || '') + '(...) { ... })'
30+
} else {
31+
throw err
32+
}
33+
}
2334
case 'boolean':
2435
return obj ? 'true' : 'false'
2536
case 'object':

test/client/stringify.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ describe('stringify', function () {
3636
})
3737
})
3838

39+
// Conditionally run Proxy tests as it's not supported by all browsers yet
40+
// http://caniuse.com/#feat=proxy
41+
if (window.Proxy) {
42+
it('should serialize proxied functions', function () {
43+
var abcProxy = new Proxy(function abc (a, b, c) { return 'whatever' }, {})
44+
var defProxy = new Proxy(function (d, e, f) { return 'whatever' }, {})
45+
46+
assert.deepEqual(stringify(abcProxy), 'Proxy(function abc(...) { ... })')
47+
assert.deepEqual(stringify(defProxy), 'Proxy(function (...) { ... })')
48+
})
49+
}
50+
3951
it('should serialize arrays', function () {
4052
assert.deepEqual(stringify(['a', 'b', null, true, false]), "['a', 'b', null, true, false]")
4153
})

0 commit comments

Comments
 (0)