@unchainedshop/utils 3.0.0-alpha7 → 3.0.0-rc2
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/lib/calculation.d.ts +0 -5
- package/lib/calculation.d.ts.map +1 -1
- package/lib/calculation.js +0 -20
- package/lib/calculation.js.map +1 -1
- package/lib/director/BaseDiscountAdapter.d.ts +3 -4
- package/lib/director/BaseDiscountAdapter.d.ts.map +1 -1
- package/lib/director/BaseDiscountAdapter.js.map +1 -1
- package/lib/director/BaseDiscountDirector.d.ts +3 -4
- package/lib/director/BaseDiscountDirector.d.ts.map +1 -1
- package/lib/director/BaseDiscountDirector.js.map +1 -1
- package/lib/director/BasePricingAdapter.d.ts +1 -5
- package/lib/director/BasePricingAdapter.d.ts.map +1 -1
- package/lib/director/BasePricingAdapter.js.map +1 -1
- package/lib/director/BasePricingDirector.d.ts +4 -5
- package/lib/director/BasePricingDirector.d.ts.map +1 -1
- package/lib/director/BasePricingDirector.js +1 -1
- package/lib/director/BasePricingDirector.js.map +1 -1
- package/lib/intersect-set.d.ts +3 -0
- package/lib/intersect-set.d.ts.map +1 -0
- package/lib/intersect-set.js +2 -0
- package/lib/intersect-set.js.map +1 -0
- package/lib/sha1.d.ts +2 -0
- package/lib/sha1.d.ts.map +1 -0
- package/lib/sha1.js +6 -0
- package/lib/sha1.js.map +1 -0
- package/lib/sha256.d.ts +2 -0
- package/lib/sha256.d.ts.map +1 -0
- package/lib/sha256.js +6 -0
- package/lib/sha256.js.map +1 -0
- package/lib/utils-index.d.ts +18 -6
- package/lib/utils-index.d.ts.map +1 -1
- package/lib/utils-index.js +3 -12
- package/lib/utils-index.js.map +1 -1
- package/lib/utils-index_BACKUP_3852.d.ts +35 -0
- package/lib/utils-index_BACKUP_3852.d.ts.map +1 -0
- package/lib/utils-index_BACKUP_3852.js +25 -0
- package/lib/utils-index_BACKUP_3852.js.map +1 -0
- package/lib/utils-index_BASE_3852.d.ts +27 -0
- package/lib/utils-index_BASE_3852.d.ts.map +1 -0
- package/lib/utils-index_BASE_3852.js +28 -0
- package/lib/utils-index_BASE_3852.js.map +1 -0
- package/lib/utils-index_LOCAL_3852.d.ts +35 -0
- package/lib/utils-index_LOCAL_3852.d.ts.map +1 -0
- package/lib/utils-index_LOCAL_3852.js +25 -0
- package/lib/utils-index_LOCAL_3852.js.map +1 -0
- package/lib/utils-index_REMOTE_3852.d.ts +31 -0
- package/lib/utils-index_REMOTE_3852.d.ts.map +1 -0
- package/lib/utils-index_REMOTE_3852.js +28 -0
- package/lib/utils-index_REMOTE_3852.js.map +1 -0
- package/package.json +8 -11
- package/src/calculation.test.ts +0 -43
- package/src/calculation.ts +0 -26
- package/src/intersect-set.ts +2 -0
- package/src/sha1.ts +5 -0
- package/src/sha256.ts +5 -0
- package/src/utils-index.test.ts +1 -1
- package/src/utils-index.ts +19 -16
- package/tsconfig.json +4 -4
- package/src/director/BaseDiscountAdapter.ts +0 -89
- package/src/director/BaseDiscountDirector.ts +0 -77
- package/src/director/BasePricingAdapter.ts +0 -89
- package/src/director/BasePricingDirector.ts +0 -133
- package/src/director/BasePricingSheet.ts +0 -121
- package/src/director/base-discount-adapter.test.ts +0 -49
- package/src/director/base-price-sheet.test.ts +0 -100
- package/src/random-value-hex.ts +0 -8
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
export interface PricingCalculation {
|
|
2
|
-
category: string;
|
|
3
|
-
amount: number;
|
|
4
|
-
baseCategory?: string;
|
|
5
|
-
meta?: any;
|
|
6
|
-
}
|
|
7
|
-
export interface PricingDiscount {
|
|
8
|
-
discountId: string;
|
|
9
|
-
amount: number;
|
|
10
|
-
currency: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface IPricingSheet<Calculation extends PricingCalculation>
|
|
14
|
-
extends IBasePricingSheet<Calculation> {
|
|
15
|
-
discountPrices: (discountId?: string) => Array<PricingDiscount>;
|
|
16
|
-
addDiscount: (params: { amount: number; discountId: string; meta?: any }) => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface IBasePricingSheet<Calculation extends PricingCalculation> {
|
|
20
|
-
calculation: Array<Calculation>;
|
|
21
|
-
currency?: string;
|
|
22
|
-
quantity?: number;
|
|
23
|
-
|
|
24
|
-
getRawPricingSheet: () => Array<Calculation>;
|
|
25
|
-
filterBy: (filter?: Partial<Calculation>) => Array<Calculation>;
|
|
26
|
-
isValid: () => boolean;
|
|
27
|
-
|
|
28
|
-
gross: () => number;
|
|
29
|
-
net: () => number;
|
|
30
|
-
sum: (filter?: Partial<Calculation>) => number;
|
|
31
|
-
total: (params?: { category?: string; discountId?: string; useNetPrice?: boolean }) => {
|
|
32
|
-
amount: number;
|
|
33
|
-
currency: string;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
taxSum: (filter?: Partial<Calculation>) => number;
|
|
37
|
-
|
|
38
|
-
resetCalculation: (sheetToInvert: IBasePricingSheet<Calculation>) => Array<Calculation>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface PricingSheetParams<Calculation extends PricingCalculation> {
|
|
42
|
-
calculation?: Array<Calculation>;
|
|
43
|
-
currency?: string;
|
|
44
|
-
quantity?: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export const BasePricingSheet = <Calculation extends PricingCalculation>(
|
|
48
|
-
params: PricingSheetParams<Calculation>,
|
|
49
|
-
): IBasePricingSheet<Calculation> => {
|
|
50
|
-
const pricingSheet: IBasePricingSheet<Calculation> = {
|
|
51
|
-
calculation: params.calculation || [],
|
|
52
|
-
currency: params.currency,
|
|
53
|
-
quantity: params.quantity,
|
|
54
|
-
|
|
55
|
-
getRawPricingSheet() {
|
|
56
|
-
return this.calculation;
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
isValid() {
|
|
60
|
-
return this.calculation.length > 0;
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
sum(filter) {
|
|
64
|
-
return this.filterBy(filter)
|
|
65
|
-
.filter(Boolean)
|
|
66
|
-
.reduce((sum: number, calculationRow: Calculation) => sum + calculationRow.amount, 0);
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
taxSum() {
|
|
70
|
-
return 0;
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
gross() {
|
|
74
|
-
// Sum contains taxes, thus it's gross when just suming everything
|
|
75
|
-
return this.sum();
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
net() {
|
|
79
|
-
// Remove all taxes from gross to get net
|
|
80
|
-
return this.sum() - this.taxSum();
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
total({ category, useNetPrice, discountId } = { useNetPrice: false }) {
|
|
84
|
-
const amount = this.sum({ category, discountId });
|
|
85
|
-
const taxAmountForCategory = this.taxSum({ baseCategory: category, discountId });
|
|
86
|
-
|
|
87
|
-
// Sum does not contain taxes when filtering by category, it's net in that case and gross if there is no category
|
|
88
|
-
const netAmount = !category ? amount - taxAmountForCategory : amount;
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
amount: Math.round(useNetPrice ? netAmount : netAmount + taxAmountForCategory),
|
|
92
|
-
currency: this.currency,
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
filterBy(filter) {
|
|
97
|
-
const filteredCalculation = Object.keys(filter || {}).reduce(
|
|
98
|
-
(oldCalculation, filterKey) =>
|
|
99
|
-
oldCalculation.filter(
|
|
100
|
-
(row: Calculation) =>
|
|
101
|
-
!!row && (filter[filterKey] === undefined || row[filterKey] === filter[filterKey]),
|
|
102
|
-
),
|
|
103
|
-
this.calculation,
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
return filteredCalculation;
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
resetCalculation(calculationSheet) {
|
|
110
|
-
calculationSheet.filterBy().forEach(({ amount, ...row }: Calculation) => {
|
|
111
|
-
this.calculation.push({
|
|
112
|
-
...row,
|
|
113
|
-
amount: amount * -1,
|
|
114
|
-
} as Calculation);
|
|
115
|
-
});
|
|
116
|
-
return this.calculation;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
return pricingSheet;
|
|
121
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { BaseDiscountAdapter } from './BaseDiscountAdapter.js';
|
|
2
|
-
|
|
3
|
-
describe('BaseDiscountAdapter instantiation', () => {
|
|
4
|
-
const adapter = BaseDiscountAdapter;
|
|
5
|
-
const context = null as any;
|
|
6
|
-
it('should initialize with default values', async () => {
|
|
7
|
-
expect(adapter.orderIndex).toEqual(0);
|
|
8
|
-
expect(adapter.log).toEqual(expect.any(Function));
|
|
9
|
-
expect(await adapter.isManualAdditionAllowed('')).toEqual(false);
|
|
10
|
-
expect(await adapter.isManualRemovalAllowed()).toEqual(false);
|
|
11
|
-
expect(await adapter.actions(context)).toEqual({
|
|
12
|
-
isValidForSystemTriggering: expect.any(Function),
|
|
13
|
-
reserve: expect.any(Function),
|
|
14
|
-
release: expect.any(Function),
|
|
15
|
-
isValidForCodeTriggering: expect.any(Function),
|
|
16
|
-
discountForPricingAdapterKey: expect.any(Function),
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('actions', () => {
|
|
21
|
-
it('isValidForSystemTriggering', async () => {
|
|
22
|
-
const result = await adapter.actions(context);
|
|
23
|
-
expect(await result.isValidForSystemTriggering()).toEqual(false);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('reserve', async () => {
|
|
27
|
-
const result = await adapter.actions(context);
|
|
28
|
-
expect(await result.reserve({ code: '' })).toEqual({});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('release', async () => {
|
|
32
|
-
const result = await adapter.actions(context);
|
|
33
|
-
expect(await result.release()).toEqual(null);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('isValidForCodeTriggering', async () => {
|
|
37
|
-
const result = await adapter.actions(context);
|
|
38
|
-
expect(await result.isValidForCodeTriggering({ code: '' })).toEqual(false);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('discountForPricingAdapterKey', async () => {
|
|
42
|
-
const result = await adapter.actions(context);
|
|
43
|
-
const calculationSheet = {};
|
|
44
|
-
expect(
|
|
45
|
-
await result.discountForPricingAdapterKey({ calculationSheet, pricingAdapterKey: 'key' } as any),
|
|
46
|
-
).toEqual(null);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
@@ -1,100 +0,0 @@
|
|
|
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
|
-
it('total() calculates the sum of the amounts in the calculations by default', () => {
|
|
26
|
-
expect(pricingSheet.total()).toEqual({ amount: 550, currency: undefined });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('total() calculates the sum of the amounts in the calculations for a given category', () => {
|
|
30
|
-
expect(pricingSheet.total({ category: 'item' })).toEqual({ amount: 400, currency: undefined });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('total() calculates the net sum of the amounts in the calculations if useNetPrice is true', () => {
|
|
34
|
-
expect(pricingSheet.total({ useNetPrice: true })).toEqual({ amount: 550, currency: undefined });
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('total() returns 0 if the calculation is empty', () => {
|
|
38
|
-
expect(BasePricingSheet({ calculation: [] }).total()).toEqual({ amount: 0, currency: undefined });
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('calculates the correct net amount', () => {
|
|
42
|
-
expect(pricingSheet.net()).toEqual(550);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('calculates the correct sum for a given category', () => {
|
|
46
|
-
expect(pricingSheet.sum({ category: 'item' })).toEqual(400);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('calculates the correct tax amount', () => {
|
|
50
|
-
expect(pricingSheet.taxSum()).toEqual(0);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe('getRawPricingSheet', () => {
|
|
54
|
-
it('returns the correct list of calculations', () => {
|
|
55
|
-
expect(pricingSheet.getRawPricingSheet()).toEqual(calculations);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('returns an empty list of calculations if none are provided', () => {
|
|
59
|
-
expect(BasePricingSheet({}).getRawPricingSheet()).toEqual([]);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
describe('isValid', () => {
|
|
64
|
-
it('returns true if the sheet has calculations', () => {
|
|
65
|
-
expect(pricingSheet.isValid()).toEqual(true);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('returns false if the sheet does not have calculations', () => {
|
|
69
|
-
expect(BasePricingSheet({}).isValid()).toEqual(false);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('filterBy', () => {
|
|
74
|
-
it('filters the list of calculations correctly', () => {
|
|
75
|
-
expect(pricingSheet.filterBy({ category: 'item' })).toEqual([ITEM1, ITEM2]);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('returns the full list of calculations if no filter is provided', () => {
|
|
79
|
-
expect(pricingSheet.filterBy()).toEqual(calculations);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe('resetCalculation', () => {
|
|
84
|
-
it('resets the list of calculations correctly', () => {
|
|
85
|
-
const expectedCalculations = calculations.reduce(
|
|
86
|
-
(prev, { amount, ...row }) => {
|
|
87
|
-
return [
|
|
88
|
-
...prev,
|
|
89
|
-
{
|
|
90
|
-
...row,
|
|
91
|
-
amount: -amount,
|
|
92
|
-
},
|
|
93
|
-
];
|
|
94
|
-
},
|
|
95
|
-
[...calculations],
|
|
96
|
-
);
|
|
97
|
-
expect(pricingSheet.resetCalculation(pricingSheet)).toEqual(expectedCalculations);
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
});
|
package/src/random-value-hex.ts
DELETED