utils.js 874 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. this.EXPORTED_SYMBOLS = [
  6. "TestingUtils",
  7. ];
  8. this.TestingUtils = {
  9. /**
  10. * Perform a deep copy of an Array or Object.
  11. */
  12. deepCopy: function deepCopy(thing, noSort) {
  13. if (typeof(thing) != "object" || thing == null) {
  14. return thing;
  15. }
  16. if (Array.isArray(thing)) {
  17. let ret = [];
  18. for (let element of thing) {
  19. ret.push(this.deepCopy(element, noSort));
  20. }
  21. return ret;
  22. }
  23. let ret = {};
  24. let props = Object.keys(thing);
  25. if (!noSort) {
  26. props = props.sort();
  27. }
  28. for (let prop of props) {
  29. ret[prop] = this.deepCopy(thing[prop], noSort);
  30. }
  31. return ret;
  32. },
  33. };