-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgs-utils.js
More file actions
275 lines (246 loc) · 7.65 KB
/
Copy pathgs-utils.js
File metadata and controls
275 lines (246 loc) · 7.65 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"use strict";
const __LOCALHOST__ = true;
const lg = __LOCALHOST__
? ( ...args ) => ( console.log( ...args ), args[ 0 ] )
: a => a;
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" );
// .............................................................................
// ..... GSUisXxx ..............................................................
// .............................................................................
function GSUisObj( o ) { return o !== null && typeof o === "object"; }
function GSUisArr( a ) { return Array.isArray( a ); }
function GSUisNaN( n ) { return Number.isNaN( n ); }
function GSUisStr( n ) { return typeof n === "string"; }
function GSUisFun( n ) { return typeof n === "function"; }
function GSUisBoo( n ) { return typeof n === "boolean"; }
function GSUisNum( n ) { return typeof n === "number" && !GSUisNaN( n ); }
function GSUisInt( n ) { return GSUisNum( n ) && n === Math.round( n ); }
function GSUisElm( o ) { return o instanceof Element; }
function GSUisJQu( o ) { return o instanceof $$; }
function GSUisEmpty( o ) {
for ( const a in o ) {
return false;
}
return !o?.size;
}
// .............................................................................
function GSUdotProp( obj, path ) {
return !path ? obj : path.split( "." ).filter( Boolean ).reduce( ( obj, p ) => {
return !GSUisObj( obj ) || !( p in obj )
? undefined
: obj[ p ] === undefined
? GSUdotProp.$undefined
: obj[ p ];
}, obj );
}
GSUdotProp.$undefined = Symbol();
// .............................................................................
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_sub( obj, fn, val ) {
let val2 = val;
for ( const k in obj ) {
val2 = fn( val2, obj[ k ], k, obj );
}
return val2;
}
function GSUmap_sub( obj, fn ) {
if ( GSUisObj( obj ) ) {
const obj2 = {};
for ( const k in obj ) {
obj2[ k ] = fn( obj[ k ], k, obj );
}
return obj2;
}
return obj;
}
function GSUreduce( obj, fn, val ) {
return obj?.reduce ? obj.reduce( fn, val ) : GSUreduce_sub( obj, fn, val );
}
function GSUmap( obj, fn ) {
return obj?.map ? obj.map( fn ) : GSUmap_sub( obj, fn );
}
function GSUfindIndex( obj, fn ) {
if ( obj?.findIndex ) {
return obj.findIndex( fn );
}
for ( const k in obj ) {
if ( fn( obj[ k ], k, obj ) ) {
return k;
}
}
return -1;
}
function GSUfind( obj, fn ) {
const ind = GSUfindIndex( obj, fn );
return ind !== -1 ? obj[ ind ] : undefined;
}
function GSUsome( obj, fn ) {
return GSUfind( obj, fn ) !== undefined;
}
// .............................................................................
function GSUisEqual( a, b ) {
return (
Object.is( a, b ) ? true :
GSUisElm( a ) || GSUisElm( b ) ? a?.isEqualNode?.( b ) || false :
!GSUisObj( a ) || !GSUisObj( b ) || GSUisArr( a ) !== GSUisArr( b ) ? false :
!(
GSUsome( a, ( av, ak ) => !GSUisEqual( av, b[ ak ] ) ) ||
GSUsome( b, ( bv, bk ) => !GSUisEqual( bv, a[ bk ] ) )
)
);
}
// .............................................................................
// ..... GSUarray ..............................................................
// .............................................................................
function GSUnewArray( l, fn ) {
return fn === undefined
? new Array( l )
: GSUisFun( fn )
? Array.from( { length: l }, ( _, i ) => fn( i ) )
: Array.from( { length: l } ).fill( fn );
}
function GSUarrayEq( a, b, diff ) {
return a.length === b.length && a.every( diff
? ( a, i ) => GSUmathApprox( a, b[ i ], diff )
: ( a, i ) => a === b[ i ]
);
}
function GSUarrayPushBack( a, ...args ) {
a.push( ...args );
return a;
}
function GSUarrayFrom( a ) {
return GSUisArr( 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( GSUisFun( fn ) ? fn( arr.length ) : fn );
}
}
}
return arr;
}
function GSUarrayRemove( arr, fn ) {
const fn2 = GSUisFun( fn ) ? fn : Object.is.bind( null, fn );
let i = 0;
let j = 0;
if ( arr ) {
for ( ; i < arr.length; ++i ) {
if ( !fn2( arr[ i ] ) ) {
arr[ j++ ] = arr[ i ];
}
}
arr.length = j;
}
return arr;
}
function GSUarrayResize( arr, len ) {
if ( len < 1 || arr.length < 1 ) { return []; }
if ( len === 1 ) { return [ arr[ 0 ] || 0 ]; }
if ( arr.length === len ) { return [ ...arr ]; }
const oldLen = arr.length - 1;
const scale = oldLen / ( len - 1 );
return GSUnewArray( len, i => {
const i2 = i * scale;
const a = Math.floor( i2 );
const b = Math.min( Math.ceil( i2 ), oldLen );
const t = i2 - a;
return ( 1 - t ) * arr[ a ] + t * arr[ b ];
} );
}
// .............................................................................
function GSUtrim2( str ) {
return str?.trim?.().replace( /\s+/ug, " " ) || "";
}
function GSUcountChar( str, c ) {
return Math.max( 0, str?.split?.( c ).length - 1 ) || 0;
}
// .............................................................................
function GSUsplitNums( str, del = " " ) {
return str && GSUisStr( str ) ? str.split( del ).map( n => +n || 0 ) : [];
}
function GSUsplitInts( str, del = " " ) {
return str && GSUisStr( str ) ? str.split( del ).map( n => n | 0 ) : [];
}
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 }`;
}
// .............................................................................
const GSUmodels = new Map();
function GSUsetModel( id, obj ) {
GSUmodels.set( id, GSUdeepFreeze( obj ) );
}
function GSUgetModel( id, obj ) {
const mod = GSUdeepCopy( GSUmodels.get( id ) );
if ( !mod ) {
return null;
}
GSUforEach( obj, ( val, key ) => {
if ( key in mod ) {
mod[ key ] = GSUdeepCopy( val );
}
} );
return Object.seal( mod );
}