@h3ravel/support 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,271 @@
1
+ /**
2
+ * Converts CamelCased strings to snake_case
3
+ */
4
+ type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}` ? `${T extends Capitalize<T> ? '_' : ''}${Lowercase<T>}${CamelToSnakeCase<U>}` : S;
5
+
6
+ /**
7
+ * Adds a dot prefix to nested keys
8
+ */
9
+ type DotPrefix<T extends string, U extends string> = T extends '' ? U : `${T}.${U}`;
10
+ /**
11
+ * Converts a union of objects into a single merged object
12
+ */
13
+ type MergeUnion<T> = (T extends any ? (k: T) => void : never) extends (k: infer I) => void ? {
14
+ [K in keyof I]: I[K];
15
+ } : never;
16
+ /**
17
+ * Flattens nested objects into dotted keys
18
+ */
19
+ type DotFlatten<T, Prefix extends string = ''> = MergeUnion<{
20
+ [K in keyof T & string]: T[K] extends Record<string, any> ? DotFlatten<T[K], DotPrefix<Prefix, K>> : {
21
+ [P in DotPrefix<Prefix, K>]: T[K];
22
+ };
23
+ }[keyof T & string]>;
24
+ /**
25
+ * Builds "nested.key" paths for autocompletion
26
+ */
27
+ type DotNestedKeys<T> = {
28
+ [K in keyof T & string]: T[K] extends object ? `${K}` | `${K}.${DotNestedKeys<T[K]>}` : `${K}`;
29
+ }[keyof T & string];
30
+ /**
31
+ * Retrieves type at a given dot-path
32
+ */
33
+ type DotNestedValue<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? DotNestedValue<T[Key], Rest> : never : Path extends keyof T ? T[Path] : never;
34
+ /**
35
+ * Convert CamelCased Object keys to snake_case
36
+ */
37
+ type KeysToSnakeCase<T> = {
38
+ [K in keyof T as CamelToSnakeCase<string & K>]: T[K];
39
+ };
40
+
41
+ /**
42
+ * Splits an array into chunks of a specified size.
43
+ *
44
+ * @template T - Type of elements in the array
45
+ * @param arr - The input array
46
+ * @param size - Size of each chunk (default: 2)
47
+ * @returns An array of chunks (arrays)
48
+ */
49
+ declare const chunk: <T>(arr: T[], size?: number) => T[][];
50
+ /**
51
+ * Generates an array of sequential numbers.
52
+ *
53
+ * @param size - Number of elements in the range
54
+ * @param startAt - Starting number (default: 0)
55
+ * @returns An array of numbers from startAt to startAt + size - 1
56
+ */
57
+ declare const range: (size: number, startAt?: number) => number[];
58
+
59
+ /**
60
+ * Abbreviates large numbers using SI symbols (K, M, B...)
61
+ * and formats the output according to the given locale.
62
+ *
63
+ * @param value - The number to abbreviate
64
+ * @param locale - Optional locale string (default: "en-US")
65
+ * @returns A localized, abbreviated number string
66
+ */
67
+ declare const abbreviate: (value?: number, locale?: string) => string;
68
+ /**
69
+ * Concverts a number into human readable string
70
+ *
71
+ * @param num The number to convert
72
+ * @param slugify convert the ouput into a slug using this as a separator
73
+ * @returns
74
+ */
75
+ declare const humanize: (num: number, slugify?: "-" | "_") => string;
76
+ /**
77
+ * Converts a number of bytes into a human-readable string.
78
+ *
79
+ * @param bytes - The size in bytes to convert
80
+ * @param decimals - Number of decimal places to display (default: 2)
81
+ * @param bits - If true, uses 1000-based (SI) units (B, KB, MB...);
82
+ * otherwise uses 1024-based binary units (Bytes, KiB...)
83
+ * @returns A formatted string with the appropriate unit
84
+ */
85
+ declare const toBytes: (bytes?: number, decimals?: number, bits?: boolean) => string;
86
+ /**
87
+ * Formats a duration (in seconds) into a human-readable string.
88
+ *
89
+ * @param seconds - Duration in seconds
90
+ * @param worded - If true, outputs worded format (e.g., "1hr 2min 3sec"),
91
+ * otherwise HH:MM:SS (e.g., "01:02:03")
92
+ * @returns A formatted time string
93
+ */
94
+ declare const toHumanTime: (seconds?: number, worded?: boolean) => string;
95
+
96
+ /**
97
+ * Flattens a nested object into a single-level object
98
+ * with dot-separated keys.
99
+ *
100
+ * Example:
101
+ * doter({
102
+ * user: { name: "John", address: { city: "NY" } },
103
+ * active: true
104
+ * })
105
+ *
106
+ * Output:
107
+ * {
108
+ * "user.name": "John",
109
+ * "user.address.city": "NY",
110
+ * "active": true
111
+ * }
112
+ *
113
+ * @template T - The type of the input object
114
+ * @param obj - The nested object to flatten
115
+ * @returns A flattened object with dotted keys and inferred types
116
+ */
117
+ declare const dot: <T extends Record<string, any>>(obj: T) => DotFlatten<T>;
118
+ /**
119
+ * Extracts a subset of properties from an object.
120
+ *
121
+ * @template T - Type of the source object
122
+ * @template K - Keys of T to extract
123
+ * @param obj - The source object
124
+ * @param keys - Array of keys to extract
125
+ * @returns A new object with only the specified keys
126
+ */
127
+ declare const extractProperties: <T extends object, K extends keyof T>(obj: T, keys?: readonly K[]) => Pick<T, K>;
128
+ /**
129
+ * Safely retrieves a value from an object by key or nested keys.
130
+ *
131
+ * @template T - Type of the source object
132
+ * @param key - Single key or tuple [parentKey, childKey]
133
+ * @param item - The source object
134
+ * @returns The found value as a string or the key itself if not found
135
+ */
136
+ declare const getValue: <T extends Record<string, any>>(key: string | [keyof T, keyof T[string]], item: T) => string;
137
+ /**
138
+ * Maps over an object's entries and returns a new object
139
+ * with transformed keys and/or values.
140
+ *
141
+ * @template T - Type of the input object
142
+ * @template R - Type of the new values
143
+ * @param obj - The object to transform
144
+ * @param callback - Function that receives [key, value] and returns [newKey, newValue]
145
+ * @returns A new object with transformed entries
146
+ */
147
+ declare const modObj: <T extends object, R>(obj: T, callback: (entry: [keyof T & string, T[keyof T]]) => [string, R]) => Record<string, R>;
148
+ declare function safeDot<T extends Record<string, any>>(data: T): T;
149
+ declare function safeDot<T extends Record<string, any>, K extends DotNestedKeys<T>>(data: T, key?: K): DotNestedValue<T, K>;
150
+ /**
151
+ * Sets a nested property on an object using dot notation.
152
+ *
153
+ * @example
154
+ * const obj = {}
155
+ * setNested(obj, 'app.user.name', 'Legacy')
156
+ * console.log(obj)
157
+ * // Output: { app: { user: { name: 'Legacy' } } }
158
+ *
159
+ * @param obj - The target object to modify.
160
+ * @param key - The dot-separated key (e.g., 'app.user.name').
161
+ * @param value - The value to set at the specified path.
162
+ */
163
+ declare const setNested: (obj: Record<string, any>, key: string, value: any) => void;
164
+ /**
165
+ * Converts object keys to a slugified format (e.g., snake_case).
166
+ *
167
+ * @template T - Type of the input object
168
+ * @param obj - The object whose keys will be slugified
169
+ * @param only - Optional array of keys to slugify (others remain unchanged)
170
+ * @param separator - Separator for slugified keys (default: "_")
171
+ * @returns A new object with slugified keys
172
+ */
173
+ declare const slugifyKeys: <T extends object>(obj: T, only?: string[], separator?: string) => KeysToSnakeCase<T>;
174
+
175
+ /**
176
+ * Get the portion of the string after the first occurrence of the given value.
177
+ *
178
+ * @param value
179
+ * @param search
180
+ * @returns
181
+ */
182
+ declare const after: (value: string, search: string) => string;
183
+ /**
184
+ * Get the portion of the string after the last occurrence of the given value.
185
+ *
186
+ * @param value
187
+ * @param search
188
+ * @returns
189
+ */
190
+ declare const afterLast: (value: string, search: string) => string;
191
+ /**
192
+ * Get the portion of the string before the first occurrence of the given value.
193
+ *
194
+ * @param value
195
+ * @param search
196
+ * @returns
197
+ */
198
+ declare const before: (value: string, search: string) => string;
199
+ /**
200
+ * Get the portion of the string before the last occurrence of the given value.
201
+ *
202
+ * @param value
203
+ * @param search
204
+ * @returns
205
+ */
206
+ declare const beforeLast: (value: string, search: string) => string;
207
+ /**
208
+ * Capitalizes the first character of a string.
209
+ *
210
+ * @param str - The input string
211
+ * @returns The string with the first character capitalized
212
+ */
213
+ declare function capitalize(str: string): string;
214
+ /**
215
+ * Returns the pluralized form of a word based on the given number.
216
+ *
217
+ * @param word - The word to pluralize
218
+ * @param count - The number determining pluralization
219
+ * @returns Singular if count === 1, otherwise plural form
220
+ */
221
+ declare const pluralize: (word: string, count: number) => string;
222
+ /**
223
+ * Converts a plural English word into its singular form.
224
+ *
225
+ * @param word - The word to singularize
226
+ * @returns The singular form of the word
227
+ */
228
+ declare const singularize: (word: string) => string;
229
+ /**
230
+ * Converts a string into a slug format.
231
+ * Handles camelCase, spaces, and non-alphanumeric characters.
232
+ *
233
+ * @param str - The input string to slugify
234
+ * @param joiner - The character used to join words (default: "_")
235
+ * @returns A slugified string
236
+ */
237
+ declare const slugify: (str: string, joiner?: string) => string;
238
+ /**
239
+ * Truncates a string to a specified length and appends an ellipsis if needed.
240
+ *
241
+ * @param str - The input string
242
+ * @param len - Maximum length of the result (including ellipsis)
243
+ * @param ellipsis - String to append if truncated (default: "...")
244
+ * @returns The truncated string
245
+ */
246
+ declare const subString: (str: string, len: number, ellipsis?: string) => string;
247
+ /**
248
+ * Replaces placeholders in a string with corresponding values from a data object.
249
+ *
250
+ * Example:
251
+ * substitute("Hello { user.name }!", { user: { name: "John" } })
252
+ * // "Hello John!"
253
+ *
254
+ * @param str - The string containing placeholders wrapped in { } braces.
255
+ * @param data - Object containing values to substitute. Supports nested keys via dot notation.
256
+ * @param def - Default value to use if a key is missing. (Optional)
257
+ * @returns The substituted string or undefined if the input string or data is invalid.
258
+ */
259
+ declare const substitute: (str: string, data?: Record<string, unknown>, def?: string) => string | undefined;
260
+ /**
261
+ * Truncates a string to a specified length, removing HTML tags and
262
+ * appending a suffix if the string exceeds the length.
263
+ *
264
+ * @param str - The string to truncate
265
+ * @param len - Maximum length (default: 20)
266
+ * @param suffix - Suffix to append if truncated (default: "...")
267
+ * @returns The truncated string
268
+ */
269
+ declare const truncate: (str: string, len?: number, suffix?: string) => string;
270
+
271
+ export { type CamelToSnakeCase, type DotFlatten, type DotNestedKeys, type DotNestedValue, type KeysToSnakeCase, abbreviate, after, afterLast, before, beforeLast, capitalize, chunk, dot, extractProperties, getValue, humanize, modObj, pluralize, range, safeDot, setNested, singularize, slugify, slugifyKeys, subString, substitute, toBytes, toHumanTime, truncate };
@@ -0,0 +1,271 @@
1
+ /**
2
+ * Converts CamelCased strings to snake_case
3
+ */
4
+ type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}` ? `${T extends Capitalize<T> ? '_' : ''}${Lowercase<T>}${CamelToSnakeCase<U>}` : S;
5
+
6
+ /**
7
+ * Adds a dot prefix to nested keys
8
+ */
9
+ type DotPrefix<T extends string, U extends string> = T extends '' ? U : `${T}.${U}`;
10
+ /**
11
+ * Converts a union of objects into a single merged object
12
+ */
13
+ type MergeUnion<T> = (T extends any ? (k: T) => void : never) extends (k: infer I) => void ? {
14
+ [K in keyof I]: I[K];
15
+ } : never;
16
+ /**
17
+ * Flattens nested objects into dotted keys
18
+ */
19
+ type DotFlatten<T, Prefix extends string = ''> = MergeUnion<{
20
+ [K in keyof T & string]: T[K] extends Record<string, any> ? DotFlatten<T[K], DotPrefix<Prefix, K>> : {
21
+ [P in DotPrefix<Prefix, K>]: T[K];
22
+ };
23
+ }[keyof T & string]>;
24
+ /**
25
+ * Builds "nested.key" paths for autocompletion
26
+ */
27
+ type DotNestedKeys<T> = {
28
+ [K in keyof T & string]: T[K] extends object ? `${K}` | `${K}.${DotNestedKeys<T[K]>}` : `${K}`;
29
+ }[keyof T & string];
30
+ /**
31
+ * Retrieves type at a given dot-path
32
+ */
33
+ type DotNestedValue<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? DotNestedValue<T[Key], Rest> : never : Path extends keyof T ? T[Path] : never;
34
+ /**
35
+ * Convert CamelCased Object keys to snake_case
36
+ */
37
+ type KeysToSnakeCase<T> = {
38
+ [K in keyof T as CamelToSnakeCase<string & K>]: T[K];
39
+ };
40
+
41
+ /**
42
+ * Splits an array into chunks of a specified size.
43
+ *
44
+ * @template T - Type of elements in the array
45
+ * @param arr - The input array
46
+ * @param size - Size of each chunk (default: 2)
47
+ * @returns An array of chunks (arrays)
48
+ */
49
+ declare const chunk: <T>(arr: T[], size?: number) => T[][];
50
+ /**
51
+ * Generates an array of sequential numbers.
52
+ *
53
+ * @param size - Number of elements in the range
54
+ * @param startAt - Starting number (default: 0)
55
+ * @returns An array of numbers from startAt to startAt + size - 1
56
+ */
57
+ declare const range: (size: number, startAt?: number) => number[];
58
+
59
+ /**
60
+ * Abbreviates large numbers using SI symbols (K, M, B...)
61
+ * and formats the output according to the given locale.
62
+ *
63
+ * @param value - The number to abbreviate
64
+ * @param locale - Optional locale string (default: "en-US")
65
+ * @returns A localized, abbreviated number string
66
+ */
67
+ declare const abbreviate: (value?: number, locale?: string) => string;
68
+ /**
69
+ * Concverts a number into human readable string
70
+ *
71
+ * @param num The number to convert
72
+ * @param slugify convert the ouput into a slug using this as a separator
73
+ * @returns
74
+ */
75
+ declare const humanize: (num: number, slugify?: "-" | "_") => string;
76
+ /**
77
+ * Converts a number of bytes into a human-readable string.
78
+ *
79
+ * @param bytes - The size in bytes to convert
80
+ * @param decimals - Number of decimal places to display (default: 2)
81
+ * @param bits - If true, uses 1000-based (SI) units (B, KB, MB...);
82
+ * otherwise uses 1024-based binary units (Bytes, KiB...)
83
+ * @returns A formatted string with the appropriate unit
84
+ */
85
+ declare const toBytes: (bytes?: number, decimals?: number, bits?: boolean) => string;
86
+ /**
87
+ * Formats a duration (in seconds) into a human-readable string.
88
+ *
89
+ * @param seconds - Duration in seconds
90
+ * @param worded - If true, outputs worded format (e.g., "1hr 2min 3sec"),
91
+ * otherwise HH:MM:SS (e.g., "01:02:03")
92
+ * @returns A formatted time string
93
+ */
94
+ declare const toHumanTime: (seconds?: number, worded?: boolean) => string;
95
+
96
+ /**
97
+ * Flattens a nested object into a single-level object
98
+ * with dot-separated keys.
99
+ *
100
+ * Example:
101
+ * doter({
102
+ * user: { name: "John", address: { city: "NY" } },
103
+ * active: true
104
+ * })
105
+ *
106
+ * Output:
107
+ * {
108
+ * "user.name": "John",
109
+ * "user.address.city": "NY",
110
+ * "active": true
111
+ * }
112
+ *
113
+ * @template T - The type of the input object
114
+ * @param obj - The nested object to flatten
115
+ * @returns A flattened object with dotted keys and inferred types
116
+ */
117
+ declare const dot: <T extends Record<string, any>>(obj: T) => DotFlatten<T>;
118
+ /**
119
+ * Extracts a subset of properties from an object.
120
+ *
121
+ * @template T - Type of the source object
122
+ * @template K - Keys of T to extract
123
+ * @param obj - The source object
124
+ * @param keys - Array of keys to extract
125
+ * @returns A new object with only the specified keys
126
+ */
127
+ declare const extractProperties: <T extends object, K extends keyof T>(obj: T, keys?: readonly K[]) => Pick<T, K>;
128
+ /**
129
+ * Safely retrieves a value from an object by key or nested keys.
130
+ *
131
+ * @template T - Type of the source object
132
+ * @param key - Single key or tuple [parentKey, childKey]
133
+ * @param item - The source object
134
+ * @returns The found value as a string or the key itself if not found
135
+ */
136
+ declare const getValue: <T extends Record<string, any>>(key: string | [keyof T, keyof T[string]], item: T) => string;
137
+ /**
138
+ * Maps over an object's entries and returns a new object
139
+ * with transformed keys and/or values.
140
+ *
141
+ * @template T - Type of the input object
142
+ * @template R - Type of the new values
143
+ * @param obj - The object to transform
144
+ * @param callback - Function that receives [key, value] and returns [newKey, newValue]
145
+ * @returns A new object with transformed entries
146
+ */
147
+ declare const modObj: <T extends object, R>(obj: T, callback: (entry: [keyof T & string, T[keyof T]]) => [string, R]) => Record<string, R>;
148
+ declare function safeDot<T extends Record<string, any>>(data: T): T;
149
+ declare function safeDot<T extends Record<string, any>, K extends DotNestedKeys<T>>(data: T, key?: K): DotNestedValue<T, K>;
150
+ /**
151
+ * Sets a nested property on an object using dot notation.
152
+ *
153
+ * @example
154
+ * const obj = {}
155
+ * setNested(obj, 'app.user.name', 'Legacy')
156
+ * console.log(obj)
157
+ * // Output: { app: { user: { name: 'Legacy' } } }
158
+ *
159
+ * @param obj - The target object to modify.
160
+ * @param key - The dot-separated key (e.g., 'app.user.name').
161
+ * @param value - The value to set at the specified path.
162
+ */
163
+ declare const setNested: (obj: Record<string, any>, key: string, value: any) => void;
164
+ /**
165
+ * Converts object keys to a slugified format (e.g., snake_case).
166
+ *
167
+ * @template T - Type of the input object
168
+ * @param obj - The object whose keys will be slugified
169
+ * @param only - Optional array of keys to slugify (others remain unchanged)
170
+ * @param separator - Separator for slugified keys (default: "_")
171
+ * @returns A new object with slugified keys
172
+ */
173
+ declare const slugifyKeys: <T extends object>(obj: T, only?: string[], separator?: string) => KeysToSnakeCase<T>;
174
+
175
+ /**
176
+ * Get the portion of the string after the first occurrence of the given value.
177
+ *
178
+ * @param value
179
+ * @param search
180
+ * @returns
181
+ */
182
+ declare const after: (value: string, search: string) => string;
183
+ /**
184
+ * Get the portion of the string after the last occurrence of the given value.
185
+ *
186
+ * @param value
187
+ * @param search
188
+ * @returns
189
+ */
190
+ declare const afterLast: (value: string, search: string) => string;
191
+ /**
192
+ * Get the portion of the string before the first occurrence of the given value.
193
+ *
194
+ * @param value
195
+ * @param search
196
+ * @returns
197
+ */
198
+ declare const before: (value: string, search: string) => string;
199
+ /**
200
+ * Get the portion of the string before the last occurrence of the given value.
201
+ *
202
+ * @param value
203
+ * @param search
204
+ * @returns
205
+ */
206
+ declare const beforeLast: (value: string, search: string) => string;
207
+ /**
208
+ * Capitalizes the first character of a string.
209
+ *
210
+ * @param str - The input string
211
+ * @returns The string with the first character capitalized
212
+ */
213
+ declare function capitalize(str: string): string;
214
+ /**
215
+ * Returns the pluralized form of a word based on the given number.
216
+ *
217
+ * @param word - The word to pluralize
218
+ * @param count - The number determining pluralization
219
+ * @returns Singular if count === 1, otherwise plural form
220
+ */
221
+ declare const pluralize: (word: string, count: number) => string;
222
+ /**
223
+ * Converts a plural English word into its singular form.
224
+ *
225
+ * @param word - The word to singularize
226
+ * @returns The singular form of the word
227
+ */
228
+ declare const singularize: (word: string) => string;
229
+ /**
230
+ * Converts a string into a slug format.
231
+ * Handles camelCase, spaces, and non-alphanumeric characters.
232
+ *
233
+ * @param str - The input string to slugify
234
+ * @param joiner - The character used to join words (default: "_")
235
+ * @returns A slugified string
236
+ */
237
+ declare const slugify: (str: string, joiner?: string) => string;
238
+ /**
239
+ * Truncates a string to a specified length and appends an ellipsis if needed.
240
+ *
241
+ * @param str - The input string
242
+ * @param len - Maximum length of the result (including ellipsis)
243
+ * @param ellipsis - String to append if truncated (default: "...")
244
+ * @returns The truncated string
245
+ */
246
+ declare const subString: (str: string, len: number, ellipsis?: string) => string;
247
+ /**
248
+ * Replaces placeholders in a string with corresponding values from a data object.
249
+ *
250
+ * Example:
251
+ * substitute("Hello { user.name }!", { user: { name: "John" } })
252
+ * // "Hello John!"
253
+ *
254
+ * @param str - The string containing placeholders wrapped in { } braces.
255
+ * @param data - Object containing values to substitute. Supports nested keys via dot notation.
256
+ * @param def - Default value to use if a key is missing. (Optional)
257
+ * @returns The substituted string or undefined if the input string or data is invalid.
258
+ */
259
+ declare const substitute: (str: string, data?: Record<string, unknown>, def?: string) => string | undefined;
260
+ /**
261
+ * Truncates a string to a specified length, removing HTML tags and
262
+ * appending a suffix if the string exceeds the length.
263
+ *
264
+ * @param str - The string to truncate
265
+ * @param len - Maximum length (default: 20)
266
+ * @param suffix - Suffix to append if truncated (default: "...")
267
+ * @returns The truncated string
268
+ */
269
+ declare const truncate: (str: string, len?: number, suffix?: string) => string;
270
+
271
+ export { type CamelToSnakeCase, type DotFlatten, type DotNestedKeys, type DotNestedValue, type KeysToSnakeCase, abbreviate, after, afterLast, before, beforeLast, capitalize, chunk, dot, extractProperties, getValue, humanize, modObj, pluralize, range, safeDot, setNested, singularize, slugify, slugifyKeys, subString, substitute, toBytes, toHumanTime, truncate };