dom_collection.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 => val && val.constructor && COLLECTION_NAMES.indexOf(val.constructor.name) !== -1;
  18. const convertCollectionToObject = collection => {
  19. let result = {};
  20. if (collection.constructor.name === 'NamedNodeMap') {
  21. for (let i = 0; i < collection.length; i++) {
  22. result[collection[i].name] = collection[i].value;
  23. }
  24. } else {
  25. result = Object.assign({}, collection);
  26. }
  27. return result;
  28. };
  29. const serialize = exports.serialize = (collection, config, indentation, depth, refs, printer) => {
  30. if (++depth > config.maxDepth) {
  31. return '[' + collection.constructor.name + ']';
  32. }
  33. return collection.constructor.name + SPACE + '{' + (0, _collections.printObjectProperties)(convertCollectionToObject(collection), config, indentation, depth, refs, printer) + '}';
  34. };
  35. exports.default = { serialize, test };