1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _jestGetType = require('jest-get-type');
|
9 |
|
10 | function _defineProperty(obj, key, value) {
|
11 | if (key in obj) {
|
12 | Object.defineProperty(obj, key, {
|
13 | value: value,
|
14 | enumerable: true,
|
15 | configurable: true,
|
16 | writable: true
|
17 | });
|
18 | } else {
|
19 | obj[key] = value;
|
20 | }
|
21 | return obj;
|
22 | }
|
23 |
|
24 | const supportTypes = ['map', 'array', 'object'];
|
25 |
|
26 |
|
27 | class Replaceable {
|
28 | constructor(object) {
|
29 | _defineProperty(this, 'object', void 0);
|
30 |
|
31 | _defineProperty(this, 'type', void 0);
|
32 |
|
33 | this.object = object;
|
34 | this.type = (0, _jestGetType.getType)(object);
|
35 |
|
36 | if (!supportTypes.includes(this.type)) {
|
37 | throw new Error(`Type ${this.type} is not support in Replaceable!`);
|
38 | }
|
39 | }
|
40 |
|
41 | static isReplaceable(obj1, obj2) {
|
42 | const obj1Type = (0, _jestGetType.getType)(obj1);
|
43 | const obj2Type = (0, _jestGetType.getType)(obj2);
|
44 | return obj1Type === obj2Type && supportTypes.includes(obj1Type);
|
45 | }
|
46 |
|
47 | forEach(cb) {
|
48 | if (this.type === 'object') {
|
49 | const descriptors = Object.getOwnPropertyDescriptors(this.object);
|
50 | [
|
51 | ...Object.keys(descriptors),
|
52 | ...Object.getOwnPropertySymbols(descriptors)
|
53 | ]
|
54 |
|
55 | .filter(key => descriptors[key].enumerable)
|
56 | .forEach(key => {
|
57 | cb(this.object[key], key, this.object);
|
58 | });
|
59 | } else {
|
60 | this.object.forEach(cb);
|
61 | }
|
62 | }
|
63 |
|
64 | get(key) {
|
65 | if (this.type === 'map') {
|
66 | return this.object.get(key);
|
67 | }
|
68 |
|
69 | return this.object[key];
|
70 | }
|
71 |
|
72 | set(key, value) {
|
73 | if (this.type === 'map') {
|
74 | this.object.set(key, value);
|
75 | } else {
|
76 | this.object[key] = value;
|
77 | }
|
78 | }
|
79 | }
|
80 |
|
81 |
|
82 | exports.default = Replaceable;
|