@slimlib/smart-mock 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Konstantin Shutkin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Smart Mock
2
+
3
+ One more proxy based mock
4
+
5
+ # License
6
+
7
+ [MIT](https://github.com/kshutkin/slimlib/blob/main/LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,307 @@
1
+ 'use strict';
2
+
3
+ const mock = Symbol();
4
+ const unwrap = Symbol();
5
+ const unwrapValue = (value) => (value != null && value[unwrap]) || value;
6
+ const getMockData = (value) => (value != null && value[mock]) || undefined;
7
+ function createRecordingMockFactory() {
8
+ const mockDatas = [];
9
+ let counter = 0;
10
+ return {
11
+ createMock,
12
+ generateGlobals,
13
+ generate
14
+ };
15
+ function createMock(object, name) {
16
+ return createInternalMock(object, {
17
+ name,
18
+ source: 0 /* MockDataSource.root */,
19
+ useCount: 0,
20
+ generated: true
21
+ });
22
+ }
23
+ function generateGlobals() {
24
+ const strings = [];
25
+ for (const mockData of mockDatas) {
26
+ if (mockData.generated)
27
+ continue;
28
+ if (!mockData.instanceName) {
29
+ mockData.instanceName = getNextInstanceName();
30
+ }
31
+ strings.push('const ' + mockData.instanceName + ' = ' + getAccessor(mockData, mockData.parent));
32
+ for (const effect of (mockData.sideEffects || [])) {
33
+ switch (effect.source) {
34
+ case 3 /* MockDataSource.set */:
35
+ strings.push(mockData.instanceName + '.' + effect.name + ' = ' + stringify(effect.options, replacer));
36
+ break;
37
+ // case MockDataSource.defineProperty:
38
+ // strings.push('Object.defineProperty(' + mockData.instanceName + ', "' + (effect.name as string) + '", ' + stringify(effect.options, replacer as ReplacerFunction) + ')');
39
+ // break;
40
+ case 5 /* MockDataSource.deleteProperty */:
41
+ strings.push('delete ' + mockData.instanceName + '["' + effect.name + '"]');
42
+ break;
43
+ case 6 /* MockDataSource.setPrototypeOf */:
44
+ strings.push('Object.setPrototypeOf(' + mockData.instanceName + ', ' + stringify(effect.options, replacer) + ')');
45
+ break;
46
+ case 7 /* MockDataSource.preventExtensions */:
47
+ strings.push('Object.preventExtensions(' + mockData.instanceName + ')');
48
+ break;
49
+ }
50
+ }
51
+ }
52
+ return strings.join('\n');
53
+ function getAccessor(mockData, parent) {
54
+ var _a;
55
+ const parentName = ((_a = parent.instanceName) !== null && _a !== void 0 ? _a : parent.name);
56
+ switch (mockData.source) {
57
+ case 2 /* MockDataSource.call */:
58
+ return parentName + getParameters(mockData.options, replacer);
59
+ case 1 /* MockDataSource.get */:
60
+ return parentName + '.' + mockData.name;
61
+ case 8 /* MockDataSource.construct */:
62
+ {
63
+ const newTarget = stringify(mockData.target, replacer);
64
+ return parentName !== newTarget
65
+ ? 'Reflect.construct(' + parentName + ',' + stringify(mockData.options, replacer) + ',' + newTarget + ')'
66
+ : 'new ' + parentName + getParameters(mockData.options, replacer);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ function generate(object) {
72
+ stringify(object, bumpReplacer);
73
+ return stringify(object, replacer);
74
+ }
75
+ function bumpReplacer(value) {
76
+ const mockData = getMockData(value);
77
+ if (mockData) {
78
+ ++mockData.useCount;
79
+ return getCode(mockData, bumpReplacer, true);
80
+ }
81
+ return value;
82
+ }
83
+ function replacer(value) {
84
+ const mockData = getMockData(value);
85
+ if (mockData) {
86
+ return getCode(mockData, replacer, true);
87
+ }
88
+ return value;
89
+ }
90
+ function getCode(value, replacer, bumpCount) {
91
+ if (bumpCount && value.useCount > 1) {
92
+ if (value.source === 0 /* MockDataSource.root */) {
93
+ return value.name;
94
+ }
95
+ if (!value.instanceName) {
96
+ value.instanceName = getNextInstanceName();
97
+ }
98
+ return value.instanceName;
99
+ }
100
+ value.generated = true;
101
+ switch (value.source) {
102
+ case 2 /* MockDataSource.call */:
103
+ return getCallCode(value.parent, value);
104
+ case 1 /* MockDataSource.get */:
105
+ return getPrevCode(value) + '.' + value.name;
106
+ case 0 /* MockDataSource.root */:
107
+ return value.name;
108
+ case 8 /* MockDataSource.construct */:
109
+ {
110
+ const prevCode = getPrevCode(value);
111
+ const newTarget = stringify(value.target, replacer);
112
+ return prevCode !== newTarget
113
+ ? 'Reflect.construct(' + prevCode + ',' + stringify(value.options, replacer) + ',' + newTarget + ')'
114
+ : 'new ' + prevCode + getParameters(value.options, replacer);
115
+ }
116
+ }
117
+ function getCallCode(parent, value) {
118
+ parent.generated = true;
119
+ const prevCode = getPrevCode(parent);
120
+ return prevCode + (prevCode ? '.' : '') + parent.name + getParameters(value.options, replacer);
121
+ }
122
+ function getPrevCode(mockData) {
123
+ return mockData.parent ? getCode(mockData.parent, replacer, bumpCount) : '';
124
+ }
125
+ }
126
+ function createInternalMock(target, mockData) {
127
+ mockDatas.push(mockData);
128
+ target[mock] = mockData;
129
+ return new Proxy(target, {
130
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
131
+ set(target, p, value, receiver) {
132
+ const realValue = unwrapValue(value);
133
+ if (!mockData.sideEffects) {
134
+ mockData.sideEffects = [];
135
+ }
136
+ mockData.sideEffects.push({
137
+ useCount: 0,
138
+ name: p,
139
+ options: realValue,
140
+ parent: mockData,
141
+ source: 3 /* MockDataSource.set */,
142
+ generated: false
143
+ });
144
+ ++mockData.useCount;
145
+ Reflect.set(target, p, realValue, receiver);
146
+ return true;
147
+ },
148
+ get(target, p) {
149
+ if (p === unwrap)
150
+ return target;
151
+ if (p === mock)
152
+ return mockData;
153
+ const value = Reflect.get(target, p);
154
+ if (value === null || (typeof value !== 'object' && typeof value !== 'function')) {
155
+ return value;
156
+ }
157
+ if (!mockData.mocks) {
158
+ mockData.mocks = Object.create(null);
159
+ }
160
+ if (!mockData.mocks[p]) {
161
+ mockData.mocks[p] = createInternalMock(value, {
162
+ useCount: 0,
163
+ name: p,
164
+ parent: mockData,
165
+ source: 1 /* MockDataSource.get */,
166
+ generated: false
167
+ });
168
+ }
169
+ const result = mockData.mocks[p];
170
+ ++mockData.useCount;
171
+ return result;
172
+ },
173
+ // eslint-disable-next-line @typescript-eslint/ban-types
174
+ construct(target, argArray, newTarget) {
175
+ const realTarget = unwrapValue(newTarget);
176
+ const realArguments = unwrapValue(argArray);
177
+ ++mockData.useCount;
178
+ const result = Reflect.construct(target, realArguments, realTarget);
179
+ return createInternalMock(result, {
180
+ useCount: 0,
181
+ name: '',
182
+ options: realArguments,
183
+ target: realTarget,
184
+ parent: mockData,
185
+ source: 8 /* MockDataSource.construct */,
186
+ generated: false
187
+ });
188
+ },
189
+ defineProperty(target, property, attributes) {
190
+ const realValue = unwrapValue(attributes);
191
+ // if (!mockData.sideEffects) {
192
+ // mockData.sideEffects = [];
193
+ // }
194
+ // mockData.sideEffects.push({
195
+ // useCount: 0,
196
+ // name: property,
197
+ // options: realValue,
198
+ // parent: mockData,
199
+ // source: MockDataSource.defineProperty,
200
+ // generated: false
201
+ // });
202
+ // ++mockData.useCount;
203
+ return Reflect.defineProperty(target, property, realValue);
204
+ },
205
+ deleteProperty(target, p) {
206
+ if (!mockData.sideEffects) {
207
+ mockData.sideEffects = [];
208
+ }
209
+ mockData.sideEffects.push({
210
+ useCount: 0,
211
+ name: p,
212
+ options: undefined,
213
+ parent: mockData,
214
+ source: 5 /* MockDataSource.deleteProperty */,
215
+ generated: false
216
+ });
217
+ ++mockData.useCount;
218
+ const result = Reflect.deleteProperty(target, p);
219
+ return result;
220
+ },
221
+ setPrototypeOf(target, v) {
222
+ const realValue = unwrapValue(v);
223
+ if (!mockData.sideEffects) {
224
+ mockData.sideEffects = [];
225
+ }
226
+ mockData.sideEffects.push({
227
+ useCount: 0,
228
+ name: '',
229
+ options: realValue,
230
+ parent: mockData,
231
+ source: 6 /* MockDataSource.setPrototypeOf */,
232
+ generated: false
233
+ });
234
+ ++mockData.useCount;
235
+ return Reflect.setPrototypeOf(target, realValue);
236
+ },
237
+ preventExtensions(target) {
238
+ if (!mockData.sideEffects) {
239
+ mockData.sideEffects = [];
240
+ }
241
+ mockData.sideEffects.push({
242
+ useCount: 0,
243
+ name: '',
244
+ options: undefined,
245
+ parent: mockData,
246
+ source: 7 /* MockDataSource.preventExtensions */,
247
+ generated: false
248
+ });
249
+ ++mockData.useCount;
250
+ return Reflect.preventExtensions(target);
251
+ },
252
+ apply(target, thisArg, argumentsList) {
253
+ const realThis = unwrapValue(thisArg);
254
+ const realArguments = unwrapValue(argumentsList);
255
+ ++mockData.useCount;
256
+ return createInternalMock(Reflect.apply(target, realThis, realArguments), {
257
+ useCount: 0,
258
+ name: '',
259
+ parent: mockData,
260
+ source: 2 /* MockDataSource.call */,
261
+ options: realArguments,
262
+ generated: false
263
+ });
264
+ }
265
+ });
266
+ }
267
+ function getNextInstanceName() {
268
+ return `tmp_${counter++}`;
269
+ }
270
+ }
271
+ function getParameters(options, replacer) {
272
+ return `(${options.length ? options.map(value => stringify(value, replacer)).join(',') : ''})`;
273
+ }
274
+ function stringify(value, replacer) {
275
+ const original = value;
276
+ value = replacer(value);
277
+ if (original !== value && typeof value === 'string') {
278
+ return value;
279
+ }
280
+ if (value === null) {
281
+ return null;
282
+ }
283
+ if (value === undefined) {
284
+ return undefined;
285
+ }
286
+ if (typeof value === 'number') {
287
+ return `${value}`;
288
+ }
289
+ if (Array.isArray(value)) {
290
+ return `[${value.map((v) => stringify(v, replacer)).join(',')}]`;
291
+ }
292
+ if (typeof value === 'boolean') {
293
+ return value;
294
+ }
295
+ if (typeof value === 'function') {
296
+ return value.toString();
297
+ }
298
+ if (value instanceof RegExp) {
299
+ return value;
300
+ }
301
+ if (typeof value === 'object') {
302
+ return `{${Object.entries(value).map(([k, v]) => k + ':' + stringify(v, replacer)).join(',')}}`;
303
+ }
304
+ return '"' + String(value) + '"';
305
+ }
306
+
307
+ module.exports = createRecordingMockFactory;
@@ -0,0 +1,16 @@
1
+ export declare const enum MockDataSource {
2
+ root = 0,
3
+ get = 1,
4
+ call = 2,
5
+ set = 3,
6
+ defineProperty = 4,
7
+ deleteProperty = 5,
8
+ setPrototypeOf = 6,
9
+ preventExtensions = 7,
10
+ construct = 8
11
+ }
12
+ export default function createRecordingMockFactory(): {
13
+ createMock: <T extends object>(object: T, name: string) => T;
14
+ generateGlobals: () => string;
15
+ generate: <T_1 extends object>(object: T_1) => string | boolean | RegExp | null | undefined;
16
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,305 @@
1
+ const mock = Symbol();
2
+ const unwrap = Symbol();
3
+ const unwrapValue = (value) => (value != null && value[unwrap]) || value;
4
+ const getMockData = (value) => (value != null && value[mock]) || undefined;
5
+ function createRecordingMockFactory() {
6
+ const mockDatas = [];
7
+ let counter = 0;
8
+ return {
9
+ createMock,
10
+ generateGlobals,
11
+ generate
12
+ };
13
+ function createMock(object, name) {
14
+ return createInternalMock(object, {
15
+ name,
16
+ source: 0 /* MockDataSource.root */,
17
+ useCount: 0,
18
+ generated: true
19
+ });
20
+ }
21
+ function generateGlobals() {
22
+ const strings = [];
23
+ for (const mockData of mockDatas) {
24
+ if (mockData.generated)
25
+ continue;
26
+ if (!mockData.instanceName) {
27
+ mockData.instanceName = getNextInstanceName();
28
+ }
29
+ strings.push('const ' + mockData.instanceName + ' = ' + getAccessor(mockData, mockData.parent));
30
+ for (const effect of (mockData.sideEffects || [])) {
31
+ switch (effect.source) {
32
+ case 3 /* MockDataSource.set */:
33
+ strings.push(mockData.instanceName + '.' + effect.name + ' = ' + stringify(effect.options, replacer));
34
+ break;
35
+ // case MockDataSource.defineProperty:
36
+ // strings.push('Object.defineProperty(' + mockData.instanceName + ', "' + (effect.name as string) + '", ' + stringify(effect.options, replacer as ReplacerFunction) + ')');
37
+ // break;
38
+ case 5 /* MockDataSource.deleteProperty */:
39
+ strings.push('delete ' + mockData.instanceName + '["' + effect.name + '"]');
40
+ break;
41
+ case 6 /* MockDataSource.setPrototypeOf */:
42
+ strings.push('Object.setPrototypeOf(' + mockData.instanceName + ', ' + stringify(effect.options, replacer) + ')');
43
+ break;
44
+ case 7 /* MockDataSource.preventExtensions */:
45
+ strings.push('Object.preventExtensions(' + mockData.instanceName + ')');
46
+ break;
47
+ }
48
+ }
49
+ }
50
+ return strings.join('\n');
51
+ function getAccessor(mockData, parent) {
52
+ var _a;
53
+ const parentName = ((_a = parent.instanceName) !== null && _a !== void 0 ? _a : parent.name);
54
+ switch (mockData.source) {
55
+ case 2 /* MockDataSource.call */:
56
+ return parentName + getParameters(mockData.options, replacer);
57
+ case 1 /* MockDataSource.get */:
58
+ return parentName + '.' + mockData.name;
59
+ case 8 /* MockDataSource.construct */:
60
+ {
61
+ const newTarget = stringify(mockData.target, replacer);
62
+ return parentName !== newTarget
63
+ ? 'Reflect.construct(' + parentName + ',' + stringify(mockData.options, replacer) + ',' + newTarget + ')'
64
+ : 'new ' + parentName + getParameters(mockData.options, replacer);
65
+ }
66
+ }
67
+ }
68
+ }
69
+ function generate(object) {
70
+ stringify(object, bumpReplacer);
71
+ return stringify(object, replacer);
72
+ }
73
+ function bumpReplacer(value) {
74
+ const mockData = getMockData(value);
75
+ if (mockData) {
76
+ ++mockData.useCount;
77
+ return getCode(mockData, bumpReplacer, true);
78
+ }
79
+ return value;
80
+ }
81
+ function replacer(value) {
82
+ const mockData = getMockData(value);
83
+ if (mockData) {
84
+ return getCode(mockData, replacer, true);
85
+ }
86
+ return value;
87
+ }
88
+ function getCode(value, replacer, bumpCount) {
89
+ if (bumpCount && value.useCount > 1) {
90
+ if (value.source === 0 /* MockDataSource.root */) {
91
+ return value.name;
92
+ }
93
+ if (!value.instanceName) {
94
+ value.instanceName = getNextInstanceName();
95
+ }
96
+ return value.instanceName;
97
+ }
98
+ value.generated = true;
99
+ switch (value.source) {
100
+ case 2 /* MockDataSource.call */:
101
+ return getCallCode(value.parent, value);
102
+ case 1 /* MockDataSource.get */:
103
+ return getPrevCode(value) + '.' + value.name;
104
+ case 0 /* MockDataSource.root */:
105
+ return value.name;
106
+ case 8 /* MockDataSource.construct */:
107
+ {
108
+ const prevCode = getPrevCode(value);
109
+ const newTarget = stringify(value.target, replacer);
110
+ return prevCode !== newTarget
111
+ ? 'Reflect.construct(' + prevCode + ',' + stringify(value.options, replacer) + ',' + newTarget + ')'
112
+ : 'new ' + prevCode + getParameters(value.options, replacer);
113
+ }
114
+ }
115
+ function getCallCode(parent, value) {
116
+ parent.generated = true;
117
+ const prevCode = getPrevCode(parent);
118
+ return prevCode + (prevCode ? '.' : '') + parent.name + getParameters(value.options, replacer);
119
+ }
120
+ function getPrevCode(mockData) {
121
+ return mockData.parent ? getCode(mockData.parent, replacer, bumpCount) : '';
122
+ }
123
+ }
124
+ function createInternalMock(target, mockData) {
125
+ mockDatas.push(mockData);
126
+ target[mock] = mockData;
127
+ return new Proxy(target, {
128
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
129
+ set(target, p, value, receiver) {
130
+ const realValue = unwrapValue(value);
131
+ if (!mockData.sideEffects) {
132
+ mockData.sideEffects = [];
133
+ }
134
+ mockData.sideEffects.push({
135
+ useCount: 0,
136
+ name: p,
137
+ options: realValue,
138
+ parent: mockData,
139
+ source: 3 /* MockDataSource.set */,
140
+ generated: false
141
+ });
142
+ ++mockData.useCount;
143
+ Reflect.set(target, p, realValue, receiver);
144
+ return true;
145
+ },
146
+ get(target, p) {
147
+ if (p === unwrap)
148
+ return target;
149
+ if (p === mock)
150
+ return mockData;
151
+ const value = Reflect.get(target, p);
152
+ if (value === null || (typeof value !== 'object' && typeof value !== 'function')) {
153
+ return value;
154
+ }
155
+ if (!mockData.mocks) {
156
+ mockData.mocks = Object.create(null);
157
+ }
158
+ if (!mockData.mocks[p]) {
159
+ mockData.mocks[p] = createInternalMock(value, {
160
+ useCount: 0,
161
+ name: p,
162
+ parent: mockData,
163
+ source: 1 /* MockDataSource.get */,
164
+ generated: false
165
+ });
166
+ }
167
+ const result = mockData.mocks[p];
168
+ ++mockData.useCount;
169
+ return result;
170
+ },
171
+ // eslint-disable-next-line @typescript-eslint/ban-types
172
+ construct(target, argArray, newTarget) {
173
+ const realTarget = unwrapValue(newTarget);
174
+ const realArguments = unwrapValue(argArray);
175
+ ++mockData.useCount;
176
+ const result = Reflect.construct(target, realArguments, realTarget);
177
+ return createInternalMock(result, {
178
+ useCount: 0,
179
+ name: '',
180
+ options: realArguments,
181
+ target: realTarget,
182
+ parent: mockData,
183
+ source: 8 /* MockDataSource.construct */,
184
+ generated: false
185
+ });
186
+ },
187
+ defineProperty(target, property, attributes) {
188
+ const realValue = unwrapValue(attributes);
189
+ // if (!mockData.sideEffects) {
190
+ // mockData.sideEffects = [];
191
+ // }
192
+ // mockData.sideEffects.push({
193
+ // useCount: 0,
194
+ // name: property,
195
+ // options: realValue,
196
+ // parent: mockData,
197
+ // source: MockDataSource.defineProperty,
198
+ // generated: false
199
+ // });
200
+ // ++mockData.useCount;
201
+ return Reflect.defineProperty(target, property, realValue);
202
+ },
203
+ deleteProperty(target, p) {
204
+ if (!mockData.sideEffects) {
205
+ mockData.sideEffects = [];
206
+ }
207
+ mockData.sideEffects.push({
208
+ useCount: 0,
209
+ name: p,
210
+ options: undefined,
211
+ parent: mockData,
212
+ source: 5 /* MockDataSource.deleteProperty */,
213
+ generated: false
214
+ });
215
+ ++mockData.useCount;
216
+ const result = Reflect.deleteProperty(target, p);
217
+ return result;
218
+ },
219
+ setPrototypeOf(target, v) {
220
+ const realValue = unwrapValue(v);
221
+ if (!mockData.sideEffects) {
222
+ mockData.sideEffects = [];
223
+ }
224
+ mockData.sideEffects.push({
225
+ useCount: 0,
226
+ name: '',
227
+ options: realValue,
228
+ parent: mockData,
229
+ source: 6 /* MockDataSource.setPrototypeOf */,
230
+ generated: false
231
+ });
232
+ ++mockData.useCount;
233
+ return Reflect.setPrototypeOf(target, realValue);
234
+ },
235
+ preventExtensions(target) {
236
+ if (!mockData.sideEffects) {
237
+ mockData.sideEffects = [];
238
+ }
239
+ mockData.sideEffects.push({
240
+ useCount: 0,
241
+ name: '',
242
+ options: undefined,
243
+ parent: mockData,
244
+ source: 7 /* MockDataSource.preventExtensions */,
245
+ generated: false
246
+ });
247
+ ++mockData.useCount;
248
+ return Reflect.preventExtensions(target);
249
+ },
250
+ apply(target, thisArg, argumentsList) {
251
+ const realThis = unwrapValue(thisArg);
252
+ const realArguments = unwrapValue(argumentsList);
253
+ ++mockData.useCount;
254
+ return createInternalMock(Reflect.apply(target, realThis, realArguments), {
255
+ useCount: 0,
256
+ name: '',
257
+ parent: mockData,
258
+ source: 2 /* MockDataSource.call */,
259
+ options: realArguments,
260
+ generated: false
261
+ });
262
+ }
263
+ });
264
+ }
265
+ function getNextInstanceName() {
266
+ return `tmp_${counter++}`;
267
+ }
268
+ }
269
+ function getParameters(options, replacer) {
270
+ return `(${options.length ? options.map(value => stringify(value, replacer)).join(',') : ''})`;
271
+ }
272
+ function stringify(value, replacer) {
273
+ const original = value;
274
+ value = replacer(value);
275
+ if (original !== value && typeof value === 'string') {
276
+ return value;
277
+ }
278
+ if (value === null) {
279
+ return null;
280
+ }
281
+ if (value === undefined) {
282
+ return undefined;
283
+ }
284
+ if (typeof value === 'number') {
285
+ return `${value}`;
286
+ }
287
+ if (Array.isArray(value)) {
288
+ return `[${value.map((v) => stringify(v, replacer)).join(',')}]`;
289
+ }
290
+ if (typeof value === 'boolean') {
291
+ return value;
292
+ }
293
+ if (typeof value === 'function') {
294
+ return value.toString();
295
+ }
296
+ if (value instanceof RegExp) {
297
+ return value;
298
+ }
299
+ if (typeof value === 'object') {
300
+ return `{${Object.entries(value).map(([k, v]) => k + ':' + stringify(v, replacer)).join(',')}}`;
301
+ }
302
+ return '"' + String(value) + '"';
303
+ }
304
+
305
+ export { createRecordingMockFactory as default };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "version": "0.1.0",
3
+ "license": "MIT",
4
+ "name": "@slimlib/smart-mock",
5
+ "author": "Konstantin Shutkin",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.cjs",
10
+ "default": "./dist/index.mjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.mjs",
16
+ "typings": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist",
19
+ "LICENSE"
20
+ ],
21
+ "engines": {
22
+ "node": ">=15"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/kshutkin/slimlib.git"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/kshutkin/slimlib/issues"
30
+ },
31
+ "homepage": "https://github.com/kshutkin/slimlib/blob/main/smart-mock/README.md",
32
+ "readme": "README.md",
33
+ "description": "One more proxy based mock",
34
+ "keywords": [
35
+ "@slimlib",
36
+ "mock",
37
+ "smart-mock"
38
+ ],
39
+ "scripts": {
40
+ "build": "pkgbld-internal",
41
+ "test": "jest --collectCoverage",
42
+ "lint": "eslint ./src"
43
+ }
44
+ }