michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 07 February 2001 michael@0: * michael@0: * Functionality common to Array testing - michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: var CHAR_LBRACKET = '['; michael@0: var CHAR_RBRACKET = ']'; michael@0: var CHAR_QT_DBL = '"'; michael@0: var CHAR_QT = "'"; michael@0: var CHAR_NL = '\n'; michael@0: var CHAR_COMMA = ','; michael@0: var CHAR_SPACE = ' '; michael@0: var TYPE_STRING = typeof 'abc'; michael@0: michael@0: michael@0: /* michael@0: * If available, arr.toSource() gives more detail than arr.toString() michael@0: * michael@0: * var arr = Array(1,2,'3'); michael@0: * michael@0: * arr.toSource() michael@0: * [1, 2, "3"] michael@0: * michael@0: * arr.toString() michael@0: * 1,2,3 michael@0: * michael@0: * But toSource() doesn't exist in Rhino, so use our own imitation, below - michael@0: * michael@0: */ michael@0: function formatArray(arr) michael@0: { michael@0: try michael@0: { michael@0: return arr.toSource(); michael@0: } michael@0: catch(e) michael@0: { michael@0: return toSource(arr); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: /* michael@0: * Imitate SpiderMonkey's arr.toSource() method: michael@0: * michael@0: * a) Double-quote each array element that is of string type michael@0: * b) Represent |undefined| and |null| by empty strings michael@0: * c) Delimit elements by a comma + single space michael@0: * d) Do not add delimiter at the end UNLESS the last element is |undefined| michael@0: * e) Add square brackets to the beginning and end of the string michael@0: */ michael@0: function toSource(arr) michael@0: { michael@0: var delim = CHAR_COMMA + CHAR_SPACE; michael@0: var elt = ''; michael@0: var ret = ''; michael@0: var len = arr.length; michael@0: michael@0: for (i=0; i