From be10116dde92f3c5f87a09cec93f19665b95c129 Mon Sep 17 00:00:00 2001 From: Todd Wolfson Date: Thu, 9 Mar 2017 22:31:12 -0800 Subject: [PATCH] fix(client): add proxy support to stringify --- common/stringify.js | 13 ++++++++++++- test/client/stringify.spec.js | 12 ++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/common/stringify.js b/common/stringify.js index 327ec295a..8b07abc08 100644 --- a/common/stringify.js +++ b/common/stringify.js @@ -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': diff --git a/test/client/stringify.spec.js b/test/client/stringify.spec.js index 852bb74d4..241828295 100644 --- a/test/client/stringify.spec.js +++ b/test/client/stringify.spec.js @@ -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]") })