-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs-utils.js
215 lines (194 loc) · 5.5 KB
/
gs-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
const GSUonMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/u.test( navigator.userAgent );
const GSUonIphone = [ "iPhone Simulator", "iPhone" ].includes( navigator.platform );
const GSUonOpera = navigator.userAgent.toLowerCase().includes( "op" );
const GSUonChrome = navigator.userAgent.includes( "Chrome" ) && !GSUonOpera;
const GSUonSafari = navigator.userAgent.includes( "Safari" ) && !GSUonChrome;
const GSUonFirefox = navigator.userAgent.includes( "Firefox" );
// .............................................................................
function GSUnoop() {}
function GSUnoopFalse() {
return false;
}
function GSUisNoop( fn ) {
return !fn || fn === GSUnoop || fn === GSUnoopFalse;
}
// .............................................................................
function GSUforEach( obj, fn ) {
if ( obj?.forEach ) {
obj.forEach( fn );
} else {
for ( const k in obj ) {
fn( obj[ k ], k, obj );
}
}
return obj;
}
function GSUreduce( obj, fn, val ) {
return obj?.reduce
? obj.reduce( fn, val )
: _GSUreduce( obj, fn, val );
}
function _GSUreduce( obj, fn, val ) {
let val2 = val;
for ( const k in obj ) {
val2 = fn( val2, obj[ k ], k, obj );
}
return val2;
}
// .............................................................................
function GSUnewArray( l, fn ) {
return fn === undefined
? new Array( l )
: typeof fn === "function"
? Array.from( { length: l }, ( _, i ) => fn( i ) )
: Array.from( { length: l } ).fill( fn );
}
function GSUarrayFrom( a ) {
return Array.isArray( a ) ? a :
"length" in a ? Array.from( a ) : [ a ];
}
function GSUarrayLength( arr, len, fn ) {
if ( arr.length !== len ) {
if ( arr.length > len ) {
arr.length = len;
} else {
while ( arr.length < len ) {
arr.push( typeof fn === "function" ? fn( arr.length ) : fn );
}
}
}
return arr;
}
// .............................................................................
function GSUdebounce( fn, ms ) {
let timeoutId;
return ( ...args ) => {
clearTimeout( timeoutId );
return timeoutId = setTimeout( () => fn( ...args ), ms );
};
}
function GSUthrottle( fn, ms ) {
let timeoutId;
let argsSaved;
return ( ...args ) => {
argsSaved = args;
if ( !timeoutId ) {
timeoutId = setTimeout( () => {
fn( ...argsSaved );
timeoutId = null;
}, ms );
}
return timeoutId;
};
}
// .............................................................................
function GSUisObject( o ) {
return o !== null && typeof o === "object";
}
function GSUisEmpty( o ) {
for ( const a in o ) {
return false;
}
return true;
}
function GSUisntEmpty( o ) {
return !GSUisEmpty( o );
}
// .............................................................................
function GSUtrim2( str ) {
return str ? str.trim().replace( /\s+/ug, " " ) : "";
}
// .............................................................................
function GSUeaseInCirc( n, pow = 2 ) {
return 1 - Math.sqrt( 1 - n ** pow );
}
function GSUeaseOutCirc( n, pow = 2 ) {
return Math.sqrt( 1 - ( n - 1 ) ** pow );
}
// .............................................................................
function GSUisNum( n ) {
return typeof n === "number" && !Number.isNaN( n );
}
function GSUsignNum( n ) {
return n > 0 ? `+${ n }` : `${ n }`;
}
function GSUinRange( n, min, max ) {
return min < max
? min <= n && n <= max
: max <= n && n <= min;
}
function GSUapproxEqual( n, x, diff ) {
return GSUinRange( n, x - diff, x + diff );
}
function GSUroundNum( val, dec = 0 ) {
return typeof val === "number"
? +val.toFixed( dec )
: val.map( n => +n.toFixed( dec ) );
}
function GSUclampNum( n, min, max ) {
return min < max
? Math.max( min, Math.min( n || 0, max ) )
: Math.max( max, Math.min( n || 0, min ) );
}
function GSUsplitNums( str, del = " " ) {
return str.split( del ).map( n => +n );
}
function GSUsum( arr ) {
return arr.reduce( ( sum, n ) => sum + n, 0 );
}
function GSUavg( arr ) {
return GSUsum( arr ) / arr.length || 0;
}
function GSUstack( arr, x ) {
arr.pop();
arr.unshift( x );
return arr;
}
function GSUsplitSeconds( sec ) {
const m = `${ sec / 60 | 0 }`;
const s = `${ sec - m * 60 | 0 }`.padStart( 2, "0" );
const ms = `${ ( sec - ( sec | 0 ) ) * 1000 | 0 }`.padStart( 3, "0" );
return { m, s, ms };
}
// .............................................................................
function GSUuuid() {
const rnd = crypto.getRandomValues( new Uint8Array( 36 ) );
const uuid = rnd.reduce( ( arr, n ) => {
arr.push( ( n % 16 ).toString( 16 ) );
return arr;
}, [] );
uuid[ 14 ] = "4";
uuid[ 19 ] = ( 8 + rnd[ 19 ] % 4 ).toString( 16 );
uuid[ 8 ] =
uuid[ 13 ] =
uuid[ 18 ] =
uuid[ 23 ] = "-";
return uuid.join( "" );
}
// .............................................................................
function GSUuniqueName( nameOri, arr ) {
const name = GSUtrim2( nameOri );
if ( arr.includes( name ) ) {
const name2 = /-\d+$/u.test( name )
? name.substring( 0, name.lastIndexOf( "-" ) ).trim()
: name;
const reg = new RegExp( `^${ name2 }-(\\d+)$`, "u" );
const nb = arr.reduce( ( nb, str ) => {
const res = reg.exec( str );
return res ? Math.max( nb, +res[ 1 ] ) : nb;
}, 1 );
return `${ name2 }-${ nb + 1 }`;
}
return name;
}
// .............................................................................
function GSUplural( nb, word, s ) {
const w = word[ word.length - 1 ] === "s"
? word
: `${ word }${ nb > 1 ? "s" : "" }`;
const ws = s !== "'s"
? w
: `${ w }'${ w[ w.length - 1 ] === "s" ? "" : "s" }`;
return `${ nb } ${ ws }`;
}