tsu 1.0.2 → 1.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.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,328 @@
1
- export * from './array';
2
- export * from './function';
3
- export * from './is';
4
- export * from './maths';
5
- export * from './misc';
6
- export * from './object';
7
- export * from './scroll';
8
- export * from './string';
9
- export * from './types';
1
+ declare type Arrayable<T> = T | Array<T>;
2
+ declare type Nullable<T> = T | null | undefined;
3
+ declare type Fn<T = any> = (...args: any[]) => T;
4
+ declare type VoidFn = Fn<void>;
5
+ declare 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
+ * Returns the first item of an array.
17
+ *
18
+ * @param array - The array.
19
+ * @returns The first item.
20
+ */
21
+ declare function head<T>(array: readonly T[]): T;
22
+ /**
23
+ * Returns all but the last item of an array.
24
+ *
25
+ * @param array - The array.
26
+ * @returns The array without the last item.
27
+ */
28
+ declare function init<T>(array: readonly T[]): T[];
29
+ /**
30
+ * Returns the last item of an array.
31
+ *
32
+ * @param array - The array.
33
+ * @returns The last item.
34
+ */
35
+ declare function last<T>(array: readonly T[]): T;
36
+ /**
37
+ * Returns the maximum number of an array.
38
+ *
39
+ * @param array - The array of numbers.
40
+ * @returns The maximum number.
41
+ */
42
+ declare function max(array: readonly number[]): number | undefined;
43
+ /**
44
+ * Returns the minimum number of an array.
45
+ *
46
+ * @param array - The array of numbers.
47
+ * @returns The minimum number.
48
+ */
49
+ declare function min(array: readonly number[]): number | undefined;
50
+ /**
51
+ * Splits an array into two based on a predicate function.
52
+ *
53
+ * @param filter - The predicate function.
54
+ * @param array - The array.
55
+ * @returns The array split in two.
56
+ */
57
+ declare function partition<T>(filter: (x: T, idx: number, arr: readonly T[]) => boolean, array: readonly T[]): [T[], T[]];
58
+ /**
59
+ * Splits an array into two at a given index.
60
+ *
61
+ * @param i - The position to split at.
62
+ * @param array - The array.
63
+ * @returns The array split in two.
64
+ */
65
+ declare function splitAt<T>(i: number, array: readonly T[]): [T[], T[]];
66
+ /**
67
+ * Returns the sum of the numbers of an array.
68
+ *
69
+ * @param array - The array of numbers.
70
+ * @returns The sum.
71
+ */
72
+ declare function sum(array: readonly number[]): number;
73
+ /**
74
+ * Returns all but the first item of an array.
75
+ *
76
+ * @param array - The array.
77
+ * @returns The array without the first item.
78
+ */
79
+ declare function tail<T>(array: readonly T[]): T[];
80
+ /**
81
+ * Returns the first n items of an array.
82
+ *
83
+ * @param n - The number of items to return.
84
+ * @param array - The array.
85
+ * @returns The first n items.
86
+ */
87
+ declare function take<T>(n: number, array: readonly T[]): T[];
88
+ /**
89
+ * Wraps a single value of an array, if not already an array.
90
+ *
91
+ * @param maybeArray - The value to wrap, or the existing array.
92
+ * @returns The item wrapped in an array, or the provided array.
93
+ */
94
+ declare function toArray<T>(maybeArray: Nullable<Arrayable<T>>): T[];
95
+ /**
96
+ * Removes duplicate primitive values from an array.
97
+ *
98
+ * @param array - The array.
99
+ * @returns The array without duplicated primitive values.
100
+ */
101
+ declare function uniq<T>(array: readonly T[]): T[];
102
+
103
+ /**
104
+ * Prevents a function from running until it hasn't been called for a certain time period.
105
+ *
106
+ * @param fn - The function.
107
+ * @param delay - The time period (in ms).
108
+ * @returns The debounced function.
109
+ */
110
+ declare function debounce(fn: Fn, delay: number): VoidFn;
111
+ /**
112
+ * Optimises subsequent calls of a function by caching return values.
113
+ *
114
+ * @param fn - The function.
115
+ * @returns The memoized function.
116
+ */
117
+ declare function memoize<T>(fn: Fn<T>): Fn<T>;
118
+ /**
119
+ * Prevents a function from running for some time period after it was last called.
120
+ *
121
+ * @param fn - The function.
122
+ * @param limit - The time period.
123
+ * @returns The throttled function.
124
+ */
125
+ declare function throttle(fn: Fn, limit: number): VoidFn;
126
+
127
+ /**
128
+ * Identifies if a value is an array.
129
+ *
130
+ * @param val - The value.
131
+ * @returns If the value is an array.
132
+ */
133
+ declare function isArray(val: unknown): val is any[];
134
+ /**
135
+ * Identifies if a value is a boolean.
136
+ *
137
+ * @param val - The value.
138
+ * @returns If the value is a boolean.
139
+ */
140
+ declare function isBoolean(val: unknown): val is boolean;
141
+ /**
142
+ * Identifies if the code is being run in the browser.
143
+ *
144
+ * @returns If the code is being run on a browser.
145
+ */
146
+ declare function isBrowser(): boolean;
147
+ /**
148
+ * Identifies if a value is defined.
149
+ *
150
+ * @param val - The value.
151
+ * @returns If the value is defined.
152
+ */
153
+ declare function isDefined<T = unknown>(val?: T): val is T;
154
+ /**
155
+ * Identifies if a value is a function.
156
+ *
157
+ * @param val - The value.
158
+ * @returns If the value is a function.
159
+ */
160
+ declare function isFunction<T extends Fn>(val: unknown): val is T;
161
+ /**
162
+ * Identifies if a value is a number.
163
+ *
164
+ * @param val - The value.
165
+ * @returns If the value is a number.
166
+ */
167
+ declare function isNumber(val: unknown): val is number;
168
+ /**
169
+ * Identifies if a value is an object.
170
+ *
171
+ * @param val - The value.
172
+ * @returns If the value is an object.
173
+ */
174
+ declare function isObject(val: unknown): val is Obj;
175
+ /**
176
+ * Identifies if a value is a string.
177
+ *
178
+ * @param val - The value.
179
+ * @returns If the value is a string.
180
+ */
181
+ declare function isString(val: unknown): val is string;
182
+ /**
183
+ * Identifies if a value is the global window.
184
+ *
185
+ * @param val - The value.
186
+ * @returns If the value is the global window.
187
+ */
188
+ declare function isWindow(val: unknown): val is Window;
189
+
190
+ /**
191
+ * Identifies if a number is even.
192
+ *
193
+ * @param n - The number.
194
+ * @returns If the number is even.
195
+ */
196
+ declare function isEven(n: number): boolean;
197
+ /**
198
+ * Identifies if a number is odd.
199
+ *
200
+ * @param n - The number.
201
+ * @returns If the number is odd.
202
+ */
203
+ declare function isOdd(n: number): boolean;
204
+ /**
205
+ * Opinionated implementation of modulo (%).
206
+ *
207
+ * @param n - The dividend.
208
+ * @param m - The divisor.
209
+ * @returns The remainder of dividing n by m.
210
+ */
211
+ declare function modulo(n: number, m: number): number;
212
+ /**
213
+ * Generates a random integer between bounds.
214
+ *
215
+ * @param max - The exclusive maximum bound.
216
+ * @param min - The inclusive minimum bound.
217
+ * @returns The random integer.
218
+ */
219
+ declare function randomNumber(max: number, min?: number): number;
220
+ /**
221
+ * Rolls an n-sided die.
222
+ *
223
+ * @param n - The number of sides on the die.
224
+ * @returns If the die roll was 0.
225
+ */
226
+ declare function randomChance(n: number): boolean;
227
+
228
+ /**
229
+ * Does nothing.
230
+ *
231
+ * @returns Nothing.
232
+ */
233
+ declare function noop(): void;
234
+ /**
235
+ * Halts program execution for a specified time.
236
+ *
237
+ * @param duration - The time to wait (in ms).
238
+ * @returns The halting promise.
239
+ */
240
+ declare function sleep(duration: number): Promise<void>;
241
+
242
+ declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
243
+ /**
244
+ * Recursively merges multiple objects into one.
245
+ *
246
+ * @param objects - The objects to merge.
247
+ * @returns The merged object.
248
+ */
249
+ declare function deepMerge<T extends Obj<any>[]>(...objects: T): UnionToIntersection<T[number]>;
250
+
251
+ declare type ScrollConfig = {
252
+ to: Element | string | number;
253
+ offset?: number;
254
+ duration?: number;
255
+ container?: Element | string | null;
256
+ };
257
+ /**
258
+ * Scrolls the page or provided container to a target element or y-coordinate.
259
+ *
260
+ * @param config - The scroll config.
261
+ * @param config.to - The scroll target.
262
+ * @param config.offset - The offset from the scroll target (in px).
263
+ * @param config.duration - The scroll duration (in ms).
264
+ * @param config.container - The container to scroll in.
265
+ */
266
+ declare function scroll({ to, offset, duration, container }: ScrollConfig): void;
267
+
268
+ /**
269
+ * Capitalises the first letter of one word, or all words, in a string.
270
+ *
271
+ * @param str - The string.
272
+ * @param allWords - If all words should be capitalised.
273
+ * @returns The capitalised string.
274
+ */
275
+ declare function capitalise(str: string, allWords?: boolean): string;
276
+ /**
277
+ * Separates a string into an array of characters.
278
+ *
279
+ * @param str - The string.
280
+ * @returns The string's characters.
281
+ */
282
+ declare function chars(str: string): string[];
283
+ /**
284
+ * Identifies strings which only contain whitespace.
285
+ *
286
+ * @param str - The string.
287
+ * @returns If the string is blank.
288
+ */
289
+ declare function isBlank(str: string): boolean;
290
+ /**
291
+ * Identifies empty strings.
292
+ *
293
+ * @param str - The string.
294
+ * @returns If the string is empty.
295
+ */
296
+ declare function isEmpty(str: string): boolean;
297
+ /**
298
+ * Converts a kebab case string to camel case.
299
+ *
300
+ * @param str - The kebab case string.
301
+ * @returns The camel case string.
302
+ */
303
+ declare function toCamel(str: string): string;
304
+ /**
305
+ * Converts a camel case string to kebab case.
306
+ *
307
+ * @param str - The camel case string.
308
+ * @returns The kebab case string.
309
+ */
310
+ declare function toKebab(str: string): string;
311
+ /**
312
+ * Converts a string which contains a number to the number itself.
313
+ *
314
+ * @param str - The string.
315
+ * @returns The number.
316
+ */
317
+ declare function toNumber(str: string): number;
318
+ /**
319
+ * Truncates a string to a provided length.
320
+ *
321
+ * @param str - The string.
322
+ * @param length - The length.
323
+ * @param truncateStr - The prefix to apply after truncation.
324
+ * @returns The truncated string.
325
+ */
326
+ declare function truncate(str: string, length: number, truncateStr?: string): string;
327
+
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 };
package/dist/index.js CHANGED
@@ -1,8 +1,362 @@
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);
1
23
 
2
- 'use strict'
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
+ head: () => head,
33
+ init: () => init,
34
+ isArray: () => isArray,
35
+ isBlank: () => isBlank,
36
+ isBoolean: () => isBoolean,
37
+ isBrowser: () => isBrowser,
38
+ isDefined: () => isDefined,
39
+ isEmpty: () => isEmpty,
40
+ isEven: () => isEven,
41
+ isFunction: () => isFunction,
42
+ isNumber: () => isNumber,
43
+ isObject: () => isObject,
44
+ isOdd: () => isOdd,
45
+ isString: () => isString,
46
+ isWindow: () => isWindow,
47
+ last: () => last,
48
+ max: () => max,
49
+ memoize: () => memoize,
50
+ min: () => min,
51
+ modulo: () => modulo,
52
+ noop: () => noop,
53
+ partition: () => partition,
54
+ randomChance: () => randomChance,
55
+ randomNumber: () => randomNumber,
56
+ scroll: () => scroll,
57
+ sleep: () => sleep,
58
+ splitAt: () => splitAt,
59
+ sum: () => sum,
60
+ tail: () => tail,
61
+ take: () => take,
62
+ throttle: () => throttle,
63
+ toArray: () => toArray,
64
+ toCamel: () => toCamel,
65
+ toKebab: () => toKebab,
66
+ toNumber: () => toNumber,
67
+ truncate: () => truncate,
68
+ uniq: () => uniq
69
+ });
3
70
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./tsu.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./tsu.cjs.development.js')
71
+ // src/array.ts
72
+ function drop(n, array) {
73
+ if (n <= 0) {
74
+ return [...array];
75
+ }
76
+ return array.slice(n);
8
77
  }
78
+ function head(array) {
79
+ return array[0];
80
+ }
81
+ function init(array) {
82
+ return array.slice(0, array.length - 1);
83
+ }
84
+ function last(array) {
85
+ return array[array.length - 1];
86
+ }
87
+ function max(array) {
88
+ if (!array.length) {
89
+ return;
90
+ }
91
+ return Math.max(...array);
92
+ }
93
+ function min(array) {
94
+ if (!array.length) {
95
+ return;
96
+ }
97
+ return Math.min(...array);
98
+ }
99
+ function partition(filter, array) {
100
+ return array.reduce(([passed, failed], item, idx, arr) => {
101
+ if (filter(item, idx, arr)) {
102
+ return [[...passed, item], failed];
103
+ }
104
+ return [passed, [...failed, item]];
105
+ }, [[], []]);
106
+ }
107
+ function splitAt(i, array) {
108
+ if (i <= 0) {
109
+ return [[], [...array]];
110
+ }
111
+ return [array.slice(0, i), array.slice(i)];
112
+ }
113
+ function sum(array) {
114
+ if (!array.length) {
115
+ return 0;
116
+ }
117
+ return array.reduce((sum2, current) => sum2 + current, 0);
118
+ }
119
+ function tail(array) {
120
+ return array.slice(1);
121
+ }
122
+ function take(n, array) {
123
+ if (n <= 0) {
124
+ return [];
125
+ }
126
+ return array.slice(0, n);
127
+ }
128
+ function toArray(maybeArray) {
129
+ if (!maybeArray) {
130
+ return [];
131
+ }
132
+ return Array.isArray(maybeArray) ? maybeArray : [maybeArray];
133
+ }
134
+ function uniq(array) {
135
+ return Array.from(new Set(array));
136
+ }
137
+
138
+ // src/function.ts
139
+ function debounce(fn, delay) {
140
+ let timeout;
141
+ return function(...args) {
142
+ clearTimeout(timeout);
143
+ timeout = setTimeout(() => {
144
+ clearTimeout(timeout);
145
+ fn(...args);
146
+ }, delay);
147
+ };
148
+ }
149
+ function memoize(fn) {
150
+ const cache = {};
151
+ return function(...args) {
152
+ const argsString = JSON.stringify(args);
153
+ if (argsString in cache) {
154
+ return cache[argsString];
155
+ }
156
+ const result = fn(...args);
157
+ cache[argsString] = result;
158
+ return result;
159
+ };
160
+ }
161
+ function throttle(fn, limit) {
162
+ let isWaiting = false;
163
+ return function(...args) {
164
+ if (!isWaiting) {
165
+ fn(...args);
166
+ isWaiting = true;
167
+ setTimeout(() => {
168
+ isWaiting = false;
169
+ }, limit);
170
+ }
171
+ };
172
+ }
173
+
174
+ // src/is.ts
175
+ function isArray(val) {
176
+ return Array.isArray(val);
177
+ }
178
+ function isBoolean(val) {
179
+ return typeof val === "boolean";
180
+ }
181
+ function isBrowser() {
182
+ return typeof window !== "undefined";
183
+ }
184
+ function isDefined(val) {
185
+ return typeof val !== "undefined";
186
+ }
187
+ function isFunction(val) {
188
+ return typeof val === "function";
189
+ }
190
+ function isNumber(val) {
191
+ return typeof val === "number";
192
+ }
193
+ function isObject(val) {
194
+ return Object.prototype.toString.call(val) === "[object Object]";
195
+ }
196
+ function isString(val) {
197
+ return typeof val === "string";
198
+ }
199
+ function isWindow(val) {
200
+ return isBrowser() && toString.call(val) === "[object Window]";
201
+ }
202
+
203
+ // src/maths.ts
204
+ function isEven(n) {
205
+ return n % 2 === 0;
206
+ }
207
+ function isOdd(n) {
208
+ return n % 2 !== 0;
209
+ }
210
+ function modulo(n, m) {
211
+ return n - Math.floor(n / m) * m;
212
+ }
213
+ function randomNumber(max2, min2 = 0) {
214
+ return Math.floor(Math.random() * (max2 - min2)) + min2;
215
+ }
216
+ function randomChance(n) {
217
+ return randomNumber(n) === 0;
218
+ }
219
+
220
+ // src/misc.ts
221
+ function noop() {
222
+ return;
223
+ }
224
+ async function sleep(duration) {
225
+ return new Promise((resolve) => setTimeout(resolve, duration));
226
+ }
227
+
228
+ // src/object.ts
229
+ function deepMerge(...objects) {
230
+ return objects.reduce((result, current) => {
231
+ Object.keys(current).forEach((key) => {
232
+ if (isArray(result[key]) && isArray(current[key])) {
233
+ result[key] = uniq(result[key].concat(current[key]));
234
+ } else if (isObject(result[key]) && isObject(current[key])) {
235
+ result[key] = deepMerge(result[key], current[key]);
236
+ } else {
237
+ result[key] = current[key];
238
+ }
239
+ });
240
+ return result;
241
+ }, {});
242
+ }
243
+
244
+ // src/scroll.ts
245
+ function scroll({
246
+ to,
247
+ offset = 0,
248
+ duration = 1e3,
249
+ container = null
250
+ }) {
251
+ const target = isString(to) ? document.querySelector(to) : to;
252
+ const _container = isString(container) ? document.querySelector(container) : container;
253
+ const start = (_container == null ? void 0 : _container.scrollTop) || (window == null ? void 0 : window.pageYOffset) || 0;
254
+ const end = (isNumber(target) ? target : getElTop(target, start)) + offset;
255
+ const startTime = Date.now();
256
+ function step() {
257
+ const timeElapsed = Date.now() - startTime;
258
+ const isScrolling = timeElapsed < duration;
259
+ const position = isScrolling ? getPosition(start, end, timeElapsed, duration) : end;
260
+ if (isScrolling) {
261
+ requestAnimationFrame(step);
262
+ }
263
+ if (_container) {
264
+ _container.scrollTop = position;
265
+ } else {
266
+ window.scrollTo(0, position);
267
+ }
268
+ }
269
+ step();
270
+ }
271
+ function easing(t) {
272
+ return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
273
+ }
274
+ function getPosition(s, e, t, d) {
275
+ return s + (e - s) * easing(t / d);
276
+ }
277
+ function getElTop(element, start) {
278
+ return element.nodeName === "HTML" ? -start : element.getBoundingClientRect().top + start;
279
+ }
280
+
281
+ // src/string.ts
282
+ function capitalise(str, allWords = false) {
283
+ if (!allWords) {
284
+ return str.charAt(0).toUpperCase() + str.slice(1);
285
+ }
286
+ return str.split(" ").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
287
+ }
288
+ function chars(str) {
289
+ return str.split("");
290
+ }
291
+ function isBlank(str) {
292
+ return str.trim().length === 0;
293
+ }
294
+ function isEmpty(str) {
295
+ return str.length === 0;
296
+ }
297
+ function toCamel(str) {
298
+ const RE = /-(\w)/g;
299
+ return str.replace(RE, (_, c) => c ? c.toUpperCase() : "");
300
+ }
301
+ function toKebab(str) {
302
+ const RE = /\B([A-Z])/g;
303
+ return str.replace(RE, "-$1").toLowerCase();
304
+ }
305
+ function toNumber(str) {
306
+ if (!str.length) {
307
+ return NaN;
308
+ }
309
+ return Number(str.replace(/[#£€$,%]/g, ""));
310
+ }
311
+ function truncate(str, length, truncateStr = "...") {
312
+ if (length <= 0) {
313
+ return truncateStr;
314
+ }
315
+ return str.slice(0, length) + truncateStr;
316
+ }
317
+ module.exports = __toCommonJS(src_exports);
318
+ // Annotate the CommonJS export names for ESM import in node:
319
+ 0 && (module.exports = {
320
+ capitalise,
321
+ chars,
322
+ debounce,
323
+ deepMerge,
324
+ drop,
325
+ head,
326
+ init,
327
+ isArray,
328
+ isBlank,
329
+ isBoolean,
330
+ isBrowser,
331
+ isDefined,
332
+ isEmpty,
333
+ isEven,
334
+ isFunction,
335
+ isNumber,
336
+ isObject,
337
+ isOdd,
338
+ isString,
339
+ isWindow,
340
+ last,
341
+ max,
342
+ memoize,
343
+ min,
344
+ modulo,
345
+ noop,
346
+ partition,
347
+ randomChance,
348
+ randomNumber,
349
+ scroll,
350
+ sleep,
351
+ splitAt,
352
+ sum,
353
+ tail,
354
+ take,
355
+ throttle,
356
+ toArray,
357
+ toCamel,
358
+ toKebab,
359
+ toNumber,
360
+ truncate,
361
+ uniq
362
+ });