@discomedia/utils 1.0.51 → 1.0.52
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/index-frontend.cjs +12 -5
- package/dist/index-frontend.cjs.map +1 -1
- package/dist/index-frontend.mjs +12 -5
- package/dist/index-frontend.mjs.map +1 -1
- package/dist/index.cjs +12 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +12 -5
- package/dist/index.mjs.map +1 -1
- package/dist/package.json +1 -1
- package/dist/test.js +39 -8
- package/dist/test.js.map +1 -1
- package/dist/types/market-time.d.ts +6 -6
- package/dist/types/market-time.d.ts.map +1 -1
- package/dist/types/types/market-time-types.d.ts +5 -0
- package/dist/types/types/market-time-types.d.ts.map +1 -1
- package/dist/types-frontend/market-time.d.ts +6 -6
- package/dist/types-frontend/market-time.d.ts.map +1 -1
- package/dist/types-frontend/types/market-time-types.d.ts +5 -0
- package/dist/types-frontend/types/market-time-types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/package.json
CHANGED
package/dist/test.js
CHANGED
|
@@ -967,10 +967,13 @@ function countTradingDays(startDate, endDate = new Date()) {
|
|
|
967
967
|
/**
|
|
968
968
|
* Returns the trading day N days back from a reference date, along with its market open time.
|
|
969
969
|
* Trading days are counted as full or half trading days (days that end count as 1 full trading day).
|
|
970
|
+
* By default, the most recent completed trading day counts as day 1.
|
|
971
|
+
* Set includeMostRecentFullDay to false to count strictly before that day.
|
|
970
972
|
*
|
|
971
973
|
* @param options - Object with:
|
|
972
974
|
* - referenceDate: Date to count back from (default: now)
|
|
973
|
-
* - days: Number of trading days to go back (must be >= 1)
|
|
975
|
+
* - days: Number of trading days to go back (must be an integer >= 1)
|
|
976
|
+
* - includeMostRecentFullDay: Whether to include the most recent completed trading day (default: true)
|
|
974
977
|
* @returns Object containing:
|
|
975
978
|
* - date: Trading date in YYYY-MM-DD format
|
|
976
979
|
* - marketOpenISO: Market open time as ISO string (e.g., "2025-11-15T13:30:00.000Z")
|
|
@@ -992,13 +995,17 @@ function countTradingDays(startDate, endDate = new Date()) {
|
|
|
992
995
|
*/
|
|
993
996
|
function getTradingDaysBack(options) {
|
|
994
997
|
const calendar = new MarketCalendar();
|
|
995
|
-
const
|
|
996
|
-
const
|
|
997
|
-
|
|
998
|
-
|
|
998
|
+
const { referenceDate, days, includeMostRecentFullDay = true } = options;
|
|
999
|
+
const refDate = referenceDate || new Date();
|
|
1000
|
+
const daysBack = days;
|
|
1001
|
+
if (!Number.isInteger(daysBack) || daysBack < 1) {
|
|
1002
|
+
throw new Error('days must be an integer >= 1');
|
|
999
1003
|
}
|
|
1000
1004
|
// Start from the last full trading date relative to reference
|
|
1001
1005
|
let targetDate = getLastFullTradingDateImpl(refDate);
|
|
1006
|
+
if (!includeMostRecentFullDay) {
|
|
1007
|
+
targetDate = calendar.getPreviousMarketDay(targetDate);
|
|
1008
|
+
}
|
|
1002
1009
|
// Go back the specified number of days (we're already at day 1, so go back days-1 more)
|
|
1003
1010
|
for (let i = 1; i < daysBack; i++) {
|
|
1004
1011
|
targetDate = calendar.getPreviousMarketDay(targetDate);
|
|
@@ -19642,12 +19649,32 @@ function testGetTradingDaysBack() {
|
|
|
19642
19649
|
marketOpenISO: '2025-07-03T13:30:00.000Z', // Still opens at normal time
|
|
19643
19650
|
},
|
|
19644
19651
|
},
|
|
19652
|
+
{
|
|
19653
|
+
label: '1 day back from Friday after close (exclude most recent full day)',
|
|
19654
|
+
referenceDate: '2025-07-11T18:00:00-04:00',
|
|
19655
|
+
days: 1,
|
|
19656
|
+
includeMostRecentFullDay: false,
|
|
19657
|
+
expected: {
|
|
19658
|
+
date: '2025-07-10',
|
|
19659
|
+
marketOpenISO: '2025-07-10T13:30:00.000Z',
|
|
19660
|
+
},
|
|
19661
|
+
},
|
|
19662
|
+
{
|
|
19663
|
+
label: '3 days back from Monday after close (exclude most recent full day)',
|
|
19664
|
+
referenceDate: '2025-07-14T18:00:00-04:00',
|
|
19665
|
+
days: 3,
|
|
19666
|
+
includeMostRecentFullDay: false,
|
|
19667
|
+
expected: {
|
|
19668
|
+
date: '2025-07-09',
|
|
19669
|
+
marketOpenISO: '2025-07-09T13:30:00.000Z',
|
|
19670
|
+
},
|
|
19671
|
+
},
|
|
19645
19672
|
];
|
|
19646
19673
|
console.log('\n=== Testing getTradingDaysBack ===\n');
|
|
19647
|
-
for (const { label, referenceDate, days, expected } of testCases) {
|
|
19674
|
+
for (const { label, referenceDate, days, includeMostRecentFullDay, expected } of testCases) {
|
|
19648
19675
|
try {
|
|
19649
19676
|
const refDate = new Date(referenceDate);
|
|
19650
|
-
const result = disco.time.getTradingDaysBack({ referenceDate: refDate, days });
|
|
19677
|
+
const result = disco.time.getTradingDaysBack({ referenceDate: refDate, days, includeMostRecentFullDay });
|
|
19651
19678
|
const dateMatches = result.date === expected.date;
|
|
19652
19679
|
const isoMatches = result.marketOpenISO === expected.marketOpenISO;
|
|
19653
19680
|
const unixValid = typeof result.unixTimestamp === 'number' && result.unixTimestamp > 0;
|
|
@@ -19678,7 +19705,11 @@ function testGetTradingDaysBack() {
|
|
|
19678
19705
|
const passedTests = testCases.filter((tc) => {
|
|
19679
19706
|
try {
|
|
19680
19707
|
const refDate = new Date(tc.referenceDate);
|
|
19681
|
-
const result = disco.time.getTradingDaysBack({
|
|
19708
|
+
const result = disco.time.getTradingDaysBack({
|
|
19709
|
+
referenceDate: refDate,
|
|
19710
|
+
days: tc.days,
|
|
19711
|
+
includeMostRecentFullDay: tc.includeMostRecentFullDay,
|
|
19712
|
+
});
|
|
19682
19713
|
return result.date === tc.expected.date && result.marketOpenISO === tc.expected.marketOpenISO;
|
|
19683
19714
|
}
|
|
19684
19715
|
catch {
|