tsu 1.1.0 → 1.2.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/dist/index.d.ts CHANGED
@@ -12,6 +12,14 @@ declare type Obj<V = unknown> = Record<string | symbol | number, V>;
12
12
  * @returns The remaining items.
13
13
  */
14
14
  declare function drop<T>(n: number, array: readonly T[]): T[];
15
+ /**
16
+ * Splits an array into groups of size s.
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[][];
15
23
  /**
16
24
  * Returns the first item of an array.
17
25
  *
@@ -52,19 +60,19 @@ declare function min(array: readonly number[]): number | undefined;
52
60
  *
53
61
  * @param filter - The predicate function.
54
62
  * @param array - The array.
55
- * @returns The array split in two.
63
+ * @returns The tuple of values that satisfy the predicate and those that don't.
56
64
  */
57
- declare function partition<T>(filter: (x: T, idx: number, arr: readonly T[]) => boolean, array: readonly T[]): [T[], T[]];
65
+ declare function partition<T>(filter: (x: T, idx: number, a: readonly T[]) => boolean, array: readonly T[]): [T[], T[]];
58
66
  /**
59
67
  * Splits an array into two at a given index.
60
68
  *
61
69
  * @param i - The position to split at.
62
70
  * @param array - The array.
63
- * @returns The array split in two.
71
+ * @returns The tuple of values before and after the split.
64
72
  */
65
73
  declare function splitAt<T>(i: number, array: readonly T[]): [T[], T[]];
66
74
  /**
67
- * Returns the sum of the numbers of an array.
75
+ * Returns the sum of an array of numbers.
68
76
  *
69
77
  * @param array - The array of numbers.
70
78
  * @returns The sum.
@@ -115,6 +123,13 @@ declare function debounce(fn: Fn, delay: number): VoidFn;
115
123
  * @returns The memoized function.
116
124
  */
117
125
  declare function memoize<T>(fn: Fn<T>): Fn<T>;
126
+ /**
127
+ * Enforces that a function is only callable one time.
128
+ *
129
+ * @param fn - The function.
130
+ * @returns The one-time callable function.
131
+ */
132
+ declare function once(fn: Fn): VoidFn;
118
133
  /**
119
134
  * Prevents a function from running for some time period after it was last called.
120
135
  *
@@ -139,9 +154,9 @@ declare function isArray(val: unknown): val is any[];
139
154
  */
140
155
  declare function isBoolean(val: unknown): val is boolean;
141
156
  /**
142
- * Identifies if the code is being run in the browser.
157
+ * Identifies if the code is being run in a browser.
143
158
  *
144
- * @returns If the code is being run on a browser.
159
+ * @returns If the code is being run in a browser.
145
160
  */
146
161
  declare function isBrowser(): boolean;
147
162
  /**
@@ -151,6 +166,13 @@ declare function isBrowser(): boolean;
151
166
  * @returns If the value is defined.
152
167
  */
153
168
  declare function isDefined<T = unknown>(val?: T): val is T;
169
+ /**
170
+ * Identifies if a value is an empty object.
171
+ *
172
+ * @param val - The value.
173
+ * @returns If the value is an empty object.
174
+ */
175
+ declare function isEmptyObject(val: unknown): val is Record<string, never>;
154
176
  /**
155
177
  * Identifies if a value is a function.
156
178
  *
@@ -179,6 +201,12 @@ declare function isObject(val: unknown): val is Obj;
179
201
  * @returns If the value is a string.
180
202
  */
181
203
  declare function isString(val: unknown): val is string;
204
+ /**
205
+ * Identifies if the code is being run on a touch device.
206
+ *
207
+ * @returns If the code is being run on a touch device.
208
+ */
209
+ declare function isTouchDevice(): boolean;
182
210
  /**
183
211
  * Identifies if a value is the global window.
184
212
  *
@@ -270,9 +298,10 @@ declare function scroll({ to, offset, duration, container }: ScrollConfig): void
270
298
  *
271
299
  * @param str - The string.
272
300
  * @param allWords - If all words should be capitalised.
301
+ * @param delimiter - The delimiter to split the string by.
273
302
  * @returns The capitalised string.
274
303
  */
275
- declare function capitalise(str: string, allWords?: boolean): string;
304
+ declare function capitalise(str: string, allWords?: boolean, delimiter?: string): string;
276
305
  /**
277
306
  * Separates a string into an array of characters.
278
307
  *
@@ -315,6 +344,13 @@ declare function toKebab(str: string): string;
315
344
  * @returns The number.
316
345
  */
317
346
  declare function toNumber(str: string): number;
347
+ /**
348
+ * Adds the respective ordinal suffix (-st, -nd, -rd or -th) to a number.
349
+ *
350
+ * @param n - The number.
351
+ * @returns The number with the ordinal suffix appended.
352
+ */
353
+ declare function toOrdinal(n: number | string): string;
318
354
  /**
319
355
  * Truncates a string to a provided length.
320
356
  *
@@ -325,4 +361,4 @@ declare function toNumber(str: string): number;
325
361
  */
326
362
  declare function truncate(str: string, length: number, truncateStr?: string): string;
327
363
 
328
- export { Arrayable, Fn, Nullable, Obj, VoidFn, capitalise, chars, debounce, deepMerge, drop, head, init, isArray, isBlank, isBoolean, isBrowser, isDefined, isEmpty, isEven, isFunction, isNumber, isObject, isOdd, isString, isWindow, last, max, memoize, min, modulo, noop, partition, randomChance, randomNumber, scroll, sleep, splitAt, sum, tail, take, throttle, toArray, toCamel, toKebab, toNumber, truncate, uniq };
364
+ export { Arrayable, Fn, Nullable, Obj, VoidFn, capitalise, chars, debounce, deepMerge, drop, groupsOf, head, init, isArray, isBlank, isBoolean, isBrowser, isDefined, isEmpty, isEmptyObject, isEven, isFunction, isNumber, isObject, isOdd, isString, isTouchDevice, isWindow, last, max, memoize, min, modulo, noop, once, partition, randomChance, randomNumber, scroll, sleep, splitAt, sum, tail, take, throttle, toArray, toCamel, toKebab, toNumber, toOrdinal, truncate, uniq };
package/dist/index.js CHANGED
@@ -29,6 +29,7 @@ __export(src_exports, {
29
29
  debounce: () => debounce,
30
30
  deepMerge: () => deepMerge,
31
31
  drop: () => drop,
32
+ groupsOf: () => groupsOf,
32
33
  head: () => head,
33
34
  init: () => init,
34
35
  isArray: () => isArray,
@@ -37,12 +38,14 @@ __export(src_exports, {
37
38
  isBrowser: () => isBrowser,
38
39
  isDefined: () => isDefined,
39
40
  isEmpty: () => isEmpty,
41
+ isEmptyObject: () => isEmptyObject,
40
42
  isEven: () => isEven,
41
43
  isFunction: () => isFunction,
42
44
  isNumber: () => isNumber,
43
45
  isObject: () => isObject,
44
46
  isOdd: () => isOdd,
45
47
  isString: () => isString,
48
+ isTouchDevice: () => isTouchDevice,
46
49
  isWindow: () => isWindow,
47
50
  last: () => last,
48
51
  max: () => max,
@@ -50,6 +53,7 @@ __export(src_exports, {
50
53
  min: () => min,
51
54
  modulo: () => modulo,
52
55
  noop: () => noop,
56
+ once: () => once,
53
57
  partition: () => partition,
54
58
  randomChance: () => randomChance,
55
59
  randomNumber: () => randomNumber,
@@ -64,6 +68,7 @@ __export(src_exports, {
64
68
  toCamel: () => toCamel,
65
69
  toKebab: () => toKebab,
66
70
  toNumber: () => toNumber,
71
+ toOrdinal: () => toOrdinal,
67
72
  truncate: () => truncate,
68
73
  uniq: () => uniq
69
74
  });
@@ -75,6 +80,19 @@ function drop(n, array) {
75
80
  }
76
81
  return array.slice(n);
77
82
  }
83
+ function groupsOf(s, array) {
84
+ if (s <= 0) {
85
+ throw new Error("[tsu] Invalid group size. Is it greater than 0?");
86
+ }
87
+ if (s >= array.length) {
88
+ return [[...array]];
89
+ }
90
+ const groupedArray = [];
91
+ for (let i = 0; i < array.length; i += s) {
92
+ groupedArray.push(array.slice(i, i + s));
93
+ }
94
+ return groupedArray;
95
+ }
78
96
  function head(array) {
79
97
  return array[0];
80
98
  }
@@ -158,6 +176,15 @@ function memoize(fn) {
158
176
  return result;
159
177
  };
160
178
  }
179
+ function once(fn) {
180
+ let run = false;
181
+ return (...args) => {
182
+ if (!run) {
183
+ fn(...args);
184
+ run = true;
185
+ }
186
+ };
187
+ }
161
188
  function throttle(fn, limit) {
162
189
  let isWaiting = false;
163
190
  return function(...args) {
@@ -184,6 +211,15 @@ function isBrowser() {
184
211
  function isDefined(val) {
185
212
  return typeof val !== "undefined";
186
213
  }
214
+ function isEmptyObject(val) {
215
+ if (!isObject(val)) {
216
+ return false;
217
+ }
218
+ for (const _ in val) {
219
+ return false;
220
+ }
221
+ return true;
222
+ }
187
223
  function isFunction(val) {
188
224
  return typeof val === "function";
189
225
  }
@@ -196,6 +232,9 @@ function isObject(val) {
196
232
  function isString(val) {
197
233
  return typeof val === "string";
198
234
  }
235
+ function isTouchDevice() {
236
+ return "ontouchstart" in window || navigator.maxTouchPoints > 0;
237
+ }
199
238
  function isWindow(val) {
200
239
  return isBrowser() && toString.call(val) === "[object Window]";
201
240
  }
@@ -279,11 +318,11 @@ function getElTop(element, start) {
279
318
  }
280
319
 
281
320
  // src/string.ts
282
- function capitalise(str, allWords = false) {
283
- if (!allWords) {
284
- return str.charAt(0).toUpperCase() + str.slice(1);
321
+ function capitalise(str, allWords = false, delimiter = " ") {
322
+ if (allWords) {
323
+ return str.split(delimiter).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(delimiter);
285
324
  }
286
- return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
325
+ return str.charAt(0).toUpperCase() + str.slice(1);
287
326
  }
288
327
  function chars(str) {
289
328
  return str.split("");
@@ -308,6 +347,15 @@ function toNumber(str) {
308
347
  }
309
348
  return Number(str.replace(/[#£€$,%]/g, ""));
310
349
  }
350
+ function toOrdinal(n) {
351
+ const invalidString = typeof n === "string" && (n.trim() === "" || isNaN(parseFloat(n)));
352
+ if (invalidString) {
353
+ throw new Error("[tsu] invalid string. Does it contain only a number?");
354
+ }
355
+ const suffixes = ["st", "nd", "rd"];
356
+ const suffix = suffixes[(Number(n) / 10 % 10 ^ 1 && Number(n) % 10) - 1] || "th";
357
+ return `${n}${suffix}`;
358
+ }
311
359
  function truncate(str, length, truncateStr = "...") {
312
360
  if (length <= 0) {
313
361
  return truncateStr;
@@ -322,6 +370,7 @@ module.exports = __toCommonJS(src_exports);
322
370
  debounce,
323
371
  deepMerge,
324
372
  drop,
373
+ groupsOf,
325
374
  head,
326
375
  init,
327
376
  isArray,
@@ -330,12 +379,14 @@ module.exports = __toCommonJS(src_exports);
330
379
  isBrowser,
331
380
  isDefined,
332
381
  isEmpty,
382
+ isEmptyObject,
333
383
  isEven,
334
384
  isFunction,
335
385
  isNumber,
336
386
  isObject,
337
387
  isOdd,
338
388
  isString,
389
+ isTouchDevice,
339
390
  isWindow,
340
391
  last,
341
392
  max,
@@ -343,6 +394,7 @@ module.exports = __toCommonJS(src_exports);
343
394
  min,
344
395
  modulo,
345
396
  noop,
397
+ once,
346
398
  partition,
347
399
  randomChance,
348
400
  randomNumber,
@@ -357,6 +409,7 @@ module.exports = __toCommonJS(src_exports);
357
409
  toCamel,
358
410
  toKebab,
359
411
  toNumber,
412
+ toOrdinal,
360
413
  truncate,
361
414
  uniq
362
415
  });
package/dist/index.mjs CHANGED
@@ -5,6 +5,19 @@ function drop(n, array) {
5
5
  }
6
6
  return array.slice(n);
7
7
  }
8
+ function groupsOf(s, array) {
9
+ if (s <= 0) {
10
+ throw new Error("[tsu] Invalid group size. Is it greater than 0?");
11
+ }
12
+ if (s >= array.length) {
13
+ return [[...array]];
14
+ }
15
+ const groupedArray = [];
16
+ for (let i = 0; i < array.length; i += s) {
17
+ groupedArray.push(array.slice(i, i + s));
18
+ }
19
+ return groupedArray;
20
+ }
8
21
  function head(array) {
9
22
  return array[0];
10
23
  }
@@ -88,6 +101,15 @@ function memoize(fn) {
88
101
  return result;
89
102
  };
90
103
  }
104
+ function once(fn) {
105
+ let run = false;
106
+ return (...args) => {
107
+ if (!run) {
108
+ fn(...args);
109
+ run = true;
110
+ }
111
+ };
112
+ }
91
113
  function throttle(fn, limit) {
92
114
  let isWaiting = false;
93
115
  return function(...args) {
@@ -114,6 +136,15 @@ function isBrowser() {
114
136
  function isDefined(val) {
115
137
  return typeof val !== "undefined";
116
138
  }
139
+ function isEmptyObject(val) {
140
+ if (!isObject(val)) {
141
+ return false;
142
+ }
143
+ for (const _ in val) {
144
+ return false;
145
+ }
146
+ return true;
147
+ }
117
148
  function isFunction(val) {
118
149
  return typeof val === "function";
119
150
  }
@@ -126,6 +157,9 @@ function isObject(val) {
126
157
  function isString(val) {
127
158
  return typeof val === "string";
128
159
  }
160
+ function isTouchDevice() {
161
+ return "ontouchstart" in window || navigator.maxTouchPoints > 0;
162
+ }
129
163
  function isWindow(val) {
130
164
  return isBrowser() && toString.call(val) === "[object Window]";
131
165
  }
@@ -209,11 +243,11 @@ function getElTop(element, start) {
209
243
  }
210
244
 
211
245
  // src/string.ts
212
- function capitalise(str, allWords = false) {
213
- if (!allWords) {
214
- return str.charAt(0).toUpperCase() + str.slice(1);
246
+ function capitalise(str, allWords = false, delimiter = " ") {
247
+ if (allWords) {
248
+ return str.split(delimiter).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(delimiter);
215
249
  }
216
- return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
250
+ return str.charAt(0).toUpperCase() + str.slice(1);
217
251
  }
218
252
  function chars(str) {
219
253
  return str.split("");
@@ -238,6 +272,15 @@ function toNumber(str) {
238
272
  }
239
273
  return Number(str.replace(/[#£€$,%]/g, ""));
240
274
  }
275
+ function toOrdinal(n) {
276
+ const invalidString = typeof n === "string" && (n.trim() === "" || isNaN(parseFloat(n)));
277
+ if (invalidString) {
278
+ throw new Error("[tsu] invalid string. Does it contain only a number?");
279
+ }
280
+ const suffixes = ["st", "nd", "rd"];
281
+ const suffix = suffixes[(Number(n) / 10 % 10 ^ 1 && Number(n) % 10) - 1] || "th";
282
+ return `${n}${suffix}`;
283
+ }
241
284
  function truncate(str, length, truncateStr = "...") {
242
285
  if (length <= 0) {
243
286
  return truncateStr;
@@ -250,6 +293,7 @@ export {
250
293
  debounce,
251
294
  deepMerge,
252
295
  drop,
296
+ groupsOf,
253
297
  head,
254
298
  init,
255
299
  isArray,
@@ -258,12 +302,14 @@ export {
258
302
  isBrowser,
259
303
  isDefined,
260
304
  isEmpty,
305
+ isEmptyObject,
261
306
  isEven,
262
307
  isFunction,
263
308
  isNumber,
264
309
  isObject,
265
310
  isOdd,
266
311
  isString,
312
+ isTouchDevice,
267
313
  isWindow,
268
314
  last,
269
315
  max,
@@ -271,6 +317,7 @@ export {
271
317
  min,
272
318
  modulo,
273
319
  noop,
320
+ once,
274
321
  partition,
275
322
  randomChance,
276
323
  randomNumber,
@@ -285,6 +332,7 @@ export {
285
332
  toCamel,
286
333
  toKebab,
287
334
  toNumber,
335
+ toOrdinal,
288
336
  truncate,
289
337
  uniq
290
338
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsu",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "A collection of TypeScript utilities.",
5
5
  "author": "eb3n",
6
6
  "repository": {
@@ -41,7 +41,7 @@
41
41
  "ts-jest": "^27.1.3",
42
42
  "tsup": "^5.12.0",
43
43
  "typescript": "^4.6.2",
44
- "vue": "^3.2.31",
45
- "vuepress": "^1.9.7"
46
- }
44
+ "vuepress": "^2.0.0-beta.41"
45
+ },
46
+ "dependencies": {}
47
47
  }