services/common/modules-testing/utils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/common/modules-testing/utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,42 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = [
    1.11 +  "TestingUtils",
    1.12 +];
    1.13 +
    1.14 +this.TestingUtils = {
    1.15 +  /**
    1.16 +   * Perform a deep copy of an Array or Object.
    1.17 +   */
    1.18 +  deepCopy: function deepCopy(thing, noSort) {
    1.19 +    if (typeof(thing) != "object" || thing == null) {
    1.20 +      return thing;
    1.21 +    }
    1.22 +
    1.23 +    if (Array.isArray(thing)) {
    1.24 +      let ret = [];
    1.25 +      for (let element of thing) {
    1.26 +        ret.push(this.deepCopy(element, noSort));
    1.27 +      }
    1.28 +
    1.29 +      return ret;
    1.30 +    }
    1.31 +
    1.32 +    let ret = {};
    1.33 +    let props = [p for (p in thing)];
    1.34 +
    1.35 +    if (!noSort) {
    1.36 +      props = props.sort();
    1.37 +    }
    1.38 +
    1.39 +    for (let prop of props) {
    1.40 +      ret[prop] = this.deepCopy(thing[prop], noSort);
    1.41 +    }
    1.42 +
    1.43 +    return ret;
    1.44 +  },
    1.45 +};

mercurial