@devexperts/dxcharts-lite 2.7.15 → 2.7.16
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/dist/chart/components/chart/price-formatters/__tests__/treasury-price.formatter.test.d.ts +6 -0
- package/dist/chart/components/chart/price-formatters/__tests__/treasury-price.formatter.test.js +94 -0
- package/dist/chart/components/chart/price-formatters/treasury-price.formatter.d.ts +9 -0
- package/dist/chart/components/chart/price-formatters/treasury-price.formatter.js +22 -1
- package/dist/dxchart.min.js +4 -4
- package/package.json +1 -1
package/dist/chart/components/chart/price-formatters/__tests__/treasury-price.formatter.test.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
|
4
|
+
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
export {};
|
package/dist/chart/components/chart/price-formatters/__tests__/treasury-price.formatter.test.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
|
4
|
+
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
import { treasuryPriceFormatter, parseTreasuryPrice, isTreasuryPriceFormat, TREASURY_32ND } from '../treasury-price.formatter';
|
|
7
|
+
describe('Treasury Price Formatter', () => {
|
|
8
|
+
describe('treasuryPriceFormatter', () => {
|
|
9
|
+
it('should format decimal prices to treasury format', () => {
|
|
10
|
+
expect(treasuryPriceFormatter(132.0)).toBe("132'00");
|
|
11
|
+
expect(treasuryPriceFormatter(132.0625)).toBe("132'02");
|
|
12
|
+
expect(treasuryPriceFormatter(132.3125)).toBe("132'10");
|
|
13
|
+
expect(treasuryPriceFormatter(132.5)).toBe("132'16");
|
|
14
|
+
expect(treasuryPriceFormatter(132.96875)).toBe("132'31");
|
|
15
|
+
});
|
|
16
|
+
it('should handle edge cases', () => {
|
|
17
|
+
expect(treasuryPriceFormatter(0)).toBe("0'00");
|
|
18
|
+
expect(treasuryPriceFormatter(0.03125)).toBe("0'01");
|
|
19
|
+
expect(treasuryPriceFormatter(1.0)).toBe("1'00");
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
describe('parseTreasuryPrice', () => {
|
|
23
|
+
it('should parse treasury format back to decimal', () => {
|
|
24
|
+
expect(parseTreasuryPrice("132'00")).toBe(132.0);
|
|
25
|
+
expect(parseTreasuryPrice("132'02")).toBe(132.0625);
|
|
26
|
+
expect(parseTreasuryPrice("132'10")).toBe(132.3125);
|
|
27
|
+
expect(parseTreasuryPrice("132'16")).toBe(132.5);
|
|
28
|
+
expect(parseTreasuryPrice("132'31")).toBe(132.96875);
|
|
29
|
+
});
|
|
30
|
+
it('should handle edge cases', () => {
|
|
31
|
+
expect(parseTreasuryPrice("0'00")).toBe(0);
|
|
32
|
+
expect(parseTreasuryPrice("0'01")).toBe(0.03125);
|
|
33
|
+
expect(parseTreasuryPrice("1'00")).toBe(1.0);
|
|
34
|
+
});
|
|
35
|
+
it('should return NaN for invalid formats', () => {
|
|
36
|
+
expect(parseTreasuryPrice("invalid")).toBe(NaN);
|
|
37
|
+
expect(parseTreasuryPrice("132'")).toBe(NaN);
|
|
38
|
+
expect(parseTreasuryPrice("'10")).toBe(NaN);
|
|
39
|
+
expect(parseTreasuryPrice("132'1")).toBe(NaN);
|
|
40
|
+
expect(parseTreasuryPrice("132'100")).toBe(NaN);
|
|
41
|
+
expect(parseTreasuryPrice("abc123")).toBe(NaN);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe('isTreasuryPriceFormat', () => {
|
|
45
|
+
it('should identify valid treasury format strings', () => {
|
|
46
|
+
expect(isTreasuryPriceFormat("132'00")).toBe(true);
|
|
47
|
+
expect(isTreasuryPriceFormat("132'10")).toBe(true);
|
|
48
|
+
expect(isTreasuryPriceFormat("0'00")).toBe(true);
|
|
49
|
+
expect(isTreasuryPriceFormat("1'31")).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
it('should reject invalid formats', () => {
|
|
52
|
+
expect(isTreasuryPriceFormat("invalid")).toBe(false);
|
|
53
|
+
expect(isTreasuryPriceFormat("132")).toBe(false);
|
|
54
|
+
expect(isTreasuryPriceFormat("132'")).toBe(false);
|
|
55
|
+
expect(isTreasuryPriceFormat("'10")).toBe(false);
|
|
56
|
+
expect(isTreasuryPriceFormat("132'1")).toBe(false);
|
|
57
|
+
expect(isTreasuryPriceFormat("132'100")).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
describe('TREASURY_32ND constant', () => {
|
|
61
|
+
it('should equal 1/32', () => {
|
|
62
|
+
expect(TREASURY_32ND).toBe(1 / 32);
|
|
63
|
+
expect(TREASURY_32ND).toBe(0.03125);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('Round trip conversion', () => {
|
|
67
|
+
it('should maintain precision through format and parse cycle', () => {
|
|
68
|
+
const originalPrice = 234.3125;
|
|
69
|
+
const formatted = treasuryPriceFormatter(originalPrice);
|
|
70
|
+
const parsed = parseTreasuryPrice(formatted);
|
|
71
|
+
expect(parsed).toBe(originalPrice);
|
|
72
|
+
});
|
|
73
|
+
it('should work with various precision levels', () => {
|
|
74
|
+
const testCases = [
|
|
75
|
+
132.0, // 132'00
|
|
76
|
+
132.03125, // 132'01
|
|
77
|
+
132.0625, // 132'02
|
|
78
|
+
132.09375, // 132'03
|
|
79
|
+
132.125, // 132'04
|
|
80
|
+
132.15625, // 132'05
|
|
81
|
+
132.1875, // 132'06
|
|
82
|
+
132.21875, // 132'07
|
|
83
|
+
132.25, // 132'08
|
|
84
|
+
132.28125, // 132'09
|
|
85
|
+
132.3125, // 132'10
|
|
86
|
+
];
|
|
87
|
+
testCases.forEach(price => {
|
|
88
|
+
const formatted = treasuryPriceFormatter(price);
|
|
89
|
+
const parsed = parseTreasuryPrice(formatted);
|
|
90
|
+
expect(parsed).toBe(price);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -15,3 +15,12 @@
|
|
|
15
15
|
export declare const TREASURY_32ND: number;
|
|
16
16
|
export declare const treasuryPriceFormatter: (value: number) => string;
|
|
17
17
|
export declare const isTreasuryPriceFormat: (value: string) => boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Parses treasury price format back to decimal number
|
|
20
|
+
*
|
|
21
|
+
* Examples:
|
|
22
|
+
* 132'00 => 132.0
|
|
23
|
+
* 132'02 => 132.0625
|
|
24
|
+
* 132'10 => 132.3125
|
|
25
|
+
*/
|
|
26
|
+
export declare const parseTreasuryPrice: (value: string) => number;
|
|
@@ -24,4 +24,25 @@ export const treasuryPriceFormatter = (value) => {
|
|
|
24
24
|
const thirtySecondsFormatted = thirtySeconds.toString().padStart(2, '0');
|
|
25
25
|
return `${integerValue}'${thirtySecondsFormatted}`;
|
|
26
26
|
};
|
|
27
|
-
|
|
27
|
+
const getTreasuryPriceMatch = (value) => value.match(/^\-?0*(\d+)'(\d{2})$/);
|
|
28
|
+
export const isTreasuryPriceFormat = (value) => Boolean(getTreasuryPriceMatch(value));
|
|
29
|
+
/**
|
|
30
|
+
* Parses treasury price format back to decimal number
|
|
31
|
+
*
|
|
32
|
+
* Examples:
|
|
33
|
+
* 132'00 => 132.0
|
|
34
|
+
* 132'02 => 132.0625
|
|
35
|
+
* 132'10 => 132.3125
|
|
36
|
+
*/
|
|
37
|
+
export const parseTreasuryPrice = (value) => {
|
|
38
|
+
const match = getTreasuryPriceMatch(value);
|
|
39
|
+
if (match) {
|
|
40
|
+
const integerPart = parseInt(match[1], 10);
|
|
41
|
+
const thirtySeconds = parseInt(match[2], 10);
|
|
42
|
+
// Convert 32nds to decimal
|
|
43
|
+
const decimalPart = (thirtySeconds * TREASURY_32ND);
|
|
44
|
+
return integerPart + decimalPart;
|
|
45
|
+
}
|
|
46
|
+
// Return Number(value) for non-treasury formats (this will be NaN for invalid strings)
|
|
47
|
+
return Number(value);
|
|
48
|
+
};
|