123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = exports.serialize = exports.test = void 0;
- var _collections = require('../collections');
- function _objectSpread(target) {
- for (var i = 1; i < arguments.length; i++) {
- var source = arguments[i] != null ? arguments[i] : {};
- var ownKeys = Object.keys(source);
- if (typeof Object.getOwnPropertySymbols === 'function') {
- ownKeys = ownKeys.concat(
- Object.getOwnPropertySymbols(source).filter(function(sym) {
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
- })
- );
- }
- ownKeys.forEach(function(key) {
- _defineProperty(target, key, source[key]);
- });
- }
- return target;
- }
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
- const SPACE = ' ';
- const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap'];
- const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/;
- const testName = name =>
- OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name);
- const test = val =>
- val &&
- val.constructor &&
- val.constructor.name &&
- testName(val.constructor.name); // Convert array of attribute objects to props object.
- exports.test = test;
- const propsReducer = (props, attribute) => {
- props[attribute.name] = attribute.value;
- return props;
- };
- const serialize = (collection, config, indentation, depth, refs, printer) => {
- const name = collection.constructor.name;
- if (++depth > config.maxDepth) {
- return '[' + name + ']';
- }
- return (
- (config.min ? '' : name + SPACE) +
- (OBJECT_NAMES.indexOf(name) !== -1
- ? '{' +
- (0, _collections.printObjectProperties)(
- name === 'NamedNodeMap'
- ? Array.prototype.reduce.call(collection, propsReducer, {})
- : _objectSpread({}, collection),
- config,
- indentation,
- depth,
- refs,
- printer
- ) +
- '}'
- : '[' +
- (0, _collections.printListItems)(
- Array.from(collection),
- config,
- indentation,
- depth,
- refs,
- printer
- ) +
- ']')
- );
- };
- exports.serialize = serialize;
- const plugin = {
- serialize,
- test
- };
- var _default = plugin;
- exports.default = _default;
|