UNPKG

1.93 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = exports.serialize = exports.test = void 0;
7
8var _collections = require('../collections');
9
10/**
11 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
12 *
13 * This source code is licensed under the MIT license found in the
14 * LICENSE file in the root directory of this source tree.
15 */
16
17/* eslint-disable local/ban-types-eventually */
18const SPACE = ' ';
19const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap'];
20const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/;
21
22const testName = name =>
23 OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name);
24
25const test = val =>
26 val &&
27 val.constructor &&
28 !!val.constructor.name &&
29 testName(val.constructor.name);
30
31exports.test = test;
32
33const isNamedNodeMap = collection =>
34 collection.constructor.name === 'NamedNodeMap';
35
36const serialize = (collection, config, indentation, depth, refs, printer) => {
37 const name = collection.constructor.name;
38
39 if (++depth > config.maxDepth) {
40 return '[' + name + ']';
41 }
42
43 return (
44 (config.min ? '' : name + SPACE) +
45 (OBJECT_NAMES.indexOf(name) !== -1
46 ? '{' +
47 (0, _collections.printObjectProperties)(
48 isNamedNodeMap(collection)
49 ? Array.from(collection).reduce((props, attribute) => {
50 props[attribute.name] = attribute.value;
51 return props;
52 }, {})
53 : {...collection},
54 config,
55 indentation,
56 depth,
57 refs,
58 printer
59 ) +
60 '}'
61 : '[' +
62 (0, _collections.printListItems)(
63 Array.from(collection),
64 config,
65 indentation,
66 depth,
67 refs,
68 printer
69 ) +
70 ']')
71 );
72};
73
74exports.serialize = serialize;
75const plugin = {
76 serialize,
77 test
78};
79var _default = plugin;
80exports.default = _default;