@hybrd/utils 1.2.3 → 1.2.5
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/.turbo/turbo-build.log +6 -6
- package/.turbo/turbo-lint$colon$fix.log +6 -0
- package/README.md +293 -0
- package/package.json +3 -3
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @hybrd/utils@1.2.
|
|
3
|
+
> @hybrd/utils@1.2.5 build /Users/ian/Projects/01/hybrid/packages/utils
|
|
4
4
|
> tsup
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: {"index":"src/index.ts"}
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
[34mCLI[39m Cleaning output folder
|
|
12
12
|
[34mCJS[39m Build start
|
|
13
13
|
[34mESM[39m Build start
|
|
14
|
-
[32mESM[39m [1mdist/index.js [22m[32m5.11 KB[39m
|
|
15
|
-
[32mESM[39m [1mdist/index.js.map [22m[32m13.61 KB[39m
|
|
16
|
-
[32mESM[39m ⚡️ Build success in 13ms
|
|
17
14
|
[32mCJS[39m [1mdist/index.cjs [22m[32m7.59 KB[39m
|
|
18
15
|
[32mCJS[39m [1mdist/index.cjs.map [22m[32m14.07 KB[39m
|
|
19
|
-
[32mCJS[39m ⚡️ Build success in
|
|
16
|
+
[32mCJS[39m ⚡️ Build success in 15ms
|
|
17
|
+
[32mESM[39m [1mdist/index.js [22m[32m5.11 KB[39m
|
|
18
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m13.61 KB[39m
|
|
19
|
+
[32mESM[39m ⚡️ Build success in 18ms
|
|
20
20
|
DTS Build start
|
|
21
|
-
DTS ⚡️ Build success in
|
|
21
|
+
DTS ⚡️ Build success in 797ms
|
|
22
22
|
DTS dist/index.d.cts 5.76 KB
|
|
23
23
|
DTS dist/index.d.ts 5.76 KB
|
package/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# @hybrd/utils
|
|
2
|
+
|
|
3
|
+
Utility functions and helpers for the Hybrid framework ecosystem. This package provides a collection of commonly used utility functions for array manipulation, string processing, date formatting, object operations, and more.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install @hybrd/utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { chunk, truncate, formatDate, stringifyValues } from "@hybrd/utils"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API Reference
|
|
18
|
+
|
|
19
|
+
### Array Utilities
|
|
20
|
+
|
|
21
|
+
#### `chunk<T>(arr: T[], size: number): T[][]`
|
|
22
|
+
|
|
23
|
+
Splits an array into chunks of specified size.
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { chunk } from "@hybrd/utils"
|
|
27
|
+
|
|
28
|
+
const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
29
|
+
const chunks = chunk(numbers, 3)
|
|
30
|
+
// Result: [[1, 2, 3], [4, 5, 6], [7, 8]]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### `uniq<T>(array: T[]): T[]`
|
|
34
|
+
|
|
35
|
+
Returns a new array with only unique elements from the input array.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { uniq } from "@hybrd/utils"
|
|
39
|
+
|
|
40
|
+
const duplicates = [1, 2, 2, 3, 3, 3, 4]
|
|
41
|
+
const unique = uniq(duplicates)
|
|
42
|
+
// Result: [1, 2, 3, 4]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### `shuffle(array: any[]): any[]`
|
|
46
|
+
|
|
47
|
+
Randomly shuffles the elements of an array. Returns an empty array if input is undefined.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { shuffle } from "@hybrd/utils"
|
|
51
|
+
|
|
52
|
+
const cards = ['A', 'K', 'Q', 'J']
|
|
53
|
+
const shuffled = shuffle(cards)
|
|
54
|
+
// Result: randomly ordered array
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### String Utilities
|
|
58
|
+
|
|
59
|
+
#### `truncate(str: string, length: number): string`
|
|
60
|
+
|
|
61
|
+
Truncates a string to a specified length, adding an ellipsis if the string is longer than the specified length.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { truncate } from "@hybrd/utils"
|
|
65
|
+
|
|
66
|
+
const longText = "This is a very long text that needs to be truncated"
|
|
67
|
+
const short = truncate(longText, 20)
|
|
68
|
+
// Result: "This is a very long..."
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Date Utilities
|
|
72
|
+
|
|
73
|
+
#### `formatDate(stringOrDate?: string | Date): string`
|
|
74
|
+
|
|
75
|
+
Formats a date string or Date object into a localized date string.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { formatDate } from "@hybrd/utils"
|
|
79
|
+
|
|
80
|
+
const date = new Date('2024-01-15')
|
|
81
|
+
const formatted = formatDate(date)
|
|
82
|
+
// Result: "Jan 15, 2024"
|
|
83
|
+
|
|
84
|
+
const dateString = formatDate('2024-01-15')
|
|
85
|
+
// Result: "Jan 15, 2024"
|
|
86
|
+
|
|
87
|
+
const empty = formatDate()
|
|
88
|
+
// Result: ""
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### `formatRelativeDate(date: Date): string`
|
|
92
|
+
|
|
93
|
+
Formats a date relative to the current time with intelligent formatting:
|
|
94
|
+
- "Today, [time]" for today's dates
|
|
95
|
+
- "Yesterday, [time]" for yesterday's dates
|
|
96
|
+
- "MMM d, h:mm a" for dates in current year
|
|
97
|
+
- "MMM d, yyyy" for dates in other years
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { formatRelativeDate } from "@hybrd/utils"
|
|
101
|
+
|
|
102
|
+
const today = new Date()
|
|
103
|
+
const todayFormatted = formatRelativeDate(today)
|
|
104
|
+
// Result: "Today, 2:30 PM"
|
|
105
|
+
|
|
106
|
+
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000)
|
|
107
|
+
const yesterdayFormatted = formatRelativeDate(yesterday)
|
|
108
|
+
// Result: "Yesterday, 2:30 PM"
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Object Utilities
|
|
112
|
+
|
|
113
|
+
#### `stringifyValues(obj: Record<string, unknown>): Record<string, string>`
|
|
114
|
+
|
|
115
|
+
Stringifies all values in an object, including objects and null values. Returns an empty object if input is undefined.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { stringifyValues } from "@hybrd/utils"
|
|
119
|
+
|
|
120
|
+
const mixed = {
|
|
121
|
+
string: "hello",
|
|
122
|
+
number: 42,
|
|
123
|
+
object: { nested: true },
|
|
124
|
+
nullValue: null,
|
|
125
|
+
boolean: false
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const stringified = stringifyValues(mixed)
|
|
129
|
+
// Result: {
|
|
130
|
+
// string: "hello",
|
|
131
|
+
// number: "42",
|
|
132
|
+
// object: '{"nested":true}',
|
|
133
|
+
// nullValue: "null",
|
|
134
|
+
// boolean: "false"
|
|
135
|
+
// }
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### `pruneEmpty<T>(obj: T): Partial<T>`
|
|
139
|
+
|
|
140
|
+
Removes empty values (undefined, null, empty strings) from an object.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { pruneEmpty } from "@hybrd/utils"
|
|
144
|
+
|
|
145
|
+
const withEmpties = {
|
|
146
|
+
name: "John",
|
|
147
|
+
email: "",
|
|
148
|
+
age: null,
|
|
149
|
+
active: true,
|
|
150
|
+
notes: undefined
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const pruned = pruneEmpty(withEmpties)
|
|
154
|
+
// Result: { name: "John", active: true }
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Markdown Utilities
|
|
158
|
+
|
|
159
|
+
#### `stripMarkdown(markdown: string): Promise<string>`
|
|
160
|
+
|
|
161
|
+
Strips markdown formatting from a string, returning plain text.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { stripMarkdown } from "@hybrd/utils"
|
|
165
|
+
|
|
166
|
+
const markdown = "# Hello **world**\n\nThis is *italic* text."
|
|
167
|
+
const plain = await stripMarkdown(markdown)
|
|
168
|
+
// Result: "Hello world\n\nThis is italic text."
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Cloudflare Utilities
|
|
172
|
+
|
|
173
|
+
#### `getCloudflareEnvironment(): CloudflareEnvironment`
|
|
174
|
+
|
|
175
|
+
Detects if running in Cloudflare environment and returns appropriate configuration.
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import { getCloudflareEnvironment } from "@hybrd/utils"
|
|
179
|
+
|
|
180
|
+
const env = getCloudflareEnvironment()
|
|
181
|
+
// Result: { isCloudflare: boolean, branch?: string, url?: string }
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### `getCloudflareStoragePath(subPath?: string): string`
|
|
185
|
+
|
|
186
|
+
Gets the appropriate storage path for the current environment.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { getCloudflareStoragePath } from "@hybrd/utils"
|
|
190
|
+
|
|
191
|
+
const storagePath = getCloudflareStoragePath("uploads")
|
|
192
|
+
// Result: appropriate path based on environment
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### `getCloudflareServiceUrl(fallbackPort?: number): string`
|
|
196
|
+
|
|
197
|
+
Gets service URL based on Cloudflare environment variables.
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
import { getCloudflareServiceUrl } from "@hybrd/utils"
|
|
201
|
+
|
|
202
|
+
const serviceUrl = getCloudflareServiceUrl(3000)
|
|
203
|
+
// Result: CF_PAGES_URL or localhost with fallback port
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Storage Utilities
|
|
207
|
+
|
|
208
|
+
#### `R2StorageAdapter`
|
|
209
|
+
|
|
210
|
+
Storage adapter for Cloudflare R2.
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
import { R2StorageAdapter } from "@hybrd/utils"
|
|
214
|
+
|
|
215
|
+
const adapter = new R2StorageAdapter(bucket)
|
|
216
|
+
await adapter.uploadFile(localPath, remotePath)
|
|
217
|
+
await adapter.downloadFile(remotePath, localPath)
|
|
218
|
+
await adapter.delete(remotePath)
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### `ExternalDatabaseAdapter`
|
|
222
|
+
|
|
223
|
+
Storage adapter for external databases.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import { ExternalDatabaseAdapter } from "@hybrd/utils"
|
|
227
|
+
|
|
228
|
+
const adapter = new ExternalDatabaseAdapter(connectionString)
|
|
229
|
+
await adapter.uploadFile(localPath, remotePath)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### `createStorageAdapter(): StorageAdapter | null`
|
|
233
|
+
|
|
234
|
+
Factory function to create appropriate storage adapter based on environment.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import { createStorageAdapter } from "@hybrd/utils"
|
|
238
|
+
|
|
239
|
+
const adapter = createStorageAdapter()
|
|
240
|
+
if (adapter) {
|
|
241
|
+
await adapter.uploadFile(localPath, remotePath)
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### URL Utilities
|
|
246
|
+
|
|
247
|
+
#### `getUrl(path?: string): string`
|
|
248
|
+
|
|
249
|
+
Gets the appropriate URL for the current environment, checking:
|
|
250
|
+
1. `AGENT_URL` environment variable
|
|
251
|
+
2. `RAILWAY_PUBLIC_DOMAIN` environment variable (Railway deployment)
|
|
252
|
+
3. `http://localhost:8454/` (default)
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import { getUrl } from "@hybrd/utils"
|
|
256
|
+
|
|
257
|
+
const baseUrl = getUrl()
|
|
258
|
+
// Result: appropriate URL based on environment
|
|
259
|
+
|
|
260
|
+
const apiUrl = getUrl("/api/messages")
|
|
261
|
+
// Result: full URL with path appended
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### UUID Utilities
|
|
265
|
+
|
|
266
|
+
#### `randomUUID(): string`
|
|
267
|
+
|
|
268
|
+
Generates a random UUID string. Uses the uuid package for browser compatibility.
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
import { randomUUID } from "@hybrd/utils"
|
|
272
|
+
|
|
273
|
+
const id = randomUUID()
|
|
274
|
+
// Result: "550e8400-e29b-41d4-a716-446655440000"
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Dependencies
|
|
278
|
+
|
|
279
|
+
This package uses the following external dependencies:
|
|
280
|
+
|
|
281
|
+
- `date-fns` - Date formatting and manipulation
|
|
282
|
+
- `remark` & `strip-markdown` - Markdown processing
|
|
283
|
+
- `uuid` - UUID generation
|
|
284
|
+
|
|
285
|
+
## Related Packages
|
|
286
|
+
|
|
287
|
+
- [`hybrid`](../core) - Main agent framework
|
|
288
|
+
- [`@hybrd/xmtp`](../xmtp) - XMTP client integration
|
|
289
|
+
- [`@hybrd/cli`](../cli) - Command-line interface
|
|
290
|
+
|
|
291
|
+
## License
|
|
292
|
+
|
|
293
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hybrd/utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/node": "22.8.6",
|
|
21
21
|
"tsup": "^8.5.0",
|
|
22
|
-
"@config/
|
|
23
|
-
"@config/
|
|
22
|
+
"@config/tsconfig": "0.0.0",
|
|
23
|
+
"@config/biome": "0.0.0"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "tsup",
|