dom_collection.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.serialize = exports.test = undefined;
  6. var _collections = require('../collections');
  7. /**
  8. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. *
  13. *
  14. */
  15. const SPACE = ' ';
  16. const COLLECTION_NAMES = ['DOMStringMap', 'NamedNodeMap'];
  17. const test = (exports.test = val =>
  18. val &&
  19. val.constructor &&
  20. COLLECTION_NAMES.indexOf(val.constructor.name) !== -1);
  21. const convertCollectionToObject = collection => {
  22. let result = {};
  23. if (collection.constructor.name === 'NamedNodeMap') {
  24. for (let i = 0; i < collection.length; i++) {
  25. result[collection[i].name] = collection[i].value;
  26. }
  27. } else {
  28. result = Object.assign({}, collection);
  29. }
  30. return result;
  31. };
  32. const serialize = (exports.serialize = (
  33. collection,
  34. config,
  35. indentation,
  36. depth,
  37. refs,
  38. printer
  39. ) => {
  40. if (++depth > config.maxDepth) {
  41. return '[' + collection.constructor.name + ']';
  42. }
  43. return (
  44. collection.constructor.name +
  45. SPACE +
  46. '{' +
  47. (0, _collections.printObjectProperties)(
  48. convertCollectionToObject(collection),
  49. config,
  50. indentation,
  51. depth,
  52. refs,
  53. printer
  54. ) +
  55. '}'
  56. );
  57. });
  58. exports.default = {serialize: serialize, test: test};