@unchainedshop/utils 2.0.0-rc.4 → 2.0.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/jest.config.js +3 -0
- package/lib/find-localized-text.js +9 -3
- package/lib/find-localized-text.js.map +1 -1
- package/package.json +5 -5
- package/src/director/base-director.test.ts +129 -0
- package/src/director/base-discount-adapter.test.ts +52 -0
- package/src/director/base-price-sheet.test.ts +106 -0
- package/src/find-localized-text.ts +9 -3
- package/{tests → src}/utils-index.test.ts +1 -2
- package/tsconfig.json +5 -23
package/jest.config.js
CHANGED
|
@@ -19,19 +19,25 @@ const findLocalizedText = async (collection, selector, locale) => {
|
|
|
19
19
|
const cachedText = textCache.get(cacheKey);
|
|
20
20
|
if (cachedText)
|
|
21
21
|
return cachedText;
|
|
22
|
-
const exactTranslation = await collection.findOne(extendSelectorWithLocale(selector, locale), {
|
|
22
|
+
const exactTranslation = await collection.findOne(extendSelectorWithLocale(selector, locale), {
|
|
23
|
+
sort: { updated: -1 },
|
|
24
|
+
});
|
|
23
25
|
if (exactTranslation) {
|
|
24
26
|
textCache.set(cacheKey, exactTranslation);
|
|
25
27
|
return exactTranslation;
|
|
26
28
|
}
|
|
27
29
|
if (systemLocale.normalized !== locale.normalized) {
|
|
28
|
-
const fallbackTranslation = await collection.findOne(extendSelectorWithLocale(selector, systemLocale), {
|
|
30
|
+
const fallbackTranslation = await collection.findOne(extendSelectorWithLocale(selector, systemLocale), {
|
|
31
|
+
sort: { updated: -1 },
|
|
32
|
+
});
|
|
29
33
|
if (fallbackTranslation) {
|
|
30
34
|
textCache.set(cacheKey, fallbackTranslation);
|
|
31
35
|
return fallbackTranslation;
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
|
-
const foundText = await collection.findOne(selector, {
|
|
38
|
+
const foundText = await collection.findOne(selector, {
|
|
39
|
+
sort: { updated: -1 },
|
|
40
|
+
});
|
|
35
41
|
textCache.set(cacheKey, foundText);
|
|
36
42
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M,CAAC,EAAE;QAC5F,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE;KACtB,CAAC,CAAC;IACH,IAAI,gBAAgB,EAAE;QACpB,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;QAC1C,OAAO,gBAAqB,CAAC;KAC9B;IAED,IAAI,YAAY,CAAC,UAAU,KAAK,MAAM,CAAC,UAAU,EAAE;QACjD,MAAM,mBAAmB,GAAG,MAAM,UAAU,CAAC,OAAO,CAClD,wBAAwB,CAAC,QAAQ,EAAE,YAAY,CAAC,EAChD;YACE,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE;SACtB,CACF,CAAC;QACF,IAAI,mBAAmB,EAAE;YACvB,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,mBAAmB,CAAC,CAAC;YAC7C,OAAO,mBAAwB,CAAC;SACjC;KACF;IAED,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,OAAO,CAAC,QAAQ,EAAE;QACnD,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE;KACtB,CAAC,CAAC;IACH,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;IACnC,OAAO,SAAc,CAAC;AACxB,CAAC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/utils",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"main": "lib/utils-index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/utils-index.js",
|
|
@@ -30,18 +30,18 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@unchainedshop/logger": "^2.0.0
|
|
33
|
+
"@unchainedshop/logger": "^2.0.0",
|
|
34
34
|
"abort-controller": "3.0.0",
|
|
35
|
-
"bson": "^4.7.
|
|
35
|
+
"bson": "^4.7.2",
|
|
36
36
|
"hashids": "^2.2.10",
|
|
37
|
-
"jest": "^29.
|
|
37
|
+
"jest": "^29.4.1",
|
|
38
38
|
"locale": "0.1.0",
|
|
39
39
|
"lru-cache": "^7.14.1",
|
|
40
40
|
"simpl-schema": "=3.2.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^18.11.18",
|
|
44
|
-
"@unchainedshop/types": "^2.0.0
|
|
44
|
+
"@unchainedshop/types": "^2.0.0",
|
|
45
45
|
"typescript": "^4.9.4"
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { BaseDirector } from "./BaseDirector.js";
|
|
2
|
+
|
|
3
|
+
const adapter1 = {
|
|
4
|
+
key: 'adapter1',
|
|
5
|
+
label: 'Adapter 1',
|
|
6
|
+
version: '1',
|
|
7
|
+
log: import.meta.jest.fn(),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const adapter2 = {
|
|
11
|
+
key: 'adapter2',
|
|
12
|
+
label: 'Adapter 2',
|
|
13
|
+
version: '2',
|
|
14
|
+
log: import.meta.jest.fn(),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const adapter3 = {
|
|
18
|
+
key: 'adapter3',
|
|
19
|
+
label: 'Adapter 3',
|
|
20
|
+
version: '3',
|
|
21
|
+
log: import.meta.jest.fn(),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe('BaseDirector', () => {
|
|
25
|
+
let director;
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
director = BaseDirector('TestDirector');
|
|
29
|
+
director.registerAdapter(adapter1);
|
|
30
|
+
director.registerAdapter(adapter3);
|
|
31
|
+
director.registerAdapter(adapter2);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('registerAdapter', ()=> {
|
|
35
|
+
|
|
36
|
+
it('should add the adapter to the director', () => {
|
|
37
|
+
director.registerAdapter(adapter1);
|
|
38
|
+
expect(director.getAdapter(adapter1.key)).toBe(adapter1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
xit('should generate a log message when an adapter is registered', () => {
|
|
42
|
+
const spy = import.meta.jest.spyOn(console, 'log')
|
|
43
|
+
expect(spy).toHaveBeenCalledWith(
|
|
44
|
+
'TestDirector -> Registered adapter1 1.0.0 (Adapter 1)',
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should allow multiple adapters to be registered', () => {
|
|
49
|
+
expect(director.getAdapter(adapter1.key)).toBe(adapter1);
|
|
50
|
+
expect(director.getAdapter(adapter2.key)).toBe(adapter2);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should register an adapter and add it to the internal map', () => {
|
|
54
|
+
expect(director.getAdapter(adapter1.key)).toEqual(adapter1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should use the adapterKeyField specified in the options to store the adapter in the internal map', () => {
|
|
58
|
+
|
|
59
|
+
director = BaseDirector('TestDirector', {
|
|
60
|
+
adapterKeyField: 'label',
|
|
61
|
+
});
|
|
62
|
+
director.registerAdapter(adapter1);
|
|
63
|
+
expect(director.getAdapter(adapter1.label)).toEqual(adapter1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should return the correct Adapter object', () => {
|
|
67
|
+
const directorName = 'TestDirector';
|
|
68
|
+
const adapterKeyField = 'key';
|
|
69
|
+
director = BaseDirector(directorName, { adapterKeyField });
|
|
70
|
+
director.registerAdapter(adapter1);
|
|
71
|
+
|
|
72
|
+
expect(director.getAdapter(adapter1.key)).toEqual(adapter1);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should return undefined if the Adapter does not exist', () => {
|
|
76
|
+
const directorName = 'TestDirector';
|
|
77
|
+
const adapterKeyField = 'key';
|
|
78
|
+
director = BaseDirector(directorName, { adapterKeyField });
|
|
79
|
+
expect(director.getAdapter(adapter1.key)).toBeUndefined();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
describe('getAdapter', () => {
|
|
86
|
+
it('should return the correct Adapter object for a given key', () => {
|
|
87
|
+
// Expect that calling getAdapter with the key 'foo' returns the fooAdapter
|
|
88
|
+
expect(director.getAdapter(adapter1.key)).toBe(adapter1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should return undefined if no Adapter is registered with the given key', () => {
|
|
92
|
+
// Expect that calling getAdapter with a key that has not been registered returns undefined
|
|
93
|
+
expect(director.getAdapter('invalid-key')).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should return the correct Adapter object when the keyField is set to a property other than "key"', () => {
|
|
97
|
+
// Create a director with the keyField option set to 'label'
|
|
98
|
+
director = BaseDirector('TestDirector', { adapterKeyField: 'label' });
|
|
99
|
+
director.registerAdapter(adapter1);
|
|
100
|
+
expect(director.getAdapter(adapter1.label)).toBe(adapter1);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('getAdapters', () => {
|
|
105
|
+
it('should return an array of registered Adapter objects', () => {
|
|
106
|
+
expect(director.getAdapters()).toEqual([adapter1, adapter3, adapter2]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should return an empty array if no Adapter objects have been registered', () => {
|
|
110
|
+
const directorName = 'TestDirector';
|
|
111
|
+
const adapterKeyField = 'key';
|
|
112
|
+
const baseDirector = BaseDirector(directorName, { adapterKeyField });
|
|
113
|
+
expect(baseDirector.getAdapters()).toEqual([]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should return an array of registered Adapter objects sorted by the adapterSortKey provided', () => {
|
|
117
|
+
director = BaseDirector('TestDirector', { adapterSortKey: 'version' });
|
|
118
|
+
director.registerAdapter(adapter1);
|
|
119
|
+
director.registerAdapter(adapter3);
|
|
120
|
+
director.registerAdapter(adapter2);
|
|
121
|
+
expect(director.getAdapters()).toEqual([adapter1, adapter2, adapter3]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { BaseDiscountAdapter } from "./BaseDiscountAdapter.js";
|
|
2
|
+
|
|
3
|
+
describe("BaseDiscountAdapter instantiation", () => {
|
|
4
|
+
const adapter = BaseDiscountAdapter;
|
|
5
|
+
const context = null
|
|
6
|
+
it("should initialize with default values", () => {
|
|
7
|
+
expect(adapter.orderIndex).toEqual(0);
|
|
8
|
+
expect(adapter.log).toEqual(expect.any(Function));
|
|
9
|
+
expect(adapter.isManualAdditionAllowed('')).resolves.toEqual(false);
|
|
10
|
+
expect(adapter.isManualRemovalAllowed()).resolves.toEqual(false);
|
|
11
|
+
|
|
12
|
+
expect(adapter.actions(context)).resolves.toEqual({
|
|
13
|
+
isValidForSystemTriggering: expect.any(Function),
|
|
14
|
+
reserve: expect.any(Function),
|
|
15
|
+
release: expect.any(Function),
|
|
16
|
+
isValidForCodeTriggering: expect.any(Function),
|
|
17
|
+
discountForPricingAdapterKey: expect.any(Function),
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('actions', ()=> {
|
|
22
|
+
const actions = adapter.actions(context)
|
|
23
|
+
|
|
24
|
+
it('isValidForSystemTriggering', async () => {
|
|
25
|
+
const result = await adapter.actions(context);
|
|
26
|
+
|
|
27
|
+
expect(await result.isValidForSystemTriggering()).toEqual(false)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('reserve', async () => {
|
|
31
|
+
const result = await adapter.actions(context);
|
|
32
|
+
expect(await result.reserve({code: ''})).toEqual({})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('release', async () => {
|
|
36
|
+
const result = await adapter.actions(context);
|
|
37
|
+
expect(await result.release()).toEqual(null)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('isValidForCodeTriggering', async () => {
|
|
41
|
+
const result = await adapter.actions(context);
|
|
42
|
+
expect(await result.isValidForCodeTriggering({code: ''})).toEqual(false)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('discountForPricingAdapterKey', async () => {
|
|
46
|
+
const result = await adapter.actions(context);
|
|
47
|
+
const calculationSheet = {}
|
|
48
|
+
expect(await result.discountForPricingAdapterKey({calculationSheet, pricingAdapterKey: 'key'} as any)).toEqual(null)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
})
|
|
52
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { BasePricingSheet } from "./BasePricingSheet.js";
|
|
2
|
+
|
|
3
|
+
describe('BasePricingSheet', () => {
|
|
4
|
+
let pricingSheet;
|
|
5
|
+
const TAX = { category: 'tax', amount: 100 };
|
|
6
|
+
const DISCOUNT = { category: 'discount', amount: 50 };
|
|
7
|
+
const ITEM1 = { category: 'item', amount: 200 };
|
|
8
|
+
const ITEM2 = { category: 'item', amount: 200 };
|
|
9
|
+
const calculations = [TAX, DISCOUNT, ITEM1, ITEM2];
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
pricingSheet = BasePricingSheet({
|
|
13
|
+
calculation: calculations,
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('gross() calculates the sum of the amounts in the calculations', () => {
|
|
18
|
+
expect(pricingSheet.gross()).toEqual(550);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('gross() returns 0 if the calculation is empty', () => {
|
|
22
|
+
expect(BasePricingSheet({ calculation: [] }).gross()).toEqual(0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
it('total() calculates the sum of the amounts in the calculations by default', () => {
|
|
28
|
+
expect(pricingSheet.total()).toEqual({ amount: 550, currency: undefined });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('total() calculates the sum of the amounts in the calculations for a given category', () => {
|
|
32
|
+
expect(pricingSheet.total({ category: 'item' })).toEqual({ amount: 400, currency: undefined });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('total() calculates the net sum of the amounts in the calculations if useNetPrice is true', () => {
|
|
36
|
+
expect(pricingSheet.total({ useNetPrice: true })).toEqual({ amount: 550, currency: undefined });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('total() returns 0 if the calculation is empty', () => {
|
|
40
|
+
expect(BasePricingSheet({ calculation: [] }).total()).toEqual({ amount: 0, currency: undefined });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('calculates the correct net amount', () => {
|
|
44
|
+
expect(pricingSheet.net()).toEqual(550);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('calculates the correct sum for a given category', () => {
|
|
48
|
+
expect(pricingSheet.sum({ category: 'item' })).toEqual(400);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('calculates the correct tax amount', () => {
|
|
52
|
+
expect(pricingSheet.taxSum()).toEqual(0);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
describe('getRawPricingSheet', () => {
|
|
58
|
+
it('returns the correct list of calculations', () => {
|
|
59
|
+
expect(pricingSheet.getRawPricingSheet()).toEqual(calculations);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('returns an empty list of calculations if none are provided', () => {
|
|
63
|
+
expect(BasePricingSheet({}).getRawPricingSheet()).toEqual([]);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
describe('isValid', () => {
|
|
69
|
+
it('returns true if the sheet has calculations', () => {
|
|
70
|
+
expect(pricingSheet.isValid()).toEqual(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('returns false if the sheet does not have calculations', () => {
|
|
74
|
+
expect(BasePricingSheet({}).isValid()).toEqual(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('filterBy', () => {
|
|
79
|
+
it('filters the list of calculations correctly', () => {
|
|
80
|
+
expect(pricingSheet.filterBy({ category: 'item' })).toEqual([ITEM1, ITEM2]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('returns the full list of calculations if no filter is provided', () => {
|
|
84
|
+
expect(pricingSheet.filterBy()).toEqual(calculations);
|
|
85
|
+
})
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
describe('resetCalculation', () => {
|
|
91
|
+
it('resets the list of calculations correctly', () => {
|
|
92
|
+
const expectedCalculations = calculations.reduce((prev,{ amount, ...row}) => {
|
|
93
|
+
return [
|
|
94
|
+
...prev,
|
|
95
|
+
{
|
|
96
|
+
...row,
|
|
97
|
+
amount: -amount
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
}, [...calculations])
|
|
101
|
+
expect(pricingSheet.resetCalculation(pricingSheet)).toEqual(expectedCalculations);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
});
|
|
@@ -31,7 +31,9 @@ const findLocalizedText = async <T extends Document>(
|
|
|
31
31
|
|
|
32
32
|
if (cachedText) return cachedText as T;
|
|
33
33
|
|
|
34
|
-
const exactTranslation = await collection.findOne(extendSelectorWithLocale(selector, locale), {
|
|
34
|
+
const exactTranslation = await collection.findOne(extendSelectorWithLocale(selector, locale), {
|
|
35
|
+
sort: { updated: -1 },
|
|
36
|
+
});
|
|
35
37
|
if (exactTranslation) {
|
|
36
38
|
textCache.set(cacheKey, exactTranslation);
|
|
37
39
|
return exactTranslation as T;
|
|
@@ -40,7 +42,9 @@ const findLocalizedText = async <T extends Document>(
|
|
|
40
42
|
if (systemLocale.normalized !== locale.normalized) {
|
|
41
43
|
const fallbackTranslation = await collection.findOne(
|
|
42
44
|
extendSelectorWithLocale(selector, systemLocale),
|
|
43
|
-
{
|
|
45
|
+
{
|
|
46
|
+
sort: { updated: -1 },
|
|
47
|
+
},
|
|
44
48
|
);
|
|
45
49
|
if (fallbackTranslation) {
|
|
46
50
|
textCache.set(cacheKey, fallbackTranslation);
|
|
@@ -48,7 +52,9 @@ const findLocalizedText = async <T extends Document>(
|
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
const foundText = await collection.findOne(selector, {
|
|
55
|
+
const foundText = await collection.findOne(selector, {
|
|
56
|
+
sort: { updated: -1 },
|
|
57
|
+
});
|
|
52
58
|
textCache.set(cacheKey, foundText);
|
|
53
59
|
return foundText as T;
|
|
54
60
|
};
|
|
@@ -15,11 +15,10 @@ import {
|
|
|
15
15
|
resolveBestCountry,
|
|
16
16
|
resolveBestSupported,
|
|
17
17
|
resolveUserRemoteAddress,
|
|
18
|
-
|
|
19
18
|
slugify,
|
|
20
19
|
systemLocale,
|
|
21
20
|
} from '@unchainedshop/utils';
|
|
22
|
-
import generateHashId from '
|
|
21
|
+
import generateHashId from './generate-random-hash.js'
|
|
23
22
|
|
|
24
23
|
describe('Utils', () => {
|
|
25
24
|
|
package/tsconfig.json
CHANGED
|
@@ -1,29 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
+
"extends": "../shared/base.tsconfig.json",
|
|
2
3
|
"compilerOptions": {
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"experimentalDecorators": true,
|
|
8
|
-
"forceConsistentCasingInFileNames": true,
|
|
9
|
-
"lib": [
|
|
10
|
-
"esnext"
|
|
11
|
-
],
|
|
12
|
-
"module": "Node16",
|
|
13
|
-
"moduleResolution": "Node16",
|
|
14
|
-
"noImplicitReturns": true,
|
|
15
|
-
"noUnusedLocals": false,
|
|
16
|
-
"outDir": "lib",
|
|
17
|
-
"preserveWatchOutput": true,
|
|
18
|
-
"skipLibCheck": true,
|
|
19
|
-
"sourceMap": true,
|
|
20
|
-
"target": "ES2022",
|
|
21
|
-
"types": [
|
|
22
|
-
"node",
|
|
23
|
-
"jest"
|
|
24
|
-
]
|
|
4
|
+
"rootDir": "src",
|
|
5
|
+
"outDir": "lib"
|
|
25
6
|
},
|
|
7
|
+
"exclude": ["**/*.test.ts", "**/*.test.js", "tests"],
|
|
26
8
|
"include": [
|
|
27
|
-
"src"
|
|
9
|
+
"./src"
|
|
28
10
|
]
|
|
29
11
|
}
|