tsu 2.3.0 → 2.4.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.
package/README.md CHANGED
@@ -32,7 +32,7 @@ import { ... } from 'tsu'
32
32
 
33
33
  ## Documentation
34
34
 
35
- Full documentation is available [here](https://tsu.eb3n.dev).
35
+ Full documentation is available [here](https://tsu-docs.netlify.app/).
36
36
 
37
37
  ## Credits
38
38
 
@@ -0,0 +1,417 @@
1
+ type Arrayable<T> = T | Array<T>;
2
+ type Nullable<T> = T | null | undefined;
3
+ type Fn<T = any> = (...args: any[]) => T;
4
+ type VoidFn = Fn<void>;
5
+ type Obj<V = unknown> = Record<string | symbol | number, V>;
6
+
7
+ /**
8
+ * Removes the first `n` items of an array.
9
+ *
10
+ * @param n - The number of items to remove.
11
+ * @param array - The array.
12
+ * @returns The remaining items.
13
+ */
14
+ declare function drop<T>(n: number, array: readonly T[]): T[];
15
+ /**
16
+ * Splits an array into groups of equal size.
17
+ *
18
+ * @param s - The size of each group.
19
+ * @param array - The array.
20
+ * @returns The array split into groups.
21
+ */
22
+ declare function groupsOf<T>(s: number, array: readonly T[]): T[][];
23
+ /**
24
+ * Returns the first item of an array.
25
+ *
26
+ * @param array - The array.
27
+ * @returns The first item.
28
+ */
29
+ declare function head(array: readonly []): undefined;
30
+ declare function head<T>(array: readonly T[]): T;
31
+ /**
32
+ * Returns all but the last item of an array.
33
+ *
34
+ * @param array - The array.
35
+ * @returns The array without the last item.
36
+ */
37
+ declare function init<T>(array: readonly T[]): T[];
38
+ /**
39
+ * Returns the last item of an array.
40
+ *
41
+ * @param array - The array.
42
+ * @returns The last item.
43
+ */
44
+ declare function last(array: readonly []): undefined;
45
+ declare function last<T>(array: readonly T[]): T;
46
+ /**
47
+ * Splits an array into two based on a predicate function.
48
+ *
49
+ * @param filter - The predicate function.
50
+ * @param array - The array.
51
+ * @returns The tuple of values that satisfy the predicate and those that don't.
52
+ */
53
+ declare function partition<T, U = T>(filter: (current: T | U, idx: number, array: readonly (T | U)[]) => boolean, array: readonly (T | U)[]): [T[], U[]];
54
+ /**
55
+ * Generates a range of numbers.
56
+ *
57
+ * @param start - The inclusive start value.
58
+ * @param stop - The exlusive stop value.
59
+ * @param step - The step to increment by.
60
+ * @returns The range of numbers.
61
+ */
62
+ declare function range(stop: number): number[];
63
+ declare function range(start: number, stop: number, step?: number): number[];
64
+ /**
65
+ * Splits an array into two at a specified index.
66
+ *
67
+ * @param i - The position to split at.
68
+ * @param array - The array.
69
+ * @returns The tuple of values before and after the split.
70
+ */
71
+ declare function splitAt<T>(i: number, array: readonly T[]): [T[], T[]];
72
+ /**
73
+ * Returns all but the first item of an array.
74
+ *
75
+ * @param array - The array.
76
+ * @returns The array without the first item.
77
+ */
78
+ declare function tail<T>(array: readonly T[]): T[];
79
+ /**
80
+ * Returns the first `n` items of an array.
81
+ *
82
+ * @param n - The number of items to return.
83
+ * @param array - The array.
84
+ * @returns The first `n` items.
85
+ */
86
+ declare function take<T>(n: number, array: readonly T[]): T[];
87
+ /**
88
+ * Wraps a single value in an array, if not already an array.
89
+ *
90
+ * @param maybeArray - The value to wrap, or the existing array.
91
+ * @returns The item wrapped in an array, or the provided array.
92
+ */
93
+ declare function toArray<T>(maybeArray: Nullable<Arrayable<T>>): T[];
94
+ /**
95
+ * Removes duplicate primitive values from an array.
96
+ *
97
+ * @param array - The array.
98
+ * @returns The array without duplicated primitive values.
99
+ */
100
+ declare function uniq<T>(array: readonly T[]): T[];
101
+
102
+ interface ScrollConfig {
103
+ to: Element | string | number;
104
+ offset?: number;
105
+ duration?: number;
106
+ container?: Element | string | null;
107
+ }
108
+ /**
109
+ * Scrolls the page or provided container to a target element or y-coordinate.
110
+ *
111
+ * @param config - The scroll config.
112
+ * @param config.to - The scroll target.
113
+ * @param config.offset - The offset from the scroll target (in px).
114
+ * @param config.duration - The scroll duration (in ms).
115
+ * @param config.container - The container to scroll in.
116
+ */
117
+ declare function scroll({ to, offset, duration, container }: ScrollConfig): void;
118
+
119
+ /**
120
+ * Prevents function execution until it hasn't been called for a specified time period.
121
+ *
122
+ * @param fn - The function.
123
+ * @param delay - The time period (in ms).
124
+ * @returns The debounced function.
125
+ */
126
+ declare function debounce(fn: VoidFn, delay: number): VoidFn;
127
+ /**
128
+ * Optimises subsequent calls of a function by caching return values.
129
+ *
130
+ * @param fn - The function.
131
+ * @returns The memoized function.
132
+ */
133
+ declare function memoize<T>(fn: Fn<T>): Fn<T>;
134
+ /**
135
+ * Enforces that a function is only callable one time.
136
+ *
137
+ * @param fn - The function.
138
+ * @returns The one-time callable function.
139
+ */
140
+ declare function once(fn: VoidFn): VoidFn;
141
+ /**
142
+ * Prevents function execution for a specified time period after it was last called.
143
+ *
144
+ * @param fn - The function.
145
+ * @param limit - The time period.
146
+ * @returns The throttled function.
147
+ */
148
+ declare function throttle(fn: VoidFn, limit: number): VoidFn;
149
+
150
+ /**
151
+ * Identifies if a value is an array.
152
+ *
153
+ * @param val - The value.
154
+ * @returns If the value is an array.
155
+ */
156
+ declare function isArray(val: unknown): val is any[];
157
+ /**
158
+ * Identifies if a value is a boolean.
159
+ *
160
+ * @param val - The value.
161
+ * @returns If the value is a boolean.
162
+ */
163
+ declare function isBoolean(val: unknown): val is boolean;
164
+ /**
165
+ * Identifies if the code is being run in a browser.
166
+ *
167
+ * @returns If the code is being run in a browser.
168
+ */
169
+ declare function isBrowser(): boolean;
170
+ /**
171
+ * Identifies if a value is defined.
172
+ *
173
+ * @param val - The value.
174
+ * @returns If the value is defined.
175
+ */
176
+ declare function isDefined<T = unknown>(val?: T): val is T;
177
+ /**
178
+ * Identifies if a value is an empty object.
179
+ *
180
+ * @param val - The value.
181
+ * @returns If the value is an empty object.
182
+ */
183
+ declare function isEmptyObject(val: unknown): val is Record<string, never>;
184
+ /**
185
+ * Identifies if a value is a function.
186
+ *
187
+ * @param val - The value.
188
+ * @returns If the value is a function.
189
+ */
190
+ declare function isFunction<T extends Fn>(val: unknown): val is T;
191
+ /**
192
+ * Identifies if a value is null.
193
+ *
194
+ * @param val - The value.
195
+ * @returns If the value is null.
196
+ */
197
+ declare function isNull(val: unknown): val is null;
198
+ /**
199
+ * Identifies if a value is a number.
200
+ *
201
+ * @param val - The value.
202
+ * @returns If the value is a number.
203
+ */
204
+ declare function isNumber(val: unknown): val is number;
205
+ /**
206
+ * Identifies if a value is an object.
207
+ *
208
+ * @param val - The value.
209
+ * @returns If the value is an object.
210
+ */
211
+ declare function isObject(val: unknown): val is Obj;
212
+ /**
213
+ * Identifies if a value is a string.
214
+ *
215
+ * @param val - The value.
216
+ * @returns If the value is a string.
217
+ */
218
+ declare function isString(val: unknown): val is string;
219
+ /**
220
+ * Identifies if the code is being run on a touch device.
221
+ *
222
+ * @returns If the code is being run on a touch device.
223
+ */
224
+ declare function isTouchDevice(): boolean;
225
+ /**
226
+ * Identifies if a value is the global window.
227
+ *
228
+ * @param val - The value.
229
+ * @returns If the value is the global window.
230
+ */
231
+ declare function isWindow(val: unknown): val is Window;
232
+
233
+ /**
234
+ * Rounds a number up to the nearest multiple of the specified factor.
235
+ *
236
+ * @param n - The number to round up.
237
+ * @param factor - The factor used for rounding.
238
+ * @returns The rounded number.
239
+ */
240
+ declare function ceil(n: number, factor?: number): number;
241
+ /**
242
+ * Rounds a number down to the nearest multiple of the specified factor.
243
+ *
244
+ * @param n - The number to round down.
245
+ * @param factor - The factor used for rounding.
246
+ * @returns The rounded number.
247
+ */
248
+ declare function floor(n: number, factor?: number): number;
249
+ /**
250
+ * Identifies if a number is even.
251
+ *
252
+ * @param n - The number.
253
+ * @returns If the number is even.
254
+ */
255
+ declare function isEven(n: number): boolean;
256
+ /**
257
+ * Identifies if a number is odd.
258
+ *
259
+ * @param n - The number.
260
+ * @returns If the number is odd.
261
+ */
262
+ declare function isOdd(n: number): boolean;
263
+ /**
264
+ * Returns the maximum number of an array.
265
+ *
266
+ * @param array - The array of numbers.
267
+ * @returns The maximum number.
268
+ */
269
+ declare function max(array: readonly []): undefined;
270
+ declare function max(array: readonly number[]): number;
271
+ /**
272
+ * Returns the minimum number of an array.
273
+ *
274
+ * @param array - The array of numbers.
275
+ * @returns The minimum number.
276
+ */
277
+ declare function min(array: readonly []): undefined;
278
+ declare function min(array: readonly number[]): number;
279
+ /**
280
+ * Rolls an n-sided die.
281
+ *
282
+ * @param n - The number of sides on the die.
283
+ * @returns If the die roll was 0.
284
+ */
285
+ declare function randomChance(n: number): boolean;
286
+ /**
287
+ * Generates a random integer between bounds.
288
+ *
289
+ * @param min - The inclusive minimum bound.
290
+ * @param max - The exclusive maximum bound.
291
+ * @returns The random integer.
292
+ */
293
+ declare function randomNumber(max: number): number;
294
+ declare function randomNumber(min: number, max: number): number;
295
+ /**
296
+ * Rounds a number to the nearest multiple of the specified factor.
297
+ *
298
+ * @param n - The number to round.
299
+ * @param factor - The factor used for rounding.
300
+ * @returns The rounded number.
301
+ */
302
+ declare function round(n: number, factor?: number): number;
303
+ /**
304
+ * Returns the sum of a number array.
305
+ *
306
+ * @param ns - The number array.
307
+ * @returns The sum.
308
+ */
309
+ declare function sum(ns: readonly number[]): number;
310
+
311
+ /**
312
+ * Does nothing.
313
+ *
314
+ * @returns Nothing.
315
+ */
316
+ declare function noop(): void;
317
+ /**
318
+ * Halts thread execution for a specified time period.
319
+ *
320
+ * @param duration - The time period (in ms).
321
+ * @returns The halting promise.
322
+ */
323
+ declare function sleep(duration: number): Promise<void>;
324
+ /**
325
+ * Generates a random string of a specified length.
326
+ *
327
+ * @param length - The length of the randomly generated string.
328
+ * @returns The randomly generated string.
329
+ */
330
+ declare function uuid(length?: number): string;
331
+
332
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
333
+ /**
334
+ * Recursively merges multiple objects into one.
335
+ *
336
+ * @param objects - The objects to merge.
337
+ * @returns The merged object.
338
+ */
339
+ declare function deepMerge<T extends Obj<any>[]>(...objects: T): UnionToIntersection<T[number]>;
340
+
341
+ /**
342
+ * Capitalises the first letter of one word, or all words, in a string.
343
+ *
344
+ * @param str - The string.
345
+ * @param allWords - If all words should be capitalised.
346
+ * @param delimiter - The delimiter to split the string by.
347
+ * @returns The capitalised string.
348
+ */
349
+ declare function capitalise(str: string, allWords?: boolean, delimiter?: string): string;
350
+ /**
351
+ * Separates a string into an array of characters.
352
+ *
353
+ * @param str - The string.
354
+ * @returns The string's characters.
355
+ */
356
+ declare function chars(str: string): string[];
357
+ /**
358
+ * Identifies strings which only contain whitespace.
359
+ *
360
+ * @param str - The string.
361
+ * @returns If the string is blank.
362
+ */
363
+ declare function isBlank(str: string): boolean;
364
+ /**
365
+ * Identifies empty strings.
366
+ *
367
+ * @param str - The string.
368
+ * @returns If the string is empty.
369
+ */
370
+ declare function isEmpty(str: string): boolean;
371
+ /**
372
+ * Splits a string into two at a specified index.
373
+ *
374
+ * @param i - The position to split at.
375
+ * @param str - The string.
376
+ * @returns The tuple of strings before and after the split.
377
+ */
378
+ declare function splitStrAt(i: number, str: string): [string, string];
379
+ /**
380
+ * Converts a kebab case string to camel case.
381
+ *
382
+ * @param str - The kebab case string.
383
+ * @returns The camel case string.
384
+ */
385
+ declare function toCamel(str: string): string;
386
+ /**
387
+ * Converts a camel case string to kebab case.
388
+ *
389
+ * @param str - The camel case string.
390
+ * @returns The kebab case string.
391
+ */
392
+ declare function toKebab(str: string): string;
393
+ /**
394
+ * Converts a string which contains a number into the number itself.
395
+ *
396
+ * @param str - The string.
397
+ * @returns The number.
398
+ */
399
+ declare function toNumber(str: string): number;
400
+ /**
401
+ * Adds the respective ordinal suffix (-st, -nd, -rd or -th) to a number.
402
+ *
403
+ * @param n - The number.
404
+ * @returns The number with the ordinal suffix appended.
405
+ */
406
+ declare function toOrdinal(n: number): string;
407
+ /**
408
+ * Truncates a string to a provided length.
409
+ *
410
+ * @param str - The string.
411
+ * @param length - The length.
412
+ * @param suffix - The suffix to apply after truncation.
413
+ * @returns The truncated string.
414
+ */
415
+ declare function truncate(str: string, length: number, suffix?: string): string;
416
+
417
+ export { type Arrayable, type Fn, type Nullable, type Obj, type VoidFn, capitalise, ceil, chars, debounce, deepMerge, drop, floor, groupsOf, head, init, isArray, isBlank, isBoolean, isBrowser, isDefined, isEmpty, isEmptyObject, isEven, isFunction, isNull, isNumber, isObject, isOdd, isString, isTouchDevice, isWindow, last, max, memoize, min, noop, once, partition, randomChance, randomNumber, range, round, scroll, sleep, splitAt, splitStrAt, sum, tail, take, throttle, toArray, toCamel, toKebab, toNumber, toOrdinal, truncate, uniq, uuid };
package/dist/index.d.ts CHANGED
@@ -414,4 +414,4 @@ declare function toOrdinal(n: number): string;
414
414
  */
415
415
  declare function truncate(str: string, length: number, suffix?: string): string;
416
416
 
417
- export { Arrayable, Fn, Nullable, Obj, VoidFn, capitalise, ceil, chars, debounce, deepMerge, drop, floor, groupsOf, head, init, isArray, isBlank, isBoolean, isBrowser, isDefined, isEmpty, isEmptyObject, isEven, isFunction, isNull, isNumber, isObject, isOdd, isString, isTouchDevice, isWindow, last, max, memoize, min, noop, once, partition, randomChance, randomNumber, range, round, scroll, sleep, splitAt, splitStrAt, sum, tail, take, throttle, toArray, toCamel, toKebab, toNumber, toOrdinal, truncate, uniq, uuid };
417
+ export { type Arrayable, type Fn, type Nullable, type Obj, type VoidFn, capitalise, ceil, chars, debounce, deepMerge, drop, floor, groupsOf, head, init, isArray, isBlank, isBoolean, isBrowser, isDefined, isEmpty, isEmptyObject, isEven, isFunction, isNull, isNumber, isObject, isOdd, isString, isTouchDevice, isWindow, last, max, memoize, min, noop, once, partition, randomChance, randomNumber, range, round, scroll, sleep, splitAt, splitStrAt, sum, tail, take, throttle, toArray, toCamel, toKebab, toNumber, toOrdinal, truncate, uniq, uuid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsu",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "type": "module",
5
5
  "description": "TypeScript utilities",
6
6
  "license": "MIT",
@@ -35,17 +35,17 @@
35
35
  "prepublishOnly": "nr test; nr build"
36
36
  },
37
37
  "devDependencies": {
38
- "@antfu/ni": "^0.17.2",
39
- "@typescript-eslint/eslint-plugin": "^5.33.0",
40
- "@typescript-eslint/parser": "^5.33.0",
41
- "eslint": "^8.22.0",
42
- "eslint-config-prettier": "^8.5.0",
43
- "eslint-plugin-prettier": "^4.2.1",
44
- "prettier": "^2.7.1",
45
- "tsup": "^6.2.2",
46
- "typescript": "^4.7.4",
47
- "vite": "^4.3.8",
48
- "vitepress": "1.0.0-alpha.35",
49
- "vitest": "^0.26.3"
38
+ "@antfu/ni": "^0.21.12",
39
+ "@typescript-eslint/eslint-plugin": "^6.16.0",
40
+ "@typescript-eslint/parser": "^6.16.0",
41
+ "eslint": "^8.56.0",
42
+ "eslint-config-prettier": "^9.1.0",
43
+ "eslint-plugin-prettier": "^5.1.2",
44
+ "prettier": "^3.1.1",
45
+ "tsup": "^8.0.1",
46
+ "typescript": "^5.3.3",
47
+ "vite": "^5.0.10",
48
+ "vitepress": "^1.0.0-rc.33",
49
+ "vitest": "^1.1.0"
50
50
  }
51
51
  }