Skip to content

Commit

Permalink
fix(client): add proxy support to stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
twolfson committed Mar 10, 2017
1 parent cb5c0bb commit be10116
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion common/stringify.js
Expand Up @@ -19,7 +19,18 @@ var stringify = function stringify (obj, depth) {
case 'undefined':
return 'undefined'
case 'function':
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
try {
// function abc(a, b, c) { /* code goes here */ }
// -> function abc(a, b, c) { ... }
return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
} catch (err) {
if (err instanceof TypeError) {
// Proxy(function abc(...) { ... })
return 'Proxy(function ' + (obj.name || '') + '(...) { ... })'
} else {
throw err
}
}
case 'boolean':
return obj ? 'true' : 'false'
case 'object':
Expand Down
12 changes: 12 additions & 0 deletions test/client/stringify.spec.js
Expand Up @@ -36,6 +36,18 @@ describe('stringify', function () {
})
})

// Conditionally run Proxy tests as it's not supported by all browsers yet
// http://caniuse.com/#feat=proxy
if (window.Proxy) {
it('should serialize proxied functions', function () {
var abcProxy = new Proxy(function abc (a, b, c) { return 'whatever' }, {})
var defProxy = new Proxy(function (d, e, f) { return 'whatever' }, {})

assert.deepEqual(stringify(abcProxy), 'Proxy(function abc(...) { ... })')
assert.deepEqual(stringify(defProxy), 'Proxy(function (...) { ... })')
})
}

it('should serialize arrays', function () {
assert.deepEqual(stringify(['a', 'b', null, true, false]), "['a', 'b', null, true, false]")
})
Expand Down

0 comments on commit be10116

Please sign in to comment.