From 82b9cb3dbf3af8a92a0b4f8cbea10296ca082cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 9 May 2013 15:30:18 -0700 Subject: [PATCH] Improve the Node version of Base.isPlainObject() --- src/node/index.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/node/index.js b/src/node/index.js index a5bd36a9..65d675c8 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -90,18 +90,19 @@ context.include('paper.js'); // Fix version now. Remove 2nd dot, so we can make a float out of it: options.version = parseFloat(json.version.replace(/(.)(\d)$/, '$2')); -// Since the created context fo Paper.js compilation, and the context in which -// Node.js scripts are executed do not share the definition of Object and Array, -// we need to redefine Base.isPlainObject() here. -// Object(obj) === obj is a trick from underscore, but also returns true for all -// Base objects. So we are filtering these out with an instanceof check, but -// Include Base instances since we're using them as hashes. +// Since the context used for Paper.js compilation, and the context in which +// Node.js scripts are executed do not share the definition of Object, we need +// to redefine Base.isPlainObject() here. +// So instead of checking for Object.prototype, we're checking +// proto.constructor.name for 'Object' // TODO: Benchmark the speed and consider this implementation instead of the -// current one. +// current one in straps.js too var Base = context.Base; Base.isPlainObject = function(obj) { - return Object(obj) === obj && !Array.isArray(obj) && (!(obj instanceof Base) - || Object.getPrototypeOf(obj) === Base.prototype); + var proto = obj !== null && typeof obj === 'object' + && Object.getPrototypeOf(obj); + return proto && (proto.constructor.name === 'Object' + || proto === Base.prototype); }; // Expose the Canvas, XMLSerializer to paper scopes: