From 4b447ad2bd9284b0404c3723617f7d219caa7bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Zori=C4=87?= Date: Fri, 9 Apr 2021 10:38:43 +0200 Subject: [PATCH 1/3] fix: properly check for array --- main.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index e8bf96d..d8ce6e4 100644 --- a/main.js +++ b/main.js @@ -11,6 +11,13 @@ var TOKEN_NULL = -3; var TOKEN_EMPTY_STRING = -4; var TOKEN_UNDEFINED = -5; + + var isArray = Array.isArray; + if (typeof isArray !== "function") { + isArray = function(item) { + return Object.prototype.toString.call(item) === '[object Array]'; + } + } var pack = function(json, options) { @@ -61,7 +68,7 @@ } // Case 1: The item is Array Object - if ( item instanceof Array) { + if (isArray(item)) { // Create a new sub-AST of type Array (@) var ast = ['@']; From 06769c602cc1e21d4df889824a9108f7f71934d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Zori=C4=87?= Date: Fri, 9 Apr 2021 11:10:18 +0200 Subject: [PATCH 2/3] fix: add instanceof check before the prototype check --- main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.js b/main.js index d8ce6e4..00308a3 100644 --- a/main.js +++ b/main.js @@ -15,6 +15,9 @@ var isArray = Array.isArray; if (typeof isArray !== "function") { isArray = function(item) { + if (item instanceof Array) { + return true; + } return Object.prototype.toString.call(item) === '[object Array]'; } } From e35a37b6f15eb18fbe89446862cdfb2c5bbd2e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Zori=C4=87?= Date: Fri, 9 Apr 2021 14:50:35 +0200 Subject: [PATCH 3/3] feat: do some prechecks before array checking if is array --- main.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 00308a3..727328e 100644 --- a/main.js +++ b/main.js @@ -15,7 +15,10 @@ var isArray = Array.isArray; if (typeof isArray !== "function") { isArray = function(item) { - if (item instanceof Array) { + if (!item || typeof item !== "object") { + return false; + } + else if (item instanceof Array) { return true; } return Object.prototype.toString.call(item) === '[object Array]';