tsu 1.1.1 → 1.2.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021-present eb3n
3
+ Copyright © 2021-Present eb3n
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -17,6 +17,9 @@ Install with your favourite package manager:
17
17
  # yarn
18
18
  yarn add tsu
19
19
 
20
+ # pnpm
21
+ pnpm install tsu
22
+
20
23
  # npm
21
24
  npm install tsu
22
25
  ```
@@ -29,7 +32,7 @@ import { ... } from 'tsu'
29
32
 
30
33
  ## Documentation
31
34
 
32
- Full documentation is available [here](https://tsu-docs.netlify.app).
35
+ Full documentation is available [here](https://tsu.eb3n.dev).
33
36
 
34
37
  ## Credits
35
38
 
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var l=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var E=(n,e)=>{for(var t in e)l(n,t,{get:e[t],enumerable:!0})},F=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of O(e))!j.call(n,o)&&o!==t&&l(n,o,{get:()=>e[o],enumerable:!(r=v(e,o))||r.enumerable});return n};var N=n=>F(l({},"__esModule",{value:!0}),n);var xn={};E(xn,{capitalise:()=>fn,chars:()=>cn,debounce:()=>W,deepMerge:()=>w,drop:()=>A,groupsOf:()=>S,head:()=>M,init:()=>C,isArray:()=>f,isBlank:()=>ln,isBoolean:()=>z,isBrowser:()=>g,isDefined:()=>J,isEmpty:()=>pn,isEmptyObject:()=>K,isEven:()=>G,isFunction:()=>Y,isNumber:()=>m,isObject:()=>u,isOdd:()=>Q,isString:()=>c,isTouchDevice:()=>Z,isWindow:()=>_,last:()=>U,max:()=>I,memoize:()=>$,min:()=>q,modulo:()=>X,noop:()=>en,once:()=>k,partition:()=>B,randomChance:()=>nn,randomNumber:()=>h,scroll:()=>rn,sleep:()=>tn,splitAt:()=>D,sum:()=>R,tail:()=>L,take:()=>P,throttle:()=>H,toArray:()=>V,toCamel:()=>mn,toKebab:()=>an,toNumber:()=>bn,toOrdinal:()=>Tn,truncate:()=>dn,uniq:()=>p});module.exports=N(xn);function A(n,e){return n<=0?[...e]:e.slice(n)}function S(n,e){if(n<=0)throw new Error("[tsu] Invalid group size. Is it greater than 0?");if(n>=e.length)return[[...e]];let t=[];for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}function M(n){return n[0]}function C(n){return n.slice(0,n.length-1)}function U(n){return n[n.length-1]}function I(n){if(!!n.length)return Math.max(...n)}function q(n){if(!!n.length)return Math.min(...n)}function B(n,e){return e.reduce(([t,r],o,i,s)=>n(o,i,s)?[[...t,o],r]:[t,[...r,o]],[[],[]])}function D(n,e){return n<=0?[[],[...e]]:[e.slice(0,n),e.slice(n)]}function R(n){return n.length?n.reduce((e,t)=>e+t,0):0}function L(n){return n.slice(1)}function P(n,e){return n<=0?[]:e.slice(0,n)}function V(n){return n?Array.isArray(n)?n:[n]:[]}function p(n){return Array.from(new Set(n))}function W(n,e){let t;return function(...r){clearTimeout(t),t=setTimeout(()=>{clearTimeout(t),n(...r)},e)}}function $(n){let e={};return function(...t){let r=JSON.stringify(t);if(r in e)return e[r];let o=n(...t);return e[r]=o,o}}function k(n){let e=!1;return(...t)=>{e||(n(...t),e=!0)}}function H(n,e){let t=!1;return function(...r){t||(n(...r),t=!0,setTimeout(()=>{t=!1},e))}}function f(n){return Array.isArray(n)}function z(n){return typeof n=="boolean"}function g(){return typeof window<"u"}function J(n){return typeof n<"u"}function K(n){if(!u(n))return!1;for(let e in n)return!1;return!0}function Y(n){return typeof n=="function"}function m(n){return typeof n=="number"}function u(n){return Object.prototype.toString.call(n)==="[object Object]"}function c(n){return typeof n=="string"}function Z(){return"ontouchstart"in window||navigator.maxTouchPoints>0}function _(n){return g()&&toString.call(n)==="[object Window]"}function G(n){return n%2===0}function Q(n){return n%2!==0}function X(n,e){return n-Math.floor(n/e)*e}function h(n,e=0){return Math.floor(Math.random()*(n-e))+e}function nn(n){return h(n)===0}function en(){}async function tn(n){return new Promise(e=>setTimeout(e,n))}function w(...n){return n.reduce((e,t)=>(Object.keys(t).forEach(r=>{f(e[r])&&f(t[r])?e[r]=p(e[r].concat(t[r])):u(e[r])&&u(t[r])?e[r]=w(e[r],t[r]):e[r]=t[r]}),e),{})}function rn({to:n,offset:e=0,duration:t=1e3,container:r=null}){let o=c(n)?document.querySelector(n):n,i=c(r)?document.querySelector(r):r,s=(i==null?void 0:i.scrollTop)||(window==null?void 0:window.pageYOffset)||0,a=(m(o)?o:sn(o,s))+e,y=Date.now();function b(){let T=Date.now()-y,d=T<t,x=d?un(s,a,T,t):a;d&&requestAnimationFrame(b),i?i.scrollTop=x:window.scrollTo(0,x)}b()}function on(n){return n<.5?4*Math.pow(n,3):(n-1)*(2*n-2)*(2*n-2)+1}function un(n,e,t,r){return n+(e-n)*on(t/r)}function sn(n,e){return n.nodeName==="HTML"?-e:n.getBoundingClientRect().top+e}function fn(n,e=!1,t=" "){return e?n.split(t).map(r=>r.charAt(0).toUpperCase()+r.slice(1)).join(t):n.charAt(0).toUpperCase()+n.slice(1)}function cn(n){return n.split("")}function ln(n){return n.trim().length===0}function pn(n){return n.length===0}function mn(n){let e=/-(\w)/g;return n.replace(e,(t,r)=>r?r.toUpperCase():"")}function an(n){let e=/\B([A-Z])/g;return n.replace(e,"-$1").toLowerCase()}function bn(n){return n.length?Number(n.replace(/[#£€$,%]/g,"")):NaN}function Tn(n){if(typeof n=="string"&&(n.trim()===""||isNaN(parseFloat(n))))throw new Error("[tsu] invalid string. Does it contain only a number?");let r=["st","nd","rd"][(Number(n)/10%10^1&&Number(n)%10)-1]||"th";return`${n}${r}`}function dn(n,e,t="..."){return e<=0?t:n.slice(0,e)+t}0&&(module.exports={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.d.ts CHANGED
@@ -13,13 +13,13 @@ declare type Obj<V = unknown> = Record<string | symbol | number, V>;
13
13
  */
14
14
  declare function drop<T>(n: number, array: readonly T[]): T[];
15
15
  /**
16
- * Split an array into groups of size `n`.
16
+ * Splits an array into groups of size s.
17
17
  *
18
- * @param n - The size of each group.
18
+ * @param s - The size of each group.
19
19
  * @param array - The array.
20
20
  * @returns The array split into groups.
21
21
  */
22
- declare function groupsOf<T>(n: number, array: readonly T[]): T[][];
22
+ declare function groupsOf<T>(s: number, array: readonly T[]): T[][];
23
23
  /**
24
24
  * Returns the first item of an array.
25
25
  *
@@ -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
@@ -1,382 +1 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __reExport = (target, module2, copyDefault, desc) => {
11
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
- for (let key of __getOwnPropNames(module2))
13
- if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
- }
16
- return target;
17
- };
18
- var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
- return (module2, temp) => {
20
- return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
- };
22
- })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
-
24
- // src/index.ts
25
- var src_exports = {};
26
- __export(src_exports, {
27
- capitalise: () => capitalise,
28
- chars: () => chars,
29
- debounce: () => debounce,
30
- deepMerge: () => deepMerge,
31
- drop: () => drop,
32
- groupsOf: () => groupsOf,
33
- head: () => head,
34
- init: () => init,
35
- isArray: () => isArray,
36
- isBlank: () => isBlank,
37
- isBoolean: () => isBoolean,
38
- isBrowser: () => isBrowser,
39
- isDefined: () => isDefined,
40
- isEmpty: () => isEmpty,
41
- isEven: () => isEven,
42
- isFunction: () => isFunction,
43
- isNumber: () => isNumber,
44
- isObject: () => isObject,
45
- isOdd: () => isOdd,
46
- isString: () => isString,
47
- isTouchDevice: () => isTouchDevice,
48
- isWindow: () => isWindow,
49
- last: () => last,
50
- max: () => max,
51
- memoize: () => memoize,
52
- min: () => min,
53
- modulo: () => modulo,
54
- noop: () => noop,
55
- partition: () => partition,
56
- randomChance: () => randomChance,
57
- randomNumber: () => randomNumber,
58
- scroll: () => scroll,
59
- sleep: () => sleep,
60
- splitAt: () => splitAt,
61
- sum: () => sum,
62
- tail: () => tail,
63
- take: () => take,
64
- throttle: () => throttle,
65
- toArray: () => toArray,
66
- toCamel: () => toCamel,
67
- toKebab: () => toKebab,
68
- toNumber: () => toNumber,
69
- truncate: () => truncate,
70
- uniq: () => uniq
71
- });
72
-
73
- // src/array.ts
74
- function drop(n, array) {
75
- if (n <= 0) {
76
- return [...array];
77
- }
78
- return array.slice(n);
79
- }
80
- function groupsOf(n, array) {
81
- if (n <= 0) {
82
- throw new Error("[tsu] Invalid group size");
83
- }
84
- if (n >= array.length) {
85
- return [[...array]];
86
- }
87
- const groupedArray = [];
88
- for (let i = 0; i < array.length; i += n) {
89
- groupedArray.push(array.slice(i, i + n));
90
- }
91
- return groupedArray;
92
- }
93
- function head(array) {
94
- return array[0];
95
- }
96
- function init(array) {
97
- return array.slice(0, array.length - 1);
98
- }
99
- function last(array) {
100
- return array[array.length - 1];
101
- }
102
- function max(array) {
103
- if (!array.length) {
104
- return;
105
- }
106
- return Math.max(...array);
107
- }
108
- function min(array) {
109
- if (!array.length) {
110
- return;
111
- }
112
- return Math.min(...array);
113
- }
114
- function partition(filter, array) {
115
- return array.reduce(([passed, failed], item, idx, arr) => {
116
- if (filter(item, idx, arr)) {
117
- return [[...passed, item], failed];
118
- }
119
- return [passed, [...failed, item]];
120
- }, [[], []]);
121
- }
122
- function splitAt(i, array) {
123
- if (i <= 0) {
124
- return [[], [...array]];
125
- }
126
- return [array.slice(0, i), array.slice(i)];
127
- }
128
- function sum(array) {
129
- if (!array.length) {
130
- return 0;
131
- }
132
- return array.reduce((sum2, current) => sum2 + current, 0);
133
- }
134
- function tail(array) {
135
- return array.slice(1);
136
- }
137
- function take(n, array) {
138
- if (n <= 0) {
139
- return [];
140
- }
141
- return array.slice(0, n);
142
- }
143
- function toArray(maybeArray) {
144
- if (!maybeArray) {
145
- return [];
146
- }
147
- return Array.isArray(maybeArray) ? maybeArray : [maybeArray];
148
- }
149
- function uniq(array) {
150
- return Array.from(new Set(array));
151
- }
152
-
153
- // src/function.ts
154
- function debounce(fn, delay) {
155
- let timeout;
156
- return function(...args) {
157
- clearTimeout(timeout);
158
- timeout = setTimeout(() => {
159
- clearTimeout(timeout);
160
- fn(...args);
161
- }, delay);
162
- };
163
- }
164
- function memoize(fn) {
165
- const cache = {};
166
- return function(...args) {
167
- const argsString = JSON.stringify(args);
168
- if (argsString in cache) {
169
- return cache[argsString];
170
- }
171
- const result = fn(...args);
172
- cache[argsString] = result;
173
- return result;
174
- };
175
- }
176
- function throttle(fn, limit) {
177
- let isWaiting = false;
178
- return function(...args) {
179
- if (!isWaiting) {
180
- fn(...args);
181
- isWaiting = true;
182
- setTimeout(() => {
183
- isWaiting = false;
184
- }, limit);
185
- }
186
- };
187
- }
188
-
189
- // src/is.ts
190
- function isArray(val) {
191
- return Array.isArray(val);
192
- }
193
- function isBoolean(val) {
194
- return typeof val === "boolean";
195
- }
196
- function isBrowser() {
197
- return typeof window !== "undefined";
198
- }
199
- function isDefined(val) {
200
- return typeof val !== "undefined";
201
- }
202
- function isFunction(val) {
203
- return typeof val === "function";
204
- }
205
- function isNumber(val) {
206
- return typeof val === "number";
207
- }
208
- function isObject(val) {
209
- return Object.prototype.toString.call(val) === "[object Object]";
210
- }
211
- function isString(val) {
212
- return typeof val === "string";
213
- }
214
- function isTouchDevice() {
215
- return "ontouchstart" in window || navigator.maxTouchPoints > 0;
216
- }
217
- function isWindow(val) {
218
- return isBrowser() && toString.call(val) === "[object Window]";
219
- }
220
-
221
- // src/maths.ts
222
- function isEven(n) {
223
- return n % 2 === 0;
224
- }
225
- function isOdd(n) {
226
- return n % 2 !== 0;
227
- }
228
- function modulo(n, m) {
229
- return n - Math.floor(n / m) * m;
230
- }
231
- function randomNumber(max2, min2 = 0) {
232
- return Math.floor(Math.random() * (max2 - min2)) + min2;
233
- }
234
- function randomChance(n) {
235
- return randomNumber(n) === 0;
236
- }
237
-
238
- // src/misc.ts
239
- function noop() {
240
- return;
241
- }
242
- async function sleep(duration) {
243
- return new Promise((resolve) => setTimeout(resolve, duration));
244
- }
245
-
246
- // src/object.ts
247
- function deepMerge(...objects) {
248
- return objects.reduce((result, current) => {
249
- Object.keys(current).forEach((key) => {
250
- if (isArray(result[key]) && isArray(current[key])) {
251
- result[key] = uniq(result[key].concat(current[key]));
252
- } else if (isObject(result[key]) && isObject(current[key])) {
253
- result[key] = deepMerge(result[key], current[key]);
254
- } else {
255
- result[key] = current[key];
256
- }
257
- });
258
- return result;
259
- }, {});
260
- }
261
-
262
- // src/scroll.ts
263
- function scroll({
264
- to,
265
- offset = 0,
266
- duration = 1e3,
267
- container = null
268
- }) {
269
- const target = isString(to) ? document.querySelector(to) : to;
270
- const _container = isString(container) ? document.querySelector(container) : container;
271
- const start = (_container == null ? void 0 : _container.scrollTop) || (window == null ? void 0 : window.pageYOffset) || 0;
272
- const end = (isNumber(target) ? target : getElTop(target, start)) + offset;
273
- const startTime = Date.now();
274
- function step() {
275
- const timeElapsed = Date.now() - startTime;
276
- const isScrolling = timeElapsed < duration;
277
- const position = isScrolling ? getPosition(start, end, timeElapsed, duration) : end;
278
- if (isScrolling) {
279
- requestAnimationFrame(step);
280
- }
281
- if (_container) {
282
- _container.scrollTop = position;
283
- } else {
284
- window.scrollTo(0, position);
285
- }
286
- }
287
- step();
288
- }
289
- function easing(t) {
290
- return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
291
- }
292
- function getPosition(s, e, t, d) {
293
- return s + (e - s) * easing(t / d);
294
- }
295
- function getElTop(element, start) {
296
- return element.nodeName === "HTML" ? -start : element.getBoundingClientRect().top + start;
297
- }
298
-
299
- // src/string.ts
300
- function capitalise(str, allWords = false) {
301
- if (!allWords) {
302
- return str.charAt(0).toUpperCase() + str.slice(1);
303
- }
304
- return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
305
- }
306
- function chars(str) {
307
- return str.split("");
308
- }
309
- function isBlank(str) {
310
- return str.trim().length === 0;
311
- }
312
- function isEmpty(str) {
313
- return str.length === 0;
314
- }
315
- function toCamel(str) {
316
- const RE = /-(\w)/g;
317
- return str.replace(RE, (_, c) => c ? c.toUpperCase() : "");
318
- }
319
- function toKebab(str) {
320
- const RE = /\B([A-Z])/g;
321
- return str.replace(RE, "-$1").toLowerCase();
322
- }
323
- function toNumber(str) {
324
- if (!str.length) {
325
- return NaN;
326
- }
327
- return Number(str.replace(/[#£€$,%]/g, ""));
328
- }
329
- function truncate(str, length, truncateStr = "...") {
330
- if (length <= 0) {
331
- return truncateStr;
332
- }
333
- return str.slice(0, length) + truncateStr;
334
- }
335
- module.exports = __toCommonJS(src_exports);
336
- // Annotate the CommonJS export names for ESM import in node:
337
- 0 && (module.exports = {
338
- capitalise,
339
- chars,
340
- debounce,
341
- deepMerge,
342
- drop,
343
- groupsOf,
344
- head,
345
- init,
346
- isArray,
347
- isBlank,
348
- isBoolean,
349
- isBrowser,
350
- isDefined,
351
- isEmpty,
352
- isEven,
353
- isFunction,
354
- isNumber,
355
- isObject,
356
- isOdd,
357
- isString,
358
- isTouchDevice,
359
- isWindow,
360
- last,
361
- max,
362
- memoize,
363
- min,
364
- modulo,
365
- noop,
366
- partition,
367
- randomChance,
368
- randomNumber,
369
- scroll,
370
- sleep,
371
- splitAt,
372
- sum,
373
- tail,
374
- take,
375
- throttle,
376
- toArray,
377
- toCamel,
378
- toKebab,
379
- toNumber,
380
- truncate,
381
- uniq
382
- });
1
+ function j(n,e){return n<=0?[...e]:e.slice(n)}function E(n,e){if(n<=0)throw new Error("[tsu] Invalid group size. Is it greater than 0?");if(n>=e.length)return[[...e]];let r=[];for(let t=0;t<e.length;t+=n)r.push(e.slice(t,t+n));return r}function F(n){return n[0]}function N(n){return n.slice(0,n.length-1)}function A(n){return n[n.length-1]}function S(n){if(!!n.length)return Math.max(...n)}function M(n){if(!!n.length)return Math.min(...n)}function C(n,e){return e.reduce(([r,t],o,i,u)=>n(o,i,u)?[[...r,o],t]:[r,[...t,o]],[[],[]])}function U(n,e){return n<=0?[[],[...e]]:[e.slice(0,n),e.slice(n)]}function I(n){return n.length?n.reduce((e,r)=>e+r,0):0}function q(n){return n.slice(1)}function B(n,e){return n<=0?[]:e.slice(0,n)}function D(n){return n?Array.isArray(n)?n:[n]:[]}function T(n){return Array.from(new Set(n))}function L(n,e){let r;return function(...t){clearTimeout(r),r=setTimeout(()=>{clearTimeout(r),n(...t)},e)}}function P(n){let e={};return function(...r){let t=JSON.stringify(r);if(t in e)return e[t];let o=n(...r);return e[t]=o,o}}function V(n){let e=!1;return(...r)=>{e||(n(...r),e=!0)}}function W(n,e){let r=!1;return function(...t){r||(n(...t),r=!0,setTimeout(()=>{r=!1},e))}}function f(n){return Array.isArray(n)}function k(n){return typeof n=="boolean"}function g(){return typeof window<"u"}function H(n){return typeof n<"u"}function z(n){if(!s(n))return!1;for(let e in n)return!1;return!0}function J(n){return typeof n=="function"}function d(n){return typeof n=="number"}function s(n){return Object.prototype.toString.call(n)==="[object Object]"}function c(n){return typeof n=="string"}function K(){return"ontouchstart"in window||navigator.maxTouchPoints>0}function Y(n){return g()&&toString.call(n)==="[object Window]"}function _(n){return n%2===0}function G(n){return n%2!==0}function Q(n,e){return n-Math.floor(n/e)*e}function h(n,e=0){return Math.floor(Math.random()*(n-e))+e}function X(n){return h(n)===0}function en(){}async function tn(n){return new Promise(e=>setTimeout(e,n))}function w(...n){return n.reduce((e,r)=>(Object.keys(r).forEach(t=>{f(e[t])&&f(r[t])?e[t]=T(e[t].concat(r[t])):s(e[t])&&s(r[t])?e[t]=w(e[t],r[t]):e[t]=r[t]}),e),{})}function cn({to:n,offset:e=0,duration:r=1e3,container:t=null}){let o=c(n)?document.querySelector(n):n,i=c(t)?document.querySelector(t):t,u=(i==null?void 0:i.scrollTop)||(window==null?void 0:window.pageYOffset)||0,l=(d(o)?o:O(o,u))+e,x=Date.now();function p(){let m=Date.now()-x,a=m<r,b=a?v(u,l,m,r):l;a&&requestAnimationFrame(p),i?i.scrollTop=b:window.scrollTo(0,b)}p()}function y(n){return n<.5?4*Math.pow(n,3):(n-1)*(2*n-2)*(2*n-2)+1}function v(n,e,r,t){return n+(e-n)*y(r/t)}function O(n,e){return n.nodeName==="HTML"?-e:n.getBoundingClientRect().top+e}function pn(n,e=!1,r=" "){return e?n.split(r).map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join(r):n.charAt(0).toUpperCase()+n.slice(1)}function mn(n){return n.split("")}function an(n){return n.trim().length===0}function bn(n){return n.length===0}function Tn(n){let e=/-(\w)/g;return n.replace(e,(r,t)=>t?t.toUpperCase():"")}function dn(n){let e=/\B([A-Z])/g;return n.replace(e,"-$1").toLowerCase()}function xn(n){return n.length?Number(n.replace(/[#£€$,%]/g,"")):NaN}function gn(n){if(typeof n=="string"&&(n.trim()===""||isNaN(parseFloat(n))))throw new Error("[tsu] invalid string. Does it contain only a number?");let t=["st","nd","rd"][(Number(n)/10%10^1&&Number(n)%10)-1]||"th";return`${n}${t}`}function hn(n,e,r="..."){return e<=0?r:n.slice(0,e)+r}export{pn as capitalise,mn as chars,L as debounce,w as deepMerge,j as drop,E as groupsOf,F as head,N as init,f as isArray,an as isBlank,k as isBoolean,g as isBrowser,H as isDefined,bn as isEmpty,z as isEmptyObject,_ as isEven,J as isFunction,d as isNumber,s as isObject,G as isOdd,c as isString,K as isTouchDevice,Y as isWindow,A as last,S as max,P as memoize,M as min,Q as modulo,en as noop,V as once,C as partition,X as randomChance,h as randomNumber,cn as scroll,tn as sleep,U as splitAt,I as sum,q as tail,B as take,W as throttle,D as toArray,Tn as toCamel,dn as toKebab,xn as toNumber,gn as toOrdinal,hn as truncate,T as uniq};
package/package.json CHANGED
@@ -1,47 +1,51 @@
1
1
  {
2
2
  "name": "tsu",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
+ "type": "module",
4
5
  "description": "A collection of TypeScript utilities.",
6
+ "license": "MIT",
5
7
  "author": "eb3n",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
6
21
  "repository": {
7
22
  "type": "git",
8
23
  "url": "git+https://gitlab.com/eb3n/tsu.git"
9
24
  },
10
- "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://gitlab.com/eb3n/tsu/issues"
27
+ },
11
28
  "scripts": {
12
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
13
- "watch": "yarn build -- --watch src",
29
+ "build": "tsup",
30
+ "watch": "nr build --watch src",
14
31
  "test": "jest",
15
- "prepublishOnly": "yarn test && yarn build",
32
+ "prepublishOnly": "nr test; nr build",
16
33
  "docs:dev": "vuepress dev docs",
17
34
  "docs:build": "vuepress build docs"
18
35
  },
19
- "main": "./dist/index.js",
20
- "module": "./dist/index.mjs",
21
- "types": "./dist/index.d.ts",
22
- "files": [
23
- "dist"
24
- ],
25
- "exports": {
26
- ".": {
27
- "require": "./dist/index.js",
28
- "import": "./dist/index.mjs",
29
- "types": "./dist/index.d.ts"
30
- }
31
- },
32
36
  "devDependencies": {
33
- "@types/jest": "^27.4.1",
34
- "@typescript-eslint/eslint-plugin": "^5.14.0",
35
- "@typescript-eslint/parser": "^5.14.0",
36
- "eslint": "^8.10.0",
37
+ "@antfu/ni": "^0.17.2",
38
+ "@types/jest": "^28.1.7",
39
+ "@typescript-eslint/eslint-plugin": "^5.33.0",
40
+ "@typescript-eslint/parser": "^5.33.0",
41
+ "eslint": "^8.22.0",
37
42
  "eslint-config-prettier": "^8.5.0",
38
- "eslint-plugin-prettier": "^4.0.0",
39
- "jest": "^27.5.1",
40
- "prettier": "^2.5.1",
41
- "ts-jest": "^27.1.3",
42
- "tsup": "^5.12.0",
43
- "typescript": "^4.6.2",
44
- "vue": "^3.2.31",
45
- "vuepress": "^1.9.7"
43
+ "eslint-plugin-prettier": "^4.2.1",
44
+ "jest": "^28.1.3",
45
+ "prettier": "^2.7.1",
46
+ "ts-jest": "^28.0.8",
47
+ "tsup": "^6.2.2",
48
+ "typescript": "^4.7.4",
49
+ "vuepress": "2.0.0-beta.41"
46
50
  }
47
51
  }
package/dist/index.mjs DELETED
@@ -1,308 +0,0 @@
1
- // src/array.ts
2
- function drop(n, array) {
3
- if (n <= 0) {
4
- return [...array];
5
- }
6
- return array.slice(n);
7
- }
8
- function groupsOf(n, array) {
9
- if (n <= 0) {
10
- throw new Error("[tsu] Invalid group size");
11
- }
12
- if (n >= array.length) {
13
- return [[...array]];
14
- }
15
- const groupedArray = [];
16
- for (let i = 0; i < array.length; i += n) {
17
- groupedArray.push(array.slice(i, i + n));
18
- }
19
- return groupedArray;
20
- }
21
- function head(array) {
22
- return array[0];
23
- }
24
- function init(array) {
25
- return array.slice(0, array.length - 1);
26
- }
27
- function last(array) {
28
- return array[array.length - 1];
29
- }
30
- function max(array) {
31
- if (!array.length) {
32
- return;
33
- }
34
- return Math.max(...array);
35
- }
36
- function min(array) {
37
- if (!array.length) {
38
- return;
39
- }
40
- return Math.min(...array);
41
- }
42
- function partition(filter, array) {
43
- return array.reduce(([passed, failed], item, idx, arr) => {
44
- if (filter(item, idx, arr)) {
45
- return [[...passed, item], failed];
46
- }
47
- return [passed, [...failed, item]];
48
- }, [[], []]);
49
- }
50
- function splitAt(i, array) {
51
- if (i <= 0) {
52
- return [[], [...array]];
53
- }
54
- return [array.slice(0, i), array.slice(i)];
55
- }
56
- function sum(array) {
57
- if (!array.length) {
58
- return 0;
59
- }
60
- return array.reduce((sum2, current) => sum2 + current, 0);
61
- }
62
- function tail(array) {
63
- return array.slice(1);
64
- }
65
- function take(n, array) {
66
- if (n <= 0) {
67
- return [];
68
- }
69
- return array.slice(0, n);
70
- }
71
- function toArray(maybeArray) {
72
- if (!maybeArray) {
73
- return [];
74
- }
75
- return Array.isArray(maybeArray) ? maybeArray : [maybeArray];
76
- }
77
- function uniq(array) {
78
- return Array.from(new Set(array));
79
- }
80
-
81
- // src/function.ts
82
- function debounce(fn, delay) {
83
- let timeout;
84
- return function(...args) {
85
- clearTimeout(timeout);
86
- timeout = setTimeout(() => {
87
- clearTimeout(timeout);
88
- fn(...args);
89
- }, delay);
90
- };
91
- }
92
- function memoize(fn) {
93
- const cache = {};
94
- return function(...args) {
95
- const argsString = JSON.stringify(args);
96
- if (argsString in cache) {
97
- return cache[argsString];
98
- }
99
- const result = fn(...args);
100
- cache[argsString] = result;
101
- return result;
102
- };
103
- }
104
- function throttle(fn, limit) {
105
- let isWaiting = false;
106
- return function(...args) {
107
- if (!isWaiting) {
108
- fn(...args);
109
- isWaiting = true;
110
- setTimeout(() => {
111
- isWaiting = false;
112
- }, limit);
113
- }
114
- };
115
- }
116
-
117
- // src/is.ts
118
- function isArray(val) {
119
- return Array.isArray(val);
120
- }
121
- function isBoolean(val) {
122
- return typeof val === "boolean";
123
- }
124
- function isBrowser() {
125
- return typeof window !== "undefined";
126
- }
127
- function isDefined(val) {
128
- return typeof val !== "undefined";
129
- }
130
- function isFunction(val) {
131
- return typeof val === "function";
132
- }
133
- function isNumber(val) {
134
- return typeof val === "number";
135
- }
136
- function isObject(val) {
137
- return Object.prototype.toString.call(val) === "[object Object]";
138
- }
139
- function isString(val) {
140
- return typeof val === "string";
141
- }
142
- function isTouchDevice() {
143
- return "ontouchstart" in window || navigator.maxTouchPoints > 0;
144
- }
145
- function isWindow(val) {
146
- return isBrowser() && toString.call(val) === "[object Window]";
147
- }
148
-
149
- // src/maths.ts
150
- function isEven(n) {
151
- return n % 2 === 0;
152
- }
153
- function isOdd(n) {
154
- return n % 2 !== 0;
155
- }
156
- function modulo(n, m) {
157
- return n - Math.floor(n / m) * m;
158
- }
159
- function randomNumber(max2, min2 = 0) {
160
- return Math.floor(Math.random() * (max2 - min2)) + min2;
161
- }
162
- function randomChance(n) {
163
- return randomNumber(n) === 0;
164
- }
165
-
166
- // src/misc.ts
167
- function noop() {
168
- return;
169
- }
170
- async function sleep(duration) {
171
- return new Promise((resolve) => setTimeout(resolve, duration));
172
- }
173
-
174
- // src/object.ts
175
- function deepMerge(...objects) {
176
- return objects.reduce((result, current) => {
177
- Object.keys(current).forEach((key) => {
178
- if (isArray(result[key]) && isArray(current[key])) {
179
- result[key] = uniq(result[key].concat(current[key]));
180
- } else if (isObject(result[key]) && isObject(current[key])) {
181
- result[key] = deepMerge(result[key], current[key]);
182
- } else {
183
- result[key] = current[key];
184
- }
185
- });
186
- return result;
187
- }, {});
188
- }
189
-
190
- // src/scroll.ts
191
- function scroll({
192
- to,
193
- offset = 0,
194
- duration = 1e3,
195
- container = null
196
- }) {
197
- const target = isString(to) ? document.querySelector(to) : to;
198
- const _container = isString(container) ? document.querySelector(container) : container;
199
- const start = (_container == null ? void 0 : _container.scrollTop) || (window == null ? void 0 : window.pageYOffset) || 0;
200
- const end = (isNumber(target) ? target : getElTop(target, start)) + offset;
201
- const startTime = Date.now();
202
- function step() {
203
- const timeElapsed = Date.now() - startTime;
204
- const isScrolling = timeElapsed < duration;
205
- const position = isScrolling ? getPosition(start, end, timeElapsed, duration) : end;
206
- if (isScrolling) {
207
- requestAnimationFrame(step);
208
- }
209
- if (_container) {
210
- _container.scrollTop = position;
211
- } else {
212
- window.scrollTo(0, position);
213
- }
214
- }
215
- step();
216
- }
217
- function easing(t) {
218
- return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
219
- }
220
- function getPosition(s, e, t, d) {
221
- return s + (e - s) * easing(t / d);
222
- }
223
- function getElTop(element, start) {
224
- return element.nodeName === "HTML" ? -start : element.getBoundingClientRect().top + start;
225
- }
226
-
227
- // src/string.ts
228
- function capitalise(str, allWords = false) {
229
- if (!allWords) {
230
- return str.charAt(0).toUpperCase() + str.slice(1);
231
- }
232
- return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
233
- }
234
- function chars(str) {
235
- return str.split("");
236
- }
237
- function isBlank(str) {
238
- return str.trim().length === 0;
239
- }
240
- function isEmpty(str) {
241
- return str.length === 0;
242
- }
243
- function toCamel(str) {
244
- const RE = /-(\w)/g;
245
- return str.replace(RE, (_, c) => c ? c.toUpperCase() : "");
246
- }
247
- function toKebab(str) {
248
- const RE = /\B([A-Z])/g;
249
- return str.replace(RE, "-$1").toLowerCase();
250
- }
251
- function toNumber(str) {
252
- if (!str.length) {
253
- return NaN;
254
- }
255
- return Number(str.replace(/[#£€$,%]/g, ""));
256
- }
257
- function truncate(str, length, truncateStr = "...") {
258
- if (length <= 0) {
259
- return truncateStr;
260
- }
261
- return str.slice(0, length) + truncateStr;
262
- }
263
- export {
264
- capitalise,
265
- chars,
266
- debounce,
267
- deepMerge,
268
- drop,
269
- groupsOf,
270
- head,
271
- init,
272
- isArray,
273
- isBlank,
274
- isBoolean,
275
- isBrowser,
276
- isDefined,
277
- isEmpty,
278
- isEven,
279
- isFunction,
280
- isNumber,
281
- isObject,
282
- isOdd,
283
- isString,
284
- isTouchDevice,
285
- isWindow,
286
- last,
287
- max,
288
- memoize,
289
- min,
290
- modulo,
291
- noop,
292
- partition,
293
- randomChance,
294
- randomNumber,
295
- scroll,
296
- sleep,
297
- splitAt,
298
- sum,
299
- tail,
300
- take,
301
- throttle,
302
- toArray,
303
- toCamel,
304
- toKebab,
305
- toNumber,
306
- truncate,
307
- uniq
308
- };