@mohasinac/utils 0.1.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.
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Date Formatting Utilities
3
+ */
4
+ /**
5
+ * Resolve any date-like value (Date, ISO string, number, or Firestore Timestamp JSON)
6
+ * into a native Date object. Returns null for falsy or unparseable input.
7
+ */
8
+ declare function resolveDate(value: unknown): Date | null;
9
+ declare function formatDate(date: Date | string | number | unknown, format?: "short" | "medium" | "long" | "full", locale?: string): string;
10
+ declare function formatDateTime(date: Date | string | number | unknown, format?: "short" | "medium" | "long" | "full", locale?: string): string;
11
+ declare function formatTime(date: Date | string, format?: "short" | "long", locale?: string): string;
12
+ declare function formatRelativeTime(date: Date | string): string;
13
+ declare function formatMonthYear(date: Date | string, locale?: string): string;
14
+ declare function formatDateRange(startDate: Date | string, endDate: Date | string, locale?: string): string;
15
+ declare function isToday(date: Date | string): boolean;
16
+ declare function isPast(date: Date | string): boolean;
17
+ declare function isFuture(date: Date | string): boolean;
18
+ declare function nowMs(): number;
19
+ declare function isSameMonth(a: Date | number, b: Date | number): boolean;
20
+ declare function currentYear(): string;
21
+ declare function nowISO(): string;
22
+
23
+ /**
24
+ * Number Formatting Utilities
25
+ */
26
+ declare function formatCurrency(amount: number, currency?: string, locale?: string): string;
27
+ declare function formatNumber(num: number, locale?: string, options?: {
28
+ decimals?: number;
29
+ }): string;
30
+ declare function formatPercentage(num: number, decimals?: number): string;
31
+ declare function formatFileSize(bytes: number): string;
32
+ declare function formatCompactNumber(num: number): string;
33
+ declare function formatDecimal(num: number, decimals?: number): string;
34
+ declare function formatOrdinal(num: number): string;
35
+ declare function parseFormattedNumber(str: string): number;
36
+
37
+ /**
38
+ * String Formatting and Manipulation Utilities
39
+ */
40
+ declare function capitalize(str: string): string;
41
+ declare function capitalizeWords(str: string): string;
42
+ declare function truncate(str: string, maxLength: number, suffix?: string): string;
43
+ declare function truncateWords(str: string, wordCount: number, suffix?: string): string;
44
+ declare function stripHtml(html: string): string;
45
+ /** Escapes HTML special characters to prevent XSS attacks. */
46
+ declare function escapeHtml(str: string): string;
47
+ declare function slugify(str: string): string;
48
+ declare function maskString(str: string, visibleStart?: number, visibleEnd?: number, maskChar?: string): string;
49
+ declare function randomString(length?: number): string;
50
+ declare function isEmptyString(str: string | null | undefined): boolean;
51
+ /**
52
+ * Converts a ProseMirror/TipTap JSON document string to HTML.
53
+ * Falls back to returning the value unchanged if it is not a ProseMirror doc.
54
+ */
55
+ declare function proseMirrorToHtml(value: string): string;
56
+
57
+ /**
58
+ * Data Type Converters
59
+ */
60
+ declare function stringToBoolean(value: string): boolean;
61
+ declare function booleanToString(value: boolean, format?: "yesno" | "truefalse" | "onoff"): string;
62
+ declare function arrayToObject<T>(arr: T[], keyField: keyof T): Record<string, T>;
63
+ declare function objectToArray<T>(obj: Record<string, T>): T[];
64
+ declare function queryStringToObject(queryString: string): Record<string, string>;
65
+ declare function objectToQueryString(obj: Record<string, unknown>): string;
66
+ declare function firestoreTimestampToDate(timestamp: {
67
+ toDate?: () => Date;
68
+ seconds?: number;
69
+ } | unknown): Date;
70
+ declare function dateToISOString(date: Date | string): string;
71
+
72
+ /**
73
+ * Cookie Utilities — client-side only (browser environment)
74
+ */
75
+ declare function parseCookies(): Record<string, string>;
76
+ declare function getCookie(name: string): string | null;
77
+ declare function hasCookie(name: string): boolean;
78
+ declare function deleteCookie(name: string, path?: string): void;
79
+
80
+ /**
81
+ * SEO-Friendly ID Generators
82
+ */
83
+ interface GenerateCategoryIdInput {
84
+ name: string;
85
+ parentName?: string;
86
+ rootName?: string;
87
+ }
88
+ declare function generateCategoryId(input: GenerateCategoryIdInput): string;
89
+ interface GenerateUserIdInput {
90
+ firstName: string;
91
+ lastName: string;
92
+ email: string;
93
+ }
94
+ declare function generateUserId(input: GenerateUserIdInput): string;
95
+ interface GenerateProductIdInput {
96
+ name: string;
97
+ category: string;
98
+ condition: "new" | "used" | "refurbished";
99
+ sellerName: string;
100
+ count?: number;
101
+ }
102
+ declare function generateProductId(input: GenerateProductIdInput): string;
103
+ interface GenerateAuctionIdInput {
104
+ name: string;
105
+ category: string;
106
+ condition: "new" | "used" | "refurbished";
107
+ sellerName: string;
108
+ count?: number;
109
+ }
110
+ declare function generateAuctionId(input: GenerateAuctionIdInput): string;
111
+ interface GeneratePreOrderIdInput {
112
+ name: string;
113
+ category: string;
114
+ condition: "new" | "used" | "refurbished";
115
+ sellerName: string;
116
+ count?: number;
117
+ }
118
+ declare function generatePreOrderId(input: GeneratePreOrderIdInput): string;
119
+ interface GenerateReviewIdInput {
120
+ productName: string;
121
+ userFirstName: string;
122
+ date?: Date;
123
+ }
124
+ declare function generateReviewId(input: GenerateReviewIdInput): string;
125
+ interface GenerateOrderIdInput {
126
+ productCount: number;
127
+ date?: Date;
128
+ }
129
+ declare function generateOrderId(input: GenerateOrderIdInput): string;
130
+ interface GenerateFAQIdInput {
131
+ category: string;
132
+ question: string;
133
+ }
134
+ declare function generateFAQId(input: GenerateFAQIdInput): string;
135
+ declare function generateCouponId(code: string): string;
136
+ interface GenerateCarouselIdInput {
137
+ title: string;
138
+ }
139
+ declare function generateCarouselId(input: GenerateCarouselIdInput): string;
140
+ interface GenerateHomepageSectionIdInput {
141
+ type: string;
142
+ }
143
+ declare function generateHomepageSectionId(input: GenerateHomepageSectionIdInput): string;
144
+ interface GenerateBidIdInput {
145
+ productName: string;
146
+ userFirstName: string;
147
+ date?: Date;
148
+ random?: string;
149
+ }
150
+ declare function generateBidId(input: GenerateBidIdInput): string;
151
+ interface GenerateBlogPostIdInput {
152
+ title: string;
153
+ category: string;
154
+ status?: "draft" | "published" | "archived";
155
+ }
156
+ declare function generateBlogPostId(input: GenerateBlogPostIdInput): string;
157
+ interface GeneratePayoutIdInput {
158
+ sellerName: string;
159
+ date?: Date;
160
+ }
161
+ declare function generatePayoutId(input: GeneratePayoutIdInput): string;
162
+
163
+ export { type GenerateAuctionIdInput, type GenerateBidIdInput, type GenerateBlogPostIdInput, type GenerateCarouselIdInput, type GenerateCategoryIdInput, type GenerateFAQIdInput, type GenerateHomepageSectionIdInput, type GenerateOrderIdInput, type GeneratePayoutIdInput, type GeneratePreOrderIdInput, type GenerateProductIdInput, type GenerateReviewIdInput, type GenerateUserIdInput, arrayToObject, booleanToString, capitalize, capitalizeWords, currentYear, dateToISOString, deleteCookie, escapeHtml, firestoreTimestampToDate, formatCompactNumber, formatCurrency, formatDate, formatDateRange, formatDateTime, formatDecimal, formatFileSize, formatMonthYear, formatNumber, formatOrdinal, formatPercentage, formatRelativeTime, formatTime, generateAuctionId, generateBidId, generateBlogPostId, generateCarouselId, generateCategoryId, generateCouponId, generateFAQId, generateHomepageSectionId, generateOrderId, generatePayoutId, generatePreOrderId, generateProductId, generateReviewId, generateUserId, getCookie, hasCookie, isEmptyString, isFuture, isPast, isSameMonth, isToday, maskString, nowISO, nowMs, objectToArray, objectToQueryString, parseCookies, parseFormattedNumber, proseMirrorToHtml, queryStringToObject, randomString, resolveDate, slugify, stringToBoolean, stripHtml, truncate, truncateWords };
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Date Formatting Utilities
3
+ */
4
+ /**
5
+ * Resolve any date-like value (Date, ISO string, number, or Firestore Timestamp JSON)
6
+ * into a native Date object. Returns null for falsy or unparseable input.
7
+ */
8
+ declare function resolveDate(value: unknown): Date | null;
9
+ declare function formatDate(date: Date | string | number | unknown, format?: "short" | "medium" | "long" | "full", locale?: string): string;
10
+ declare function formatDateTime(date: Date | string | number | unknown, format?: "short" | "medium" | "long" | "full", locale?: string): string;
11
+ declare function formatTime(date: Date | string, format?: "short" | "long", locale?: string): string;
12
+ declare function formatRelativeTime(date: Date | string): string;
13
+ declare function formatMonthYear(date: Date | string, locale?: string): string;
14
+ declare function formatDateRange(startDate: Date | string, endDate: Date | string, locale?: string): string;
15
+ declare function isToday(date: Date | string): boolean;
16
+ declare function isPast(date: Date | string): boolean;
17
+ declare function isFuture(date: Date | string): boolean;
18
+ declare function nowMs(): number;
19
+ declare function isSameMonth(a: Date | number, b: Date | number): boolean;
20
+ declare function currentYear(): string;
21
+ declare function nowISO(): string;
22
+
23
+ /**
24
+ * Number Formatting Utilities
25
+ */
26
+ declare function formatCurrency(amount: number, currency?: string, locale?: string): string;
27
+ declare function formatNumber(num: number, locale?: string, options?: {
28
+ decimals?: number;
29
+ }): string;
30
+ declare function formatPercentage(num: number, decimals?: number): string;
31
+ declare function formatFileSize(bytes: number): string;
32
+ declare function formatCompactNumber(num: number): string;
33
+ declare function formatDecimal(num: number, decimals?: number): string;
34
+ declare function formatOrdinal(num: number): string;
35
+ declare function parseFormattedNumber(str: string): number;
36
+
37
+ /**
38
+ * String Formatting and Manipulation Utilities
39
+ */
40
+ declare function capitalize(str: string): string;
41
+ declare function capitalizeWords(str: string): string;
42
+ declare function truncate(str: string, maxLength: number, suffix?: string): string;
43
+ declare function truncateWords(str: string, wordCount: number, suffix?: string): string;
44
+ declare function stripHtml(html: string): string;
45
+ /** Escapes HTML special characters to prevent XSS attacks. */
46
+ declare function escapeHtml(str: string): string;
47
+ declare function slugify(str: string): string;
48
+ declare function maskString(str: string, visibleStart?: number, visibleEnd?: number, maskChar?: string): string;
49
+ declare function randomString(length?: number): string;
50
+ declare function isEmptyString(str: string | null | undefined): boolean;
51
+ /**
52
+ * Converts a ProseMirror/TipTap JSON document string to HTML.
53
+ * Falls back to returning the value unchanged if it is not a ProseMirror doc.
54
+ */
55
+ declare function proseMirrorToHtml(value: string): string;
56
+
57
+ /**
58
+ * Data Type Converters
59
+ */
60
+ declare function stringToBoolean(value: string): boolean;
61
+ declare function booleanToString(value: boolean, format?: "yesno" | "truefalse" | "onoff"): string;
62
+ declare function arrayToObject<T>(arr: T[], keyField: keyof T): Record<string, T>;
63
+ declare function objectToArray<T>(obj: Record<string, T>): T[];
64
+ declare function queryStringToObject(queryString: string): Record<string, string>;
65
+ declare function objectToQueryString(obj: Record<string, unknown>): string;
66
+ declare function firestoreTimestampToDate(timestamp: {
67
+ toDate?: () => Date;
68
+ seconds?: number;
69
+ } | unknown): Date;
70
+ declare function dateToISOString(date: Date | string): string;
71
+
72
+ /**
73
+ * Cookie Utilities — client-side only (browser environment)
74
+ */
75
+ declare function parseCookies(): Record<string, string>;
76
+ declare function getCookie(name: string): string | null;
77
+ declare function hasCookie(name: string): boolean;
78
+ declare function deleteCookie(name: string, path?: string): void;
79
+
80
+ /**
81
+ * SEO-Friendly ID Generators
82
+ */
83
+ interface GenerateCategoryIdInput {
84
+ name: string;
85
+ parentName?: string;
86
+ rootName?: string;
87
+ }
88
+ declare function generateCategoryId(input: GenerateCategoryIdInput): string;
89
+ interface GenerateUserIdInput {
90
+ firstName: string;
91
+ lastName: string;
92
+ email: string;
93
+ }
94
+ declare function generateUserId(input: GenerateUserIdInput): string;
95
+ interface GenerateProductIdInput {
96
+ name: string;
97
+ category: string;
98
+ condition: "new" | "used" | "refurbished";
99
+ sellerName: string;
100
+ count?: number;
101
+ }
102
+ declare function generateProductId(input: GenerateProductIdInput): string;
103
+ interface GenerateAuctionIdInput {
104
+ name: string;
105
+ category: string;
106
+ condition: "new" | "used" | "refurbished";
107
+ sellerName: string;
108
+ count?: number;
109
+ }
110
+ declare function generateAuctionId(input: GenerateAuctionIdInput): string;
111
+ interface GeneratePreOrderIdInput {
112
+ name: string;
113
+ category: string;
114
+ condition: "new" | "used" | "refurbished";
115
+ sellerName: string;
116
+ count?: number;
117
+ }
118
+ declare function generatePreOrderId(input: GeneratePreOrderIdInput): string;
119
+ interface GenerateReviewIdInput {
120
+ productName: string;
121
+ userFirstName: string;
122
+ date?: Date;
123
+ }
124
+ declare function generateReviewId(input: GenerateReviewIdInput): string;
125
+ interface GenerateOrderIdInput {
126
+ productCount: number;
127
+ date?: Date;
128
+ }
129
+ declare function generateOrderId(input: GenerateOrderIdInput): string;
130
+ interface GenerateFAQIdInput {
131
+ category: string;
132
+ question: string;
133
+ }
134
+ declare function generateFAQId(input: GenerateFAQIdInput): string;
135
+ declare function generateCouponId(code: string): string;
136
+ interface GenerateCarouselIdInput {
137
+ title: string;
138
+ }
139
+ declare function generateCarouselId(input: GenerateCarouselIdInput): string;
140
+ interface GenerateHomepageSectionIdInput {
141
+ type: string;
142
+ }
143
+ declare function generateHomepageSectionId(input: GenerateHomepageSectionIdInput): string;
144
+ interface GenerateBidIdInput {
145
+ productName: string;
146
+ userFirstName: string;
147
+ date?: Date;
148
+ random?: string;
149
+ }
150
+ declare function generateBidId(input: GenerateBidIdInput): string;
151
+ interface GenerateBlogPostIdInput {
152
+ title: string;
153
+ category: string;
154
+ status?: "draft" | "published" | "archived";
155
+ }
156
+ declare function generateBlogPostId(input: GenerateBlogPostIdInput): string;
157
+ interface GeneratePayoutIdInput {
158
+ sellerName: string;
159
+ date?: Date;
160
+ }
161
+ declare function generatePayoutId(input: GeneratePayoutIdInput): string;
162
+
163
+ export { type GenerateAuctionIdInput, type GenerateBidIdInput, type GenerateBlogPostIdInput, type GenerateCarouselIdInput, type GenerateCategoryIdInput, type GenerateFAQIdInput, type GenerateHomepageSectionIdInput, type GenerateOrderIdInput, type GeneratePayoutIdInput, type GeneratePreOrderIdInput, type GenerateProductIdInput, type GenerateReviewIdInput, type GenerateUserIdInput, arrayToObject, booleanToString, capitalize, capitalizeWords, currentYear, dateToISOString, deleteCookie, escapeHtml, firestoreTimestampToDate, formatCompactNumber, formatCurrency, formatDate, formatDateRange, formatDateTime, formatDecimal, formatFileSize, formatMonthYear, formatNumber, formatOrdinal, formatPercentage, formatRelativeTime, formatTime, generateAuctionId, generateBidId, generateBlogPostId, generateCarouselId, generateCategoryId, generateCouponId, generateFAQId, generateHomepageSectionId, generateOrderId, generatePayoutId, generatePreOrderId, generateProductId, generateReviewId, generateUserId, getCookie, hasCookie, isEmptyString, isFuture, isPast, isSameMonth, isToday, maskString, nowISO, nowMs, objectToArray, objectToQueryString, parseCookies, parseFormattedNumber, proseMirrorToHtml, queryStringToObject, randomString, resolveDate, slugify, stringToBoolean, stripHtml, truncate, truncateWords };