File size: 5,442 Bytes
369fac9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.test = exports.serialize = exports.default = void 0;
var _collections = require('../collections');
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// SENTINEL constants are from https://github.com/facebook/immutable-js
const IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
const IS_MAP_SENTINEL = '@@__IMMUTABLE_MAP__@@';
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
const IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@'; // immutable v4
const IS_SEQ_SENTINEL = '@@__IMMUTABLE_SEQ__@@';
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
const IS_STACK_SENTINEL = '@@__IMMUTABLE_STACK__@@';
const getImmutableName = name => `Immutable.${name}`;
const printAsLeaf = name => `[${name}]`;
const SPACE = ' ';
const LAZY = '…'; // Seq is lazy if it calls a method like filter
const printImmutableEntries = (
val,
config,
indentation,
depth,
refs,
printer,
type
) =>
++depth > config.maxDepth
? printAsLeaf(getImmutableName(type))
: `${getImmutableName(type) + SPACE}{${(0,
_collections.printIteratorEntries)(
val.entries(),
config,
indentation,
depth,
refs,
printer
)}}`;
// Record has an entries method because it is a collection in immutable v3.
// Return an iterator for Immutable Record from version v3 or v4.
function getRecordEntries(val) {
let i = 0;
return {
next() {
if (i < val._keys.length) {
const key = val._keys[i++];
return {
done: false,
value: [key, val.get(key)]
};
}
return {
done: true,
value: undefined
};
}
};
}
const printImmutableRecord = (
val,
config,
indentation,
depth,
refs,
printer
) => {
// _name property is defined only for an Immutable Record instance
// which was constructed with a second optional descriptive name arg
const name = getImmutableName(val._name || 'Record');
return ++depth > config.maxDepth
? printAsLeaf(name)
: `${name + SPACE}{${(0, _collections.printIteratorEntries)(
getRecordEntries(val),
config,
indentation,
depth,
refs,
printer
)}}`;
};
const printImmutableSeq = (val, config, indentation, depth, refs, printer) => {
const name = getImmutableName('Seq');
if (++depth > config.maxDepth) {
return printAsLeaf(name);
}
if (val[IS_KEYED_SENTINEL]) {
return `${name + SPACE}{${
// from Immutable collection of entries or from ECMAScript object
val._iter || val._object
? (0, _collections.printIteratorEntries)(
val.entries(),
config,
indentation,
depth,
refs,
printer
)
: LAZY
}}`;
}
return `${name + SPACE}[${
val._iter ||
// from Immutable collection of values
val._array ||
// from ECMAScript array
val._collection ||
// from ECMAScript collection in immutable v4
val._iterable // from ECMAScript collection in immutable v3
? (0, _collections.printIteratorValues)(
val.values(),
config,
indentation,
depth,
refs,
printer
)
: LAZY
}]`;
};
const printImmutableValues = (
val,
config,
indentation,
depth,
refs,
printer,
type
) =>
++depth > config.maxDepth
? printAsLeaf(getImmutableName(type))
: `${getImmutableName(type) + SPACE}[${(0,
_collections.printIteratorValues)(
val.values(),
config,
indentation,
depth,
refs,
printer
)}]`;
const serialize = (val, config, indentation, depth, refs, printer) => {
if (val[IS_MAP_SENTINEL]) {
return printImmutableEntries(
val,
config,
indentation,
depth,
refs,
printer,
val[IS_ORDERED_SENTINEL] ? 'OrderedMap' : 'Map'
);
}
if (val[IS_LIST_SENTINEL]) {
return printImmutableValues(
val,
config,
indentation,
depth,
refs,
printer,
'List'
);
}
if (val[IS_SET_SENTINEL]) {
return printImmutableValues(
val,
config,
indentation,
depth,
refs,
printer,
val[IS_ORDERED_SENTINEL] ? 'OrderedSet' : 'Set'
);
}
if (val[IS_STACK_SENTINEL]) {
return printImmutableValues(
val,
config,
indentation,
depth,
refs,
printer,
'Stack'
);
}
if (val[IS_SEQ_SENTINEL]) {
return printImmutableSeq(val, config, indentation, depth, refs, printer);
}
// For compatibility with immutable v3 and v4, let record be the default.
return printImmutableRecord(val, config, indentation, depth, refs, printer);
};
// Explicitly comparing sentinel properties to true avoids false positive
// when mock identity-obj-proxy returns the key as the value for any key.
exports.serialize = serialize;
const test = val =>
val &&
(val[IS_ITERABLE_SENTINEL] === true || val[IS_RECORD_SENTINEL] === true);
exports.test = test;
const plugin = {
serialize,
test
};
var _default = plugin;
exports.default = _default;
|