@polymarbot/shared 0.3.5 → 0.3.7

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/markets/utils.cjs CHANGED
@@ -600,7 +600,12 @@ __export(utils_exports, {
600
600
  generate1dSlug: () => generate1dSlug,
601
601
  generate1hSlug: () => generate1hSlug,
602
602
  generate4hSlug: () => generate4hSlug,
603
- generateMarketUrl: () => generateMarketUrl
603
+ generateMarketUrl: () => generateMarketUrl,
604
+ getIntervalSeconds: () => getIntervalSeconds,
605
+ parseIntervalFromSlug: () => parseIntervalFromSlug,
606
+ parseSlug: () => parseSlug,
607
+ parseSymbolFromSlug: () => parseSymbolFromSlug,
608
+ parseTimeRangeFromSlug: () => parseTimeRangeFromSlug
604
609
  });
605
610
  module.exports = __toCommonJS(utils_exports);
606
611
 
@@ -680,6 +685,119 @@ function calculatePeriodStartInET(intervalSeconds) {
680
685
  const periodStartInET = Math.floor(nowInET / intervalSeconds) * intervalSeconds;
681
686
  return periodStartInET + offset;
682
687
  }
688
+ function parseSymbolFromSlug(slug) {
689
+ const lowerSlug = slug.toLowerCase();
690
+ for (const sym of SUPPORTED_SYMBOLS) {
691
+ const shortForm = sym.toLowerCase();
692
+ if (lowerSlug.startsWith(`${shortForm}-updown-`)) {
693
+ return sym;
694
+ }
695
+ }
696
+ for (const sym of SUPPORTED_SYMBOLS) {
697
+ const fullName = SYMBOL_FULL_NAME_MAP[sym];
698
+ if (fullName && lowerSlug.startsWith(`${fullName}-up-or-down-`)) {
699
+ return sym;
700
+ }
701
+ const shortForm = sym.toLowerCase();
702
+ if (lowerSlug.startsWith(`${shortForm}-up-or-down-`)) {
703
+ return sym;
704
+ }
705
+ }
706
+ return null;
707
+ }
708
+ function parseIntervalFromSlug(slug) {
709
+ const lowerSlug = slug.toLowerCase();
710
+ if (lowerSlug.includes("-updown-15m-")) {
711
+ return "15m" /* M15 */;
712
+ }
713
+ if (lowerSlug.includes("-updown-4h-")) {
714
+ return "4h" /* H4 */;
715
+ }
716
+ if (lowerSlug.match(/-up-or-down-\w+-\d+-\d+(am|pm)-et$/)) {
717
+ return "1h" /* H1 */;
718
+ }
719
+ if (lowerSlug.match(/-up-or-down-on-\w+-\d+$/)) {
720
+ return "1d" /* D1 */;
721
+ }
722
+ return null;
723
+ }
724
+ function getIntervalSeconds(interval) {
725
+ const intervalSecondsMap = {
726
+ ["15m" /* M15 */]: 15 * 60,
727
+ // 900秒
728
+ ["1h" /* H1 */]: 60 * 60,
729
+ // 3600秒
730
+ ["4h" /* H4 */]: 4 * 60 * 60,
731
+ // 14400秒
732
+ ["1d" /* D1 */]: 24 * 60 * 60
733
+ // 86400秒
734
+ };
735
+ return intervalSecondsMap[interval];
736
+ }
737
+ function parseTimeRangeFromSlug(slug, referenceYear) {
738
+ const interval = parseIntervalFromSlug(slug);
739
+ if (!interval) {
740
+ return { startTime: null, endTime: null };
741
+ }
742
+ const intervalSeconds = getIntervalSeconds(interval);
743
+ let startTime = null;
744
+ const lowerSlug = slug.toLowerCase();
745
+ if (interval === "15m" /* M15 */ || interval === "4h" /* H4 */) {
746
+ const match = lowerSlug.match(/-updown-(?:15m|4h)-(\d+)$/);
747
+ if (match) {
748
+ startTime = parseInt(match[1], 10);
749
+ }
750
+ } else if (interval === "1h" /* H1 */) {
751
+ const match = lowerSlug.match(/-up-or-down-(\w+)-(\d+)-(\d+)(am|pm)-et$/);
752
+ if (match) {
753
+ const [, monthName, day, hour, ampm] = match;
754
+ const monthIndex = MONTH_NAMES.indexOf(monthName);
755
+ if (monthIndex !== -1) {
756
+ const year = referenceYear ?? (/* @__PURE__ */ new Date()).getFullYear();
757
+ let hour24 = parseInt(hour, 10);
758
+ if (ampm === "pm" && hour24 !== 12) {
759
+ hour24 += 12;
760
+ } else if (ampm === "am" && hour24 === 12) {
761
+ hour24 = 0;
762
+ }
763
+ const etTime = dayjs_default.tz(
764
+ `${year}-${String(monthIndex + 1).padStart(2, "0")}-${String(day).padStart(2, "0")} ${String(hour24).padStart(2, "0")}:00:00`,
765
+ "America/New_York"
766
+ );
767
+ startTime = etTime.unix();
768
+ }
769
+ }
770
+ } else if (interval === "1d" /* D1 */) {
771
+ const match = lowerSlug.match(/-up-or-down-on-(\w+)-(\d+)$/);
772
+ if (match) {
773
+ const [, monthName, day] = match;
774
+ const monthIndex = MONTH_NAMES.indexOf(monthName);
775
+ if (monthIndex !== -1) {
776
+ const year = referenceYear ?? (/* @__PURE__ */ new Date()).getFullYear();
777
+ const utcTime = dayjs_default.utc(
778
+ `${year}-${String(monthIndex + 1).padStart(2, "0")}-${String(day).padStart(2, "0")} 00:00:00`
779
+ );
780
+ startTime = utcTime.unix();
781
+ }
782
+ }
783
+ }
784
+ if (startTime === null) {
785
+ return { startTime: null, endTime: null };
786
+ }
787
+ return {
788
+ startTime,
789
+ endTime: startTime + intervalSeconds
790
+ };
791
+ }
792
+ function parseSlug(slug, referenceYear) {
793
+ const { startTime, endTime } = parseTimeRangeFromSlug(slug, referenceYear);
794
+ return {
795
+ symbol: parseSymbolFromSlug(slug),
796
+ interval: parseIntervalFromSlug(slug),
797
+ startTime,
798
+ endTime
799
+ };
800
+ }
683
801
  // Annotate the CommonJS export names for ESM import in node:
684
802
  0 && (module.exports = {
685
803
  SUPPORTED_SYMBOLS,
@@ -689,5 +807,10 @@ function calculatePeriodStartInET(intervalSeconds) {
689
807
  generate1dSlug,
690
808
  generate1hSlug,
691
809
  generate4hSlug,
692
- generateMarketUrl
810
+ generateMarketUrl,
811
+ getIntervalSeconds,
812
+ parseIntervalFromSlug,
813
+ parseSlug,
814
+ parseSymbolFromSlug,
815
+ parseTimeRangeFromSlug
693
816
  });
@@ -1,4 +1,4 @@
1
- import { SupportedSymbol, SymbolFullNameMap } from './types.cjs';
1
+ import { SupportedSymbol, SymbolFullNameMap, SupportedInterval } from './types.cjs';
2
2
 
3
3
  declare const SUPPORTED_SYMBOLS: SupportedSymbol[];
4
4
  declare const SYMBOL_FULL_NAME_MAP: SymbolFullNameMap;
@@ -15,4 +15,22 @@ declare function generate1dSlug(timestamp: number, symbol: SupportedSymbol): str
15
15
 
16
16
  declare function calculatePeriodStartInET(intervalSeconds: number): number;
17
17
 
18
- export { SUPPORTED_SYMBOLS, SYMBOL_FULL_NAME_MAP, calculatePeriodStartInET, generate15mSlug, generate1dSlug, generate1hSlug, generate4hSlug, generateMarketUrl };
18
+ declare function parseSymbolFromSlug(slug: string): SupportedSymbol | null;
19
+
20
+ declare function parseIntervalFromSlug(slug: string): SupportedInterval | null;
21
+
22
+ declare function getIntervalSeconds(interval: SupportedInterval): number;
23
+
24
+ declare function parseTimeRangeFromSlug(slug: string, referenceYear?: number): {
25
+ startTime: number | null;
26
+ endTime: number | null;
27
+ };
28
+
29
+ declare function parseSlug(slug: string, referenceYear?: number): {
30
+ symbol: SupportedSymbol | null;
31
+ interval: SupportedInterval | null;
32
+ startTime: number | null;
33
+ endTime: number | null;
34
+ };
35
+
36
+ export { SUPPORTED_SYMBOLS, SYMBOL_FULL_NAME_MAP, calculatePeriodStartInET, generate15mSlug, generate1dSlug, generate1hSlug, generate4hSlug, generateMarketUrl, getIntervalSeconds, parseIntervalFromSlug, parseSlug, parseSymbolFromSlug, parseTimeRangeFromSlug };
@@ -1,4 +1,4 @@
1
- import { SupportedSymbol, SymbolFullNameMap } from './types.js';
1
+ import { SupportedSymbol, SymbolFullNameMap, SupportedInterval } from './types.js';
2
2
 
3
3
  declare const SUPPORTED_SYMBOLS: SupportedSymbol[];
4
4
  declare const SYMBOL_FULL_NAME_MAP: SymbolFullNameMap;
@@ -15,4 +15,22 @@ declare function generate1dSlug(timestamp: number, symbol: SupportedSymbol): str
15
15
 
16
16
  declare function calculatePeriodStartInET(intervalSeconds: number): number;
17
17
 
18
- export { SUPPORTED_SYMBOLS, SYMBOL_FULL_NAME_MAP, calculatePeriodStartInET, generate15mSlug, generate1dSlug, generate1hSlug, generate4hSlug, generateMarketUrl };
18
+ declare function parseSymbolFromSlug(slug: string): SupportedSymbol | null;
19
+
20
+ declare function parseIntervalFromSlug(slug: string): SupportedInterval | null;
21
+
22
+ declare function getIntervalSeconds(interval: SupportedInterval): number;
23
+
24
+ declare function parseTimeRangeFromSlug(slug: string, referenceYear?: number): {
25
+ startTime: number | null;
26
+ endTime: number | null;
27
+ };
28
+
29
+ declare function parseSlug(slug: string, referenceYear?: number): {
30
+ symbol: SupportedSymbol | null;
31
+ interval: SupportedInterval | null;
32
+ startTime: number | null;
33
+ endTime: number | null;
34
+ };
35
+
36
+ export { SUPPORTED_SYMBOLS, SYMBOL_FULL_NAME_MAP, calculatePeriodStartInET, generate15mSlug, generate1dSlug, generate1hSlug, generate4hSlug, generateMarketUrl, getIntervalSeconds, parseIntervalFromSlug, parseSlug, parseSymbolFromSlug, parseTimeRangeFromSlug };
package/markets/utils.js CHANGED
@@ -633,6 +633,119 @@ function calculatePeriodStartInET(intervalSeconds) {
633
633
  const periodStartInET = Math.floor(nowInET / intervalSeconds) * intervalSeconds;
634
634
  return periodStartInET + offset;
635
635
  }
636
+ function parseSymbolFromSlug(slug) {
637
+ const lowerSlug = slug.toLowerCase();
638
+ for (const sym of SUPPORTED_SYMBOLS) {
639
+ const shortForm = sym.toLowerCase();
640
+ if (lowerSlug.startsWith(`${shortForm}-updown-`)) {
641
+ return sym;
642
+ }
643
+ }
644
+ for (const sym of SUPPORTED_SYMBOLS) {
645
+ const fullName = SYMBOL_FULL_NAME_MAP[sym];
646
+ if (fullName && lowerSlug.startsWith(`${fullName}-up-or-down-`)) {
647
+ return sym;
648
+ }
649
+ const shortForm = sym.toLowerCase();
650
+ if (lowerSlug.startsWith(`${shortForm}-up-or-down-`)) {
651
+ return sym;
652
+ }
653
+ }
654
+ return null;
655
+ }
656
+ function parseIntervalFromSlug(slug) {
657
+ const lowerSlug = slug.toLowerCase();
658
+ if (lowerSlug.includes("-updown-15m-")) {
659
+ return "15m" /* M15 */;
660
+ }
661
+ if (lowerSlug.includes("-updown-4h-")) {
662
+ return "4h" /* H4 */;
663
+ }
664
+ if (lowerSlug.match(/-up-or-down-\w+-\d+-\d+(am|pm)-et$/)) {
665
+ return "1h" /* H1 */;
666
+ }
667
+ if (lowerSlug.match(/-up-or-down-on-\w+-\d+$/)) {
668
+ return "1d" /* D1 */;
669
+ }
670
+ return null;
671
+ }
672
+ function getIntervalSeconds(interval) {
673
+ const intervalSecondsMap = {
674
+ ["15m" /* M15 */]: 15 * 60,
675
+ // 900秒
676
+ ["1h" /* H1 */]: 60 * 60,
677
+ // 3600秒
678
+ ["4h" /* H4 */]: 4 * 60 * 60,
679
+ // 14400秒
680
+ ["1d" /* D1 */]: 24 * 60 * 60
681
+ // 86400秒
682
+ };
683
+ return intervalSecondsMap[interval];
684
+ }
685
+ function parseTimeRangeFromSlug(slug, referenceYear) {
686
+ const interval = parseIntervalFromSlug(slug);
687
+ if (!interval) {
688
+ return { startTime: null, endTime: null };
689
+ }
690
+ const intervalSeconds = getIntervalSeconds(interval);
691
+ let startTime = null;
692
+ const lowerSlug = slug.toLowerCase();
693
+ if (interval === "15m" /* M15 */ || interval === "4h" /* H4 */) {
694
+ const match = lowerSlug.match(/-updown-(?:15m|4h)-(\d+)$/);
695
+ if (match) {
696
+ startTime = parseInt(match[1], 10);
697
+ }
698
+ } else if (interval === "1h" /* H1 */) {
699
+ const match = lowerSlug.match(/-up-or-down-(\w+)-(\d+)-(\d+)(am|pm)-et$/);
700
+ if (match) {
701
+ const [, monthName, day, hour, ampm] = match;
702
+ const monthIndex = MONTH_NAMES.indexOf(monthName);
703
+ if (monthIndex !== -1) {
704
+ const year = referenceYear ?? (/* @__PURE__ */ new Date()).getFullYear();
705
+ let hour24 = parseInt(hour, 10);
706
+ if (ampm === "pm" && hour24 !== 12) {
707
+ hour24 += 12;
708
+ } else if (ampm === "am" && hour24 === 12) {
709
+ hour24 = 0;
710
+ }
711
+ const etTime = dayjs_default.tz(
712
+ `${year}-${String(monthIndex + 1).padStart(2, "0")}-${String(day).padStart(2, "0")} ${String(hour24).padStart(2, "0")}:00:00`,
713
+ "America/New_York"
714
+ );
715
+ startTime = etTime.unix();
716
+ }
717
+ }
718
+ } else if (interval === "1d" /* D1 */) {
719
+ const match = lowerSlug.match(/-up-or-down-on-(\w+)-(\d+)$/);
720
+ if (match) {
721
+ const [, monthName, day] = match;
722
+ const monthIndex = MONTH_NAMES.indexOf(monthName);
723
+ if (monthIndex !== -1) {
724
+ const year = referenceYear ?? (/* @__PURE__ */ new Date()).getFullYear();
725
+ const utcTime = dayjs_default.utc(
726
+ `${year}-${String(monthIndex + 1).padStart(2, "0")}-${String(day).padStart(2, "0")} 00:00:00`
727
+ );
728
+ startTime = utcTime.unix();
729
+ }
730
+ }
731
+ }
732
+ if (startTime === null) {
733
+ return { startTime: null, endTime: null };
734
+ }
735
+ return {
736
+ startTime,
737
+ endTime: startTime + intervalSeconds
738
+ };
739
+ }
740
+ function parseSlug(slug, referenceYear) {
741
+ const { startTime, endTime } = parseTimeRangeFromSlug(slug, referenceYear);
742
+ return {
743
+ symbol: parseSymbolFromSlug(slug),
744
+ interval: parseIntervalFromSlug(slug),
745
+ startTime,
746
+ endTime
747
+ };
748
+ }
636
749
  export {
637
750
  SUPPORTED_SYMBOLS,
638
751
  SYMBOL_FULL_NAME_MAP,
@@ -641,5 +754,10 @@ export {
641
754
  generate1dSlug,
642
755
  generate1hSlug,
643
756
  generate4hSlug,
644
- generateMarketUrl
757
+ generateMarketUrl,
758
+ getIntervalSeconds,
759
+ parseIntervalFromSlug,
760
+ parseSlug,
761
+ parseSymbolFromSlug,
762
+ parseTimeRangeFromSlug
645
763
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polymarbot/shared",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",