tsu 1.1.2 → 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
@@ -60,7 +60,7 @@ declare function min(array: readonly number[]): number | undefined;
60
60
  *
61
61
  * @param filter - The predicate function.
62
62
  * @param array - The array.
63
- * @returns The array split in two.
63
+ * @returns The tuple of values that satisfy the predicate and those that don't.
64
64
  */
65
65
  declare function partition<T>(filter: (x: T, idx: number, a: readonly T[]) => boolean, array: readonly T[]): [T[], T[]];
66
66
  /**
@@ -68,11 +68,11 @@ declare function partition<T>(filter: (x: T, idx: number, a: readonly T[]) => bo
68
68
  *
69
69
  * @param i - The position to split at.
70
70
  * @param array - The array.
71
- * @returns The array split in two.
71
+ * @returns The tuple of values before and after the split.
72
72
  */
73
73
  declare function splitAt<T>(i: number, array: readonly T[]): [T[], T[]];
74
74
  /**
75
- * Returns the sum of the numbers of an array.
75
+ * Returns the sum of an array of numbers.
76
76
  *
77
77
  * @param array - The array of numbers.
78
78
  * @returns The sum.
@@ -123,6 +123,13 @@ declare function debounce(fn: Fn, delay: number): VoidFn;
123
123
  * @returns The memoized function.
124
124
  */
125
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;
126
133
  /**
127
134
  * Prevents a function from running for some time period after it was last called.
128
135
  *
@@ -147,9 +154,9 @@ declare function isArray(val: unknown): val is any[];
147
154
  */
148
155
  declare function isBoolean(val: unknown): val is boolean;
149
156
  /**
150
- * Identifies if the code is being run in the browser.
157
+ * Identifies if the code is being run in a browser.
151
158
  *
152
- * @returns If the code is being run on a browser.
159
+ * @returns If the code is being run in a browser.
153
160
  */
154
161
  declare function isBrowser(): boolean;
155
162
  /**
@@ -159,6 +166,13 @@ declare function isBrowser(): boolean;
159
166
  * @returns If the value is defined.
160
167
  */
161
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>;
162
176
  /**
163
177
  * Identifies if a value is a function.
164
178
  *
@@ -284,9 +298,10 @@ declare function scroll({ to, offset, duration, container }: ScrollConfig): void
284
298
  *
285
299
  * @param str - The string.
286
300
  * @param allWords - If all words should be capitalised.
301
+ * @param delimiter - The delimiter to split the string by.
287
302
  * @returns The capitalised string.
288
303
  */
289
- declare function capitalise(str: string, allWords?: boolean): string;
304
+ declare function capitalise(str: string, allWords?: boolean, delimiter?: string): string;
290
305
  /**
291
306
  * Separates a string into an array of characters.
292
307
  *
@@ -329,6 +344,13 @@ declare function toKebab(str: string): string;
329
344
  * @returns The number.
330
345
  */
331
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;
332
354
  /**
333
355
  * Truncates a string to a provided length.
334
356
  *
@@ -339,4 +361,4 @@ declare function toNumber(str: string): number;
339
361
  */
340
362
  declare function truncate(str: string, length: number, truncateStr?: string): string;
341
363
 
342
- export { Arrayable, Fn, Nullable, Obj, VoidFn, capitalise, chars, debounce, deepMerge, drop, groupsOf, head, init, isArray, isBlank, isBoolean, isBrowser, isDefined, isEmpty, isEven, isFunction, isNumber, isObject, isOdd, isString, isTouchDevice, 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
@@ -38,6 +38,7 @@ __export(src_exports, {
38
38
  isBrowser: () => isBrowser,
39
39
  isDefined: () => isDefined,
40
40
  isEmpty: () => isEmpty,
41
+ isEmptyObject: () => isEmptyObject,
41
42
  isEven: () => isEven,
42
43
  isFunction: () => isFunction,
43
44
  isNumber: () => isNumber,
@@ -52,6 +53,7 @@ __export(src_exports, {
52
53
  min: () => min,
53
54
  modulo: () => modulo,
54
55
  noop: () => noop,
56
+ once: () => once,
55
57
  partition: () => partition,
56
58
  randomChance: () => randomChance,
57
59
  randomNumber: () => randomNumber,
@@ -66,6 +68,7 @@ __export(src_exports, {
66
68
  toCamel: () => toCamel,
67
69
  toKebab: () => toKebab,
68
70
  toNumber: () => toNumber,
71
+ toOrdinal: () => toOrdinal,
69
72
  truncate: () => truncate,
70
73
  uniq: () => uniq
71
74
  });
@@ -79,7 +82,7 @@ function drop(n, array) {
79
82
  }
80
83
  function groupsOf(s, array) {
81
84
  if (s <= 0) {
82
- throw new Error("[tsu] Invalid group size");
85
+ throw new Error("[tsu] Invalid group size. Is it greater than 0?");
83
86
  }
84
87
  if (s >= array.length) {
85
88
  return [[...array]];
@@ -173,6 +176,15 @@ function memoize(fn) {
173
176
  return result;
174
177
  };
175
178
  }
179
+ function once(fn) {
180
+ let run = false;
181
+ return (...args) => {
182
+ if (!run) {
183
+ fn(...args);
184
+ run = true;
185
+ }
186
+ };
187
+ }
176
188
  function throttle(fn, limit) {
177
189
  let isWaiting = false;
178
190
  return function(...args) {
@@ -199,6 +211,15 @@ function isBrowser() {
199
211
  function isDefined(val) {
200
212
  return typeof val !== "undefined";
201
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
+ }
202
223
  function isFunction(val) {
203
224
  return typeof val === "function";
204
225
  }
@@ -297,11 +318,11 @@ function getElTop(element, start) {
297
318
  }
298
319
 
299
320
  // src/string.ts
300
- function capitalise(str, allWords = false) {
301
- if (!allWords) {
302
- 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);
303
324
  }
304
- return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
325
+ return str.charAt(0).toUpperCase() + str.slice(1);
305
326
  }
306
327
  function chars(str) {
307
328
  return str.split("");
@@ -326,6 +347,15 @@ function toNumber(str) {
326
347
  }
327
348
  return Number(str.replace(/[#£€$,%]/g, ""));
328
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
+ }
329
359
  function truncate(str, length, truncateStr = "...") {
330
360
  if (length <= 0) {
331
361
  return truncateStr;
@@ -349,6 +379,7 @@ module.exports = __toCommonJS(src_exports);
349
379
  isBrowser,
350
380
  isDefined,
351
381
  isEmpty,
382
+ isEmptyObject,
352
383
  isEven,
353
384
  isFunction,
354
385
  isNumber,
@@ -363,6 +394,7 @@ module.exports = __toCommonJS(src_exports);
363
394
  min,
364
395
  modulo,
365
396
  noop,
397
+ once,
366
398
  partition,
367
399
  randomChance,
368
400
  randomNumber,
@@ -377,6 +409,7 @@ module.exports = __toCommonJS(src_exports);
377
409
  toCamel,
378
410
  toKebab,
379
411
  toNumber,
412
+ toOrdinal,
380
413
  truncate,
381
414
  uniq
382
415
  });
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ function drop(n, array) {
7
7
  }
8
8
  function groupsOf(s, array) {
9
9
  if (s <= 0) {
10
- throw new Error("[tsu] Invalid group size");
10
+ throw new Error("[tsu] Invalid group size. Is it greater than 0?");
11
11
  }
12
12
  if (s >= array.length) {
13
13
  return [[...array]];
@@ -101,6 +101,15 @@ function memoize(fn) {
101
101
  return result;
102
102
  };
103
103
  }
104
+ function once(fn) {
105
+ let run = false;
106
+ return (...args) => {
107
+ if (!run) {
108
+ fn(...args);
109
+ run = true;
110
+ }
111
+ };
112
+ }
104
113
  function throttle(fn, limit) {
105
114
  let isWaiting = false;
106
115
  return function(...args) {
@@ -127,6 +136,15 @@ function isBrowser() {
127
136
  function isDefined(val) {
128
137
  return typeof val !== "undefined";
129
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
+ }
130
148
  function isFunction(val) {
131
149
  return typeof val === "function";
132
150
  }
@@ -225,11 +243,11 @@ function getElTop(element, start) {
225
243
  }
226
244
 
227
245
  // src/string.ts
228
- function capitalise(str, allWords = false) {
229
- if (!allWords) {
230
- 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);
231
249
  }
232
- return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
250
+ return str.charAt(0).toUpperCase() + str.slice(1);
233
251
  }
234
252
  function chars(str) {
235
253
  return str.split("");
@@ -254,6 +272,15 @@ function toNumber(str) {
254
272
  }
255
273
  return Number(str.replace(/[#£€$,%]/g, ""));
256
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
+ }
257
284
  function truncate(str, length, truncateStr = "...") {
258
285
  if (length <= 0) {
259
286
  return truncateStr;
@@ -275,6 +302,7 @@ export {
275
302
  isBrowser,
276
303
  isDefined,
277
304
  isEmpty,
305
+ isEmptyObject,
278
306
  isEven,
279
307
  isFunction,
280
308
  isNumber,
@@ -289,6 +317,7 @@ export {
289
317
  min,
290
318
  modulo,
291
319
  noop,
320
+ once,
292
321
  partition,
293
322
  randomChance,
294
323
  randomNumber,
@@ -303,6 +332,7 @@ export {
303
332
  toCamel,
304
333
  toKebab,
305
334
  toNumber,
335
+ toOrdinal,
306
336
  truncate,
307
337
  uniq
308
338
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsu",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "A collection of TypeScript utilities.",
5
5
  "author": "eb3n",
6
6
  "repository": {