tsu 1.2.0 → 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 +1 -1
- package/README.md +4 -1
- package/dist/index.cjs +1 -0
- package/dist/index.js +1 -415
- package/package.json +35 -31
- package/dist/index.mjs +0 -338
package/LICENSE
CHANGED
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
|
|
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.js
CHANGED
|
@@ -1,415 +1 @@
|
|
|
1
|
-
|
|
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
|
-
isEmptyObject: () => isEmptyObject,
|
|
42
|
-
isEven: () => isEven,
|
|
43
|
-
isFunction: () => isFunction,
|
|
44
|
-
isNumber: () => isNumber,
|
|
45
|
-
isObject: () => isObject,
|
|
46
|
-
isOdd: () => isOdd,
|
|
47
|
-
isString: () => isString,
|
|
48
|
-
isTouchDevice: () => isTouchDevice,
|
|
49
|
-
isWindow: () => isWindow,
|
|
50
|
-
last: () => last,
|
|
51
|
-
max: () => max,
|
|
52
|
-
memoize: () => memoize,
|
|
53
|
-
min: () => min,
|
|
54
|
-
modulo: () => modulo,
|
|
55
|
-
noop: () => noop,
|
|
56
|
-
once: () => once,
|
|
57
|
-
partition: () => partition,
|
|
58
|
-
randomChance: () => randomChance,
|
|
59
|
-
randomNumber: () => randomNumber,
|
|
60
|
-
scroll: () => scroll,
|
|
61
|
-
sleep: () => sleep,
|
|
62
|
-
splitAt: () => splitAt,
|
|
63
|
-
sum: () => sum,
|
|
64
|
-
tail: () => tail,
|
|
65
|
-
take: () => take,
|
|
66
|
-
throttle: () => throttle,
|
|
67
|
-
toArray: () => toArray,
|
|
68
|
-
toCamel: () => toCamel,
|
|
69
|
-
toKebab: () => toKebab,
|
|
70
|
-
toNumber: () => toNumber,
|
|
71
|
-
toOrdinal: () => toOrdinal,
|
|
72
|
-
truncate: () => truncate,
|
|
73
|
-
uniq: () => uniq
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// src/array.ts
|
|
77
|
-
function drop(n, array) {
|
|
78
|
-
if (n <= 0) {
|
|
79
|
-
return [...array];
|
|
80
|
-
}
|
|
81
|
-
return array.slice(n);
|
|
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
|
-
}
|
|
96
|
-
function head(array) {
|
|
97
|
-
return array[0];
|
|
98
|
-
}
|
|
99
|
-
function init(array) {
|
|
100
|
-
return array.slice(0, array.length - 1);
|
|
101
|
-
}
|
|
102
|
-
function last(array) {
|
|
103
|
-
return array[array.length - 1];
|
|
104
|
-
}
|
|
105
|
-
function max(array) {
|
|
106
|
-
if (!array.length) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
return Math.max(...array);
|
|
110
|
-
}
|
|
111
|
-
function min(array) {
|
|
112
|
-
if (!array.length) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
return Math.min(...array);
|
|
116
|
-
}
|
|
117
|
-
function partition(filter, array) {
|
|
118
|
-
return array.reduce(([passed, failed], item, idx, arr) => {
|
|
119
|
-
if (filter(item, idx, arr)) {
|
|
120
|
-
return [[...passed, item], failed];
|
|
121
|
-
}
|
|
122
|
-
return [passed, [...failed, item]];
|
|
123
|
-
}, [[], []]);
|
|
124
|
-
}
|
|
125
|
-
function splitAt(i, array) {
|
|
126
|
-
if (i <= 0) {
|
|
127
|
-
return [[], [...array]];
|
|
128
|
-
}
|
|
129
|
-
return [array.slice(0, i), array.slice(i)];
|
|
130
|
-
}
|
|
131
|
-
function sum(array) {
|
|
132
|
-
if (!array.length) {
|
|
133
|
-
return 0;
|
|
134
|
-
}
|
|
135
|
-
return array.reduce((sum2, current) => sum2 + current, 0);
|
|
136
|
-
}
|
|
137
|
-
function tail(array) {
|
|
138
|
-
return array.slice(1);
|
|
139
|
-
}
|
|
140
|
-
function take(n, array) {
|
|
141
|
-
if (n <= 0) {
|
|
142
|
-
return [];
|
|
143
|
-
}
|
|
144
|
-
return array.slice(0, n);
|
|
145
|
-
}
|
|
146
|
-
function toArray(maybeArray) {
|
|
147
|
-
if (!maybeArray) {
|
|
148
|
-
return [];
|
|
149
|
-
}
|
|
150
|
-
return Array.isArray(maybeArray) ? maybeArray : [maybeArray];
|
|
151
|
-
}
|
|
152
|
-
function uniq(array) {
|
|
153
|
-
return Array.from(new Set(array));
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// src/function.ts
|
|
157
|
-
function debounce(fn, delay) {
|
|
158
|
-
let timeout;
|
|
159
|
-
return function(...args) {
|
|
160
|
-
clearTimeout(timeout);
|
|
161
|
-
timeout = setTimeout(() => {
|
|
162
|
-
clearTimeout(timeout);
|
|
163
|
-
fn(...args);
|
|
164
|
-
}, delay);
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
function memoize(fn) {
|
|
168
|
-
const cache = {};
|
|
169
|
-
return function(...args) {
|
|
170
|
-
const argsString = JSON.stringify(args);
|
|
171
|
-
if (argsString in cache) {
|
|
172
|
-
return cache[argsString];
|
|
173
|
-
}
|
|
174
|
-
const result = fn(...args);
|
|
175
|
-
cache[argsString] = result;
|
|
176
|
-
return result;
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
function once(fn) {
|
|
180
|
-
let run = false;
|
|
181
|
-
return (...args) => {
|
|
182
|
-
if (!run) {
|
|
183
|
-
fn(...args);
|
|
184
|
-
run = true;
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
function throttle(fn, limit) {
|
|
189
|
-
let isWaiting = false;
|
|
190
|
-
return function(...args) {
|
|
191
|
-
if (!isWaiting) {
|
|
192
|
-
fn(...args);
|
|
193
|
-
isWaiting = true;
|
|
194
|
-
setTimeout(() => {
|
|
195
|
-
isWaiting = false;
|
|
196
|
-
}, limit);
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// src/is.ts
|
|
202
|
-
function isArray(val) {
|
|
203
|
-
return Array.isArray(val);
|
|
204
|
-
}
|
|
205
|
-
function isBoolean(val) {
|
|
206
|
-
return typeof val === "boolean";
|
|
207
|
-
}
|
|
208
|
-
function isBrowser() {
|
|
209
|
-
return typeof window !== "undefined";
|
|
210
|
-
}
|
|
211
|
-
function isDefined(val) {
|
|
212
|
-
return typeof val !== "undefined";
|
|
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
|
-
}
|
|
223
|
-
function isFunction(val) {
|
|
224
|
-
return typeof val === "function";
|
|
225
|
-
}
|
|
226
|
-
function isNumber(val) {
|
|
227
|
-
return typeof val === "number";
|
|
228
|
-
}
|
|
229
|
-
function isObject(val) {
|
|
230
|
-
return Object.prototype.toString.call(val) === "[object Object]";
|
|
231
|
-
}
|
|
232
|
-
function isString(val) {
|
|
233
|
-
return typeof val === "string";
|
|
234
|
-
}
|
|
235
|
-
function isTouchDevice() {
|
|
236
|
-
return "ontouchstart" in window || navigator.maxTouchPoints > 0;
|
|
237
|
-
}
|
|
238
|
-
function isWindow(val) {
|
|
239
|
-
return isBrowser() && toString.call(val) === "[object Window]";
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// src/maths.ts
|
|
243
|
-
function isEven(n) {
|
|
244
|
-
return n % 2 === 0;
|
|
245
|
-
}
|
|
246
|
-
function isOdd(n) {
|
|
247
|
-
return n % 2 !== 0;
|
|
248
|
-
}
|
|
249
|
-
function modulo(n, m) {
|
|
250
|
-
return n - Math.floor(n / m) * m;
|
|
251
|
-
}
|
|
252
|
-
function randomNumber(max2, min2 = 0) {
|
|
253
|
-
return Math.floor(Math.random() * (max2 - min2)) + min2;
|
|
254
|
-
}
|
|
255
|
-
function randomChance(n) {
|
|
256
|
-
return randomNumber(n) === 0;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// src/misc.ts
|
|
260
|
-
function noop() {
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
async function sleep(duration) {
|
|
264
|
-
return new Promise((resolve) => setTimeout(resolve, duration));
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// src/object.ts
|
|
268
|
-
function deepMerge(...objects) {
|
|
269
|
-
return objects.reduce((result, current) => {
|
|
270
|
-
Object.keys(current).forEach((key) => {
|
|
271
|
-
if (isArray(result[key]) && isArray(current[key])) {
|
|
272
|
-
result[key] = uniq(result[key].concat(current[key]));
|
|
273
|
-
} else if (isObject(result[key]) && isObject(current[key])) {
|
|
274
|
-
result[key] = deepMerge(result[key], current[key]);
|
|
275
|
-
} else {
|
|
276
|
-
result[key] = current[key];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
return result;
|
|
280
|
-
}, {});
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// src/scroll.ts
|
|
284
|
-
function scroll({
|
|
285
|
-
to,
|
|
286
|
-
offset = 0,
|
|
287
|
-
duration = 1e3,
|
|
288
|
-
container = null
|
|
289
|
-
}) {
|
|
290
|
-
const target = isString(to) ? document.querySelector(to) : to;
|
|
291
|
-
const _container = isString(container) ? document.querySelector(container) : container;
|
|
292
|
-
const start = (_container == null ? void 0 : _container.scrollTop) || (window == null ? void 0 : window.pageYOffset) || 0;
|
|
293
|
-
const end = (isNumber(target) ? target : getElTop(target, start)) + offset;
|
|
294
|
-
const startTime = Date.now();
|
|
295
|
-
function step() {
|
|
296
|
-
const timeElapsed = Date.now() - startTime;
|
|
297
|
-
const isScrolling = timeElapsed < duration;
|
|
298
|
-
const position = isScrolling ? getPosition(start, end, timeElapsed, duration) : end;
|
|
299
|
-
if (isScrolling) {
|
|
300
|
-
requestAnimationFrame(step);
|
|
301
|
-
}
|
|
302
|
-
if (_container) {
|
|
303
|
-
_container.scrollTop = position;
|
|
304
|
-
} else {
|
|
305
|
-
window.scrollTo(0, position);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
step();
|
|
309
|
-
}
|
|
310
|
-
function easing(t) {
|
|
311
|
-
return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
|
312
|
-
}
|
|
313
|
-
function getPosition(s, e, t, d) {
|
|
314
|
-
return s + (e - s) * easing(t / d);
|
|
315
|
-
}
|
|
316
|
-
function getElTop(element, start) {
|
|
317
|
-
return element.nodeName === "HTML" ? -start : element.getBoundingClientRect().top + start;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// src/string.ts
|
|
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);
|
|
324
|
-
}
|
|
325
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
326
|
-
}
|
|
327
|
-
function chars(str) {
|
|
328
|
-
return str.split("");
|
|
329
|
-
}
|
|
330
|
-
function isBlank(str) {
|
|
331
|
-
return str.trim().length === 0;
|
|
332
|
-
}
|
|
333
|
-
function isEmpty(str) {
|
|
334
|
-
return str.length === 0;
|
|
335
|
-
}
|
|
336
|
-
function toCamel(str) {
|
|
337
|
-
const RE = /-(\w)/g;
|
|
338
|
-
return str.replace(RE, (_, c) => c ? c.toUpperCase() : "");
|
|
339
|
-
}
|
|
340
|
-
function toKebab(str) {
|
|
341
|
-
const RE = /\B([A-Z])/g;
|
|
342
|
-
return str.replace(RE, "-$1").toLowerCase();
|
|
343
|
-
}
|
|
344
|
-
function toNumber(str) {
|
|
345
|
-
if (!str.length) {
|
|
346
|
-
return NaN;
|
|
347
|
-
}
|
|
348
|
-
return Number(str.replace(/[#£€$,%]/g, ""));
|
|
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
|
-
}
|
|
359
|
-
function truncate(str, length, truncateStr = "...") {
|
|
360
|
-
if (length <= 0) {
|
|
361
|
-
return truncateStr;
|
|
362
|
-
}
|
|
363
|
-
return str.slice(0, length) + truncateStr;
|
|
364
|
-
}
|
|
365
|
-
module.exports = __toCommonJS(src_exports);
|
|
366
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
367
|
-
0 && (module.exports = {
|
|
368
|
-
capitalise,
|
|
369
|
-
chars,
|
|
370
|
-
debounce,
|
|
371
|
-
deepMerge,
|
|
372
|
-
drop,
|
|
373
|
-
groupsOf,
|
|
374
|
-
head,
|
|
375
|
-
init,
|
|
376
|
-
isArray,
|
|
377
|
-
isBlank,
|
|
378
|
-
isBoolean,
|
|
379
|
-
isBrowser,
|
|
380
|
-
isDefined,
|
|
381
|
-
isEmpty,
|
|
382
|
-
isEmptyObject,
|
|
383
|
-
isEven,
|
|
384
|
-
isFunction,
|
|
385
|
-
isNumber,
|
|
386
|
-
isObject,
|
|
387
|
-
isOdd,
|
|
388
|
-
isString,
|
|
389
|
-
isTouchDevice,
|
|
390
|
-
isWindow,
|
|
391
|
-
last,
|
|
392
|
-
max,
|
|
393
|
-
memoize,
|
|
394
|
-
min,
|
|
395
|
-
modulo,
|
|
396
|
-
noop,
|
|
397
|
-
once,
|
|
398
|
-
partition,
|
|
399
|
-
randomChance,
|
|
400
|
-
randomNumber,
|
|
401
|
-
scroll,
|
|
402
|
-
sleep,
|
|
403
|
-
splitAt,
|
|
404
|
-
sum,
|
|
405
|
-
tail,
|
|
406
|
-
take,
|
|
407
|
-
throttle,
|
|
408
|
-
toArray,
|
|
409
|
-
toCamel,
|
|
410
|
-
toKebab,
|
|
411
|
-
toNumber,
|
|
412
|
-
toOrdinal,
|
|
413
|
-
truncate,
|
|
414
|
-
uniq
|
|
415
|
-
});
|
|
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.2.
|
|
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
|
-
"
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://gitlab.com/eb3n/tsu/issues"
|
|
27
|
+
},
|
|
11
28
|
"scripts": {
|
|
12
|
-
"build": "tsup
|
|
13
|
-
"watch": "
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"watch": "nr build --watch src",
|
|
14
31
|
"test": "jest",
|
|
15
|
-
"prepublishOnly": "
|
|
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
|
-
"@
|
|
34
|
-
"@
|
|
35
|
-
"@typescript-eslint/
|
|
36
|
-
"eslint": "^
|
|
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.
|
|
39
|
-
"jest": "^
|
|
40
|
-
"prettier": "^2.
|
|
41
|
-
"ts-jest": "^
|
|
42
|
-
"tsup": "^
|
|
43
|
-
"typescript": "^4.
|
|
44
|
-
"vuepress": "
|
|
45
|
-
}
|
|
46
|
-
"dependencies": {}
|
|
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"
|
|
50
|
+
}
|
|
47
51
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,338 +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(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
|
-
}
|
|
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 once(fn) {
|
|
105
|
-
let run = false;
|
|
106
|
-
return (...args) => {
|
|
107
|
-
if (!run) {
|
|
108
|
-
fn(...args);
|
|
109
|
-
run = true;
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
function throttle(fn, limit) {
|
|
114
|
-
let isWaiting = false;
|
|
115
|
-
return function(...args) {
|
|
116
|
-
if (!isWaiting) {
|
|
117
|
-
fn(...args);
|
|
118
|
-
isWaiting = true;
|
|
119
|
-
setTimeout(() => {
|
|
120
|
-
isWaiting = false;
|
|
121
|
-
}, limit);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// src/is.ts
|
|
127
|
-
function isArray(val) {
|
|
128
|
-
return Array.isArray(val);
|
|
129
|
-
}
|
|
130
|
-
function isBoolean(val) {
|
|
131
|
-
return typeof val === "boolean";
|
|
132
|
-
}
|
|
133
|
-
function isBrowser() {
|
|
134
|
-
return typeof window !== "undefined";
|
|
135
|
-
}
|
|
136
|
-
function isDefined(val) {
|
|
137
|
-
return typeof val !== "undefined";
|
|
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
|
-
}
|
|
148
|
-
function isFunction(val) {
|
|
149
|
-
return typeof val === "function";
|
|
150
|
-
}
|
|
151
|
-
function isNumber(val) {
|
|
152
|
-
return typeof val === "number";
|
|
153
|
-
}
|
|
154
|
-
function isObject(val) {
|
|
155
|
-
return Object.prototype.toString.call(val) === "[object Object]";
|
|
156
|
-
}
|
|
157
|
-
function isString(val) {
|
|
158
|
-
return typeof val === "string";
|
|
159
|
-
}
|
|
160
|
-
function isTouchDevice() {
|
|
161
|
-
return "ontouchstart" in window || navigator.maxTouchPoints > 0;
|
|
162
|
-
}
|
|
163
|
-
function isWindow(val) {
|
|
164
|
-
return isBrowser() && toString.call(val) === "[object Window]";
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// src/maths.ts
|
|
168
|
-
function isEven(n) {
|
|
169
|
-
return n % 2 === 0;
|
|
170
|
-
}
|
|
171
|
-
function isOdd(n) {
|
|
172
|
-
return n % 2 !== 0;
|
|
173
|
-
}
|
|
174
|
-
function modulo(n, m) {
|
|
175
|
-
return n - Math.floor(n / m) * m;
|
|
176
|
-
}
|
|
177
|
-
function randomNumber(max2, min2 = 0) {
|
|
178
|
-
return Math.floor(Math.random() * (max2 - min2)) + min2;
|
|
179
|
-
}
|
|
180
|
-
function randomChance(n) {
|
|
181
|
-
return randomNumber(n) === 0;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// src/misc.ts
|
|
185
|
-
function noop() {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
async function sleep(duration) {
|
|
189
|
-
return new Promise((resolve) => setTimeout(resolve, duration));
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// src/object.ts
|
|
193
|
-
function deepMerge(...objects) {
|
|
194
|
-
return objects.reduce((result, current) => {
|
|
195
|
-
Object.keys(current).forEach((key) => {
|
|
196
|
-
if (isArray(result[key]) && isArray(current[key])) {
|
|
197
|
-
result[key] = uniq(result[key].concat(current[key]));
|
|
198
|
-
} else if (isObject(result[key]) && isObject(current[key])) {
|
|
199
|
-
result[key] = deepMerge(result[key], current[key]);
|
|
200
|
-
} else {
|
|
201
|
-
result[key] = current[key];
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
return result;
|
|
205
|
-
}, {});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// src/scroll.ts
|
|
209
|
-
function scroll({
|
|
210
|
-
to,
|
|
211
|
-
offset = 0,
|
|
212
|
-
duration = 1e3,
|
|
213
|
-
container = null
|
|
214
|
-
}) {
|
|
215
|
-
const target = isString(to) ? document.querySelector(to) : to;
|
|
216
|
-
const _container = isString(container) ? document.querySelector(container) : container;
|
|
217
|
-
const start = (_container == null ? void 0 : _container.scrollTop) || (window == null ? void 0 : window.pageYOffset) || 0;
|
|
218
|
-
const end = (isNumber(target) ? target : getElTop(target, start)) + offset;
|
|
219
|
-
const startTime = Date.now();
|
|
220
|
-
function step() {
|
|
221
|
-
const timeElapsed = Date.now() - startTime;
|
|
222
|
-
const isScrolling = timeElapsed < duration;
|
|
223
|
-
const position = isScrolling ? getPosition(start, end, timeElapsed, duration) : end;
|
|
224
|
-
if (isScrolling) {
|
|
225
|
-
requestAnimationFrame(step);
|
|
226
|
-
}
|
|
227
|
-
if (_container) {
|
|
228
|
-
_container.scrollTop = position;
|
|
229
|
-
} else {
|
|
230
|
-
window.scrollTo(0, position);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
step();
|
|
234
|
-
}
|
|
235
|
-
function easing(t) {
|
|
236
|
-
return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
|
237
|
-
}
|
|
238
|
-
function getPosition(s, e, t, d) {
|
|
239
|
-
return s + (e - s) * easing(t / d);
|
|
240
|
-
}
|
|
241
|
-
function getElTop(element, start) {
|
|
242
|
-
return element.nodeName === "HTML" ? -start : element.getBoundingClientRect().top + start;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// src/string.ts
|
|
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);
|
|
249
|
-
}
|
|
250
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
251
|
-
}
|
|
252
|
-
function chars(str) {
|
|
253
|
-
return str.split("");
|
|
254
|
-
}
|
|
255
|
-
function isBlank(str) {
|
|
256
|
-
return str.trim().length === 0;
|
|
257
|
-
}
|
|
258
|
-
function isEmpty(str) {
|
|
259
|
-
return str.length === 0;
|
|
260
|
-
}
|
|
261
|
-
function toCamel(str) {
|
|
262
|
-
const RE = /-(\w)/g;
|
|
263
|
-
return str.replace(RE, (_, c) => c ? c.toUpperCase() : "");
|
|
264
|
-
}
|
|
265
|
-
function toKebab(str) {
|
|
266
|
-
const RE = /\B([A-Z])/g;
|
|
267
|
-
return str.replace(RE, "-$1").toLowerCase();
|
|
268
|
-
}
|
|
269
|
-
function toNumber(str) {
|
|
270
|
-
if (!str.length) {
|
|
271
|
-
return NaN;
|
|
272
|
-
}
|
|
273
|
-
return Number(str.replace(/[#£€$,%]/g, ""));
|
|
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
|
-
}
|
|
284
|
-
function truncate(str, length, truncateStr = "...") {
|
|
285
|
-
if (length <= 0) {
|
|
286
|
-
return truncateStr;
|
|
287
|
-
}
|
|
288
|
-
return str.slice(0, length) + truncateStr;
|
|
289
|
-
}
|
|
290
|
-
export {
|
|
291
|
-
capitalise,
|
|
292
|
-
chars,
|
|
293
|
-
debounce,
|
|
294
|
-
deepMerge,
|
|
295
|
-
drop,
|
|
296
|
-
groupsOf,
|
|
297
|
-
head,
|
|
298
|
-
init,
|
|
299
|
-
isArray,
|
|
300
|
-
isBlank,
|
|
301
|
-
isBoolean,
|
|
302
|
-
isBrowser,
|
|
303
|
-
isDefined,
|
|
304
|
-
isEmpty,
|
|
305
|
-
isEmptyObject,
|
|
306
|
-
isEven,
|
|
307
|
-
isFunction,
|
|
308
|
-
isNumber,
|
|
309
|
-
isObject,
|
|
310
|
-
isOdd,
|
|
311
|
-
isString,
|
|
312
|
-
isTouchDevice,
|
|
313
|
-
isWindow,
|
|
314
|
-
last,
|
|
315
|
-
max,
|
|
316
|
-
memoize,
|
|
317
|
-
min,
|
|
318
|
-
modulo,
|
|
319
|
-
noop,
|
|
320
|
-
once,
|
|
321
|
-
partition,
|
|
322
|
-
randomChance,
|
|
323
|
-
randomNumber,
|
|
324
|
-
scroll,
|
|
325
|
-
sleep,
|
|
326
|
-
splitAt,
|
|
327
|
-
sum,
|
|
328
|
-
tail,
|
|
329
|
-
take,
|
|
330
|
-
throttle,
|
|
331
|
-
toArray,
|
|
332
|
-
toCamel,
|
|
333
|
-
toKebab,
|
|
334
|
-
toNumber,
|
|
335
|
-
toOrdinal,
|
|
336
|
-
truncate,
|
|
337
|
-
uniq
|
|
338
|
-
};
|