-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
1312 lines (1205 loc) · 43.5 KB
/
Copy patharray.js
File metadata and controls
1312 lines (1205 loc) · 43.5 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var _glob_array_recursive_run = 1 ;
Array.recursive_run = 1 ;
if ( typeof array_set_recursive_run != "function" ) function array_set_recursive_run( _r ) { _glob_array_recursive_run = _r ; }
if ( typeof array_default_settings != "function" ) function array_default_settings(){ _glob_array_recursive_run = 1 ; }
if ( typeof is_array != "function" ) function is_array( _a ) { return _a instanceof Array ? 1 : 0 ; }
if ( typeof is_consistent_array != "function" ) function is_consistent_array( _a ) { return is_array( _a ) ? ( ( _a.length > 0 || _a.size_associative() > 0 ) ? 1 : 0 ) : 0 ; }
if ( typeof safe_string != "function" ) function safe_string( _obj, _default_str ) { return ( typeof _obj == "string" || _obj instanceof String ) ? new String( _obj ).trim() : new String( _default_str + "" ).trim() ; }
if ( typeof safe_int != "function" ) function safe_int( _val, _set_if_nan ) { _val = parseInt( _val, 10 ); return isNaN( _val ) ? ( isNaN( _set_if_nan ) ? 0 : _set_if_nan ) : _val ; }
if ( typeof safe_float != "function" ) function safe_float( _val, _set_if_nan ) { _val = parseFloat( _val ); return isNaN( _val ) ? ( isNaN( _set_if_nan ) ? 0 : _set_if_nan ) : _val ; }
if ( !Array.reset_index ) { Array.prototype.reset_index = function() { return this.filter(function(){return true;}) ; } }
if ( !Array.flush ) { Array.prototype.flush = function() { while( this.length > 0 ) this.pop(); } }
if ( !Array.has_duplicates ) { Array.prototype.has_duplicates = function() { return this.length != this.unique().length ? 1 : 0 ; } }
if ( !Array.left ) { Array.prototype.left = function( n ) { return this.subset( n ); } }
if ( !Array.trunc ) { Array.prototype.trunc = function( n ) { return this.left( n ); } }
if ( !Array.from_to ) { Array.prototype.from_to = function( _from_index, _to_index ) { return arguments.length == 1 ? this.slice( _from_index, this.length ) : this.slice( _from_index, _to_index + 1 ); } }
if ( !Array.get_first ) { Array.prototype.get_first = function() { return this.length > 0 ? this[0] : null ; } }
if ( !Array.get_last ) { Array.prototype.get_last = function() { return this.length > 0 ? this[ this.length - 1 ] : null ; } }
if ( !Array.pop_first ) { Array.prototype.pop_first = function() { var _out = this[0] ; this.slice( 1, this.length - 1 ) ; return _out ; } }
if ( !Array.tail ) { Array.prototype.tail = function( _n ) { return this.slice( -_n ) ; } }
if ( !Array.push_safe ) { Array.prototype.push_safe = function( obj ) { if ( this.indexOf( obj ) == -1 ) this.push( obj ) ; } }
if ( !Array.push_first ) { Array.prototype.push_first = function( _item ) { return ( new Array( _item ) ).concat( this ) ; } }
if ( !Array.get_central ) { Array.prototype.get_central = function() { var _n = this.length ; return _n % 2 == 1 ? this[ Math.floor( _n / 2 ) ] : null ; } }
if ( !Array.right ) { Array.prototype.right = function( n ) { return this.subset( -n ); } }
if ( !Array.slide_back ) { Array.prototype.slide_back = function() { return ( this.slice(1) ).concat( this.slice( 0,1 ) ) ; } }
if ( !Array.slide_forward ) { Array.prototype.slide_forward = function() { return ( this.slice(this.length-1).concat( this.slice(0,this.length-1) ) ) ; } }
// methods for associative arrays
if ( !Array.insert_associative ) { Array.prototype.insert_associative = function( key, val ) { this[ key ] = val; }; }
if ( !Array.remove_key ){ Array.prototype.remove_key = function( key ) { if ( this.hasOwnProperty( key ) ) delete this[ key ] ; }; }
//////
if ( !Array.recursive_indexOf )
{
Array.prototype.recursive_indexOf = function( obj, _level, _index_rec, _flag )
{
if ( _level == null || _level == "undefined" ) _level = 0 ;
_b_found = 0 ;
outer_loop:
for ( var _i = 0; _i < this.length; _i++ )
{
if ( _level == 0 ) _flag = [] ; // I create an array so I can use it as a call-by-reference var during recursion
if ( is_array( this[_i] ) )
{
// it's required to record the downward path in a separate array,
// when a nested array is detected and then recursion gets in
if ( _level == 0 ) _index_rec = [ _i ] ;
else _index_rec.push( _i ) ;
this[_i].recursive_indexOf( obj, _level + 1, _index_rec, _flag ) ;
if ( _flag[0] == 1 ) break outer_loop ;
}
else
{
if( JSON.stringify( this[_i] ) == JSON.stringify( obj ) )
{
if ( _level == 0 || _index_rec == null ) _index_rec = [] ;
_index_rec.push( _i ) ;
_flag.push( 1 ) ;
_b_found = 1 ;
break outer_loop ;
}
}
}
if ( !_b_found )
{
if ( _index_rec == null ) _index_rec = [] ;
_index_rec.push( -1 ) ;
}
// if index is found at level 0, the integer index is returned
// otherwise this member returns an array collecting the indexes
// leading download into the nested arrays
return _index_rec == null ? -1 : ( _index_rec.length == 0 ? -1 : ( _index_rec.length == 1 ? _index_rec[0] : _index_rec ) ) ;
}
}
if ( !Array.index_remap )
{
Array.prototype.index_remap = function( _src_map, _new_array_size, _default_val )
{
if ( is_array( _src_map ) )
{
if ( _default_val == undefined ) _default_val = null ;
_new_array_size = safe_int( _new_array_size, 0 );
if ( _new_array_size == 0 ) _new_array_size = this.length ;
_src_map = _src_map.work( function( _i ) { return safe_int( _i, -1 ) ; } ) ;
var _ok_src_index = 1 ;
_src_map.test( function( _i ) { if ( _i < 0 || _i > ( _new_array_size - 1 ) ) _ok_src_index = 0 ; } ) ;
if ( !_ok_src_index ) return null ;
var _new_array = [] ;
for( var _i = 0 ; _i < _new_array_size ; _i++ ) _new_array.push( _default_val );
for( _i = 0 ; _i < _src_map.length ; _i++ ) _new_array[ _src_map[_i] ] = this[_i];
return _new_array.clone() ;
}
else return null ;
}
}
if ( !Array.getAt )
{
Array.prototype.getAt = function()
{
var _cmd = "this", _ret, _indexes = [], _a ;
if ( is_array( arguments[0] ) ) _indexes = arguments[0] ;
else for( _a = 0 ; _a < arguments.length ; _a++ ) _indexes.push( safe_int( arguments[_a], 0 ) ) ;
for( _a = 0 ; _a < _indexes.length ; _a++ ) _cmd += "["+_indexes[_a]+"]" ;
try { _ret = eval( _cmd ) ; } catch( err ) {}
return _ret ;
}
}
if( !Array.count )
{
// counts recursively how many elements of the same input type have been stored inside the array
Array.prototype.count = function( obj )
{
var _c = 0, _ref = arguments[1] == null ? this : arguments[1] ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _c += _ref[_property].count( obj, _ref[_property] ) ;
else if( JSON.stringify( _ref[_property] ) === JSON.stringify( obj ) ) _c++ ;
}
}
return _c ;
}
}
if ( !Array.is_multidimensional )
{
Array.prototype.is_multidimensional = function()
{
var _b_multi = 0 ;
for ( var _property in this )
{
if ( this.hasOwnProperty( _property ) )
{
if ( is_array( this[_property] ) && this.recursive_run )
{
_b_multi = 1 ;
break ;
}
}
}
return _b_multi ;
}
}
if ( !Array.multidimensional_depth_count )
{
// get the lowest depth (or maximal nesting level) for sub arrays
Array.prototype.multidimensional_depth_count = function()
{
var _ref = arguments[0] == null ? this : arguments[0] ;
var _data = arguments[1] == null ? [ 1 ] : arguments[1] ;
var _level = arguments[0] == null ? 0 : arguments[2] ;
for ( var _property in this )
{
if ( this.hasOwnProperty( _property ) )
{
if ( is_array( this[_property] ) && this.recursive_run )
{
if ( _level > _data[0] ) _data[0]++ ;
this[_property].multidimensional_depth_count( this[_property], _data, _level + 1 ) ;
}
}
}
return _data[0] ;
}
}
if ( !Array.multidimensional_level_get )
{
// return all entries at the input level, i.e. calling multidimensional_level_get(3) returns all entries at level 3
Array.prototype.multidimensional_level_get = function()
{
var _input_level = arguments[0] == null ? 1 : arguments[0] ;
var _curr_level = arguments[1] == null ? 1 : arguments[1] ;
var _ref = arguments[2] == null ? this : arguments[2] ;
var _data = arguments[3] == null ? [] : arguments[3] ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( _curr_level == _input_level ) _data.push( _ref[_property] );
if ( is_array( _ref[_property] ) && this.recursive_run )
this[_property].multidimensional_level_get( _input_level, _curr_level+1, _ref[_property], _data ) ;
}
}
return _data ;
}
}
if ( !Array.size_recursive )
{
Array.prototype.size_recursive = function()
{
var _size = 0 ;
var _ref = arguments[0] == null ? this : arguments[0] ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
_size += ( is_array( _ref[_property] ) ) ? _ref[_property].size_recursive( _ref[_property] ) : 1 ;
}
return _size ;
}
}
if ( !Array.append )
{
// #1 .append( [ 1, 2, 3 ] ) --> append an array
// #2 .append( _obj1, _obj2 ) --> append _obj1 and _obj2
Array.prototype.append = function()
{
var _arr = this ;
for( var _a = 0 ; _a < arguments.length ; _a++ )
{
if ( is_array( arguments[_a] ) ) _arr = _arr.concat( arguments[_a] ) ;
else _arr.push( arguments[_a] );
}
return _arr ;
}
}
if ( !Array.push_at )
{
Array.prototype.push_at = function( _item, _pos )
{
var _len = this.length ;
_pos = Math.max( 0, safe_int( _pos, -1 ) );
if ( _pos == -1 || _pos < 0 || _pos > _len ) return [] ;
else return [].concat( this.slice( 0, _pos ) ).concat( [ _item ] ).concat( this.slice( _pos, _len ) ) ;
}
}
if ( !Array.prototype.fill )
{
Array.prototype.fill = function( _obj )
{
var _ref = arguments[1] == null ? this : arguments[1] ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _ref[_property].fill( _obj, _ref[_property] ) ;
else _ref[_property] = _obj ;
}
}
}
}
if ( !Array.prototype.get_linear_array )
{
Array.prototype.get_linear_array = function()
{
// returns a one dimensional (or linear) array
var _ref = arguments[0] == null ? this : arguments[0] ;
var _level = arguments[1] == null ? 0 : arguments[1] ;
var _ret = arguments[2] == null ? [] : arguments[2] ;
if ( !_ref.is_multidimensional() && _level == 0 ) return this ;
else
{
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _ref[_property].get_linear_array( _ref[_property], _level + 1, _ret ) ;
else _ret.push( _ref[_property] ) ;
}
}
return _ret ;
}
}
}
if ( !Array.prototype.clean_from )
{
Array.prototype.clean_from = function()
{
var _ret, _i ;
var _is_fn = typeof arguments[0] === "function" ? 1 : 0, _ret = 0 ;
var _arg = arguments[0] ;
var _ref = arguments[1] == null ? this : arguments[1] ;
var _parent_ref = arguments[2] == null ? this : arguments[2] ;
var _prop_key = arguments[3] == null ? "" : arguments[3] ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) this.clean_from( _arg, _ref[_property], _ref, _property ) ;
else
{
_ret = _is_fn ? _arg.call( null, _ref[_property] ) : ( JSON.stringify( _ref[_property] ) === JSON.stringify( _arg ) ) ;
if ( _ret )
{
_ref.delete_entry( _ref[_property] ) ;
if ( _ref.size_recursive() == 0 ) _parent_ref.remove_key( _prop_key );
_ref.clean_from( _arg );
}
}
}
}
}
}
if ( !Array.prototype.purge ) // removes undefined elements
{
Array.prototype.purge = function( _self )
{
_self = safe_int( _self, 1 );
if ( _self ) this.clean_from( function( _obj ) { return _obj === undefined ; } ) ;
else
{
var _obj = this.clone(); _obj.purge();
return _obj ;
}
}
}
if ( !Array.prototype.find_it )
{
Array.prototype.find_it = function()
{
var _is_fn = typeof arguments[0] === "function" ? 1 : 0, _ret = 0 ;
var _tester = arguments[0] ; if ( _tester == null ) return null ;
var _ref = arguments[1] == null ? this : arguments[1] ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _ret = _ref[_property].find_it( _tester, _ref[_property] ) ;
else
{
_ret = _is_fn ? _tester.call( null, _ref[_property] ) : ( JSON.stringify( _ref[_property] ) === JSON.stringify( _tester ) ) ;
if ( _ret )
{
_ret = _property ;
break ;
}
}
}
}
return _ret ;
}
}
if ( !Array.prototype.check_descendent_properties )
{
Array.prototype.check_descendent_properties = function()
{
// it goes all the way down along the arguments list of properties of sub-arrays, if existing
// example #1 : check_descendent_properties( 0, 1 )
// >> check item indexed as 0 and, if the latter is a sub-array, checks for sub-items indexed as 1
// example #2 : check_descendent_properties( 0, 'prop1' )
// >> check item indexed as 0 and, if the latter is a sub-array, checks for sub-items tagged as 'prop1'
// example #3 : check_descendent_properties( 'level1', 'level2' )
// >> check item tagged as 'level1' and, if the latter is a sub-array, checks for sub-items tagged as 'level2'
var _bookmark = null, _ret = 1, _i ;
for( _i = 0 ; _i < arguments.length ; _i++ )
{
_bookmark = _i == 0 ? this[ arguments[_i] ] : _bookmark[ arguments[_i] ] ;
if ( _bookmark == null )
{
_ret = 0 ;
break ;
}
else _ret = 1 ;
}
return _ret ;
}
}
if ( !Array.prototype.shuffle )
{
// no need to shuffle it recursively
Array.prototype.shuffle = function()
{
var array = this, j, temp ;
for ( var i = array.length - 1; i > 0; i--)
{
j = Math.floor(Math.random() * (i + 1));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
}
if ( !Array.prototype.test )
{
Array.prototype.test = function()
{
var _is_fn = typeof arguments[0] === "function" ? 1 : 0 ;
var _tester = arguments[0] ; if ( !_is_fn ) return 0 ;
var _ref = arguments[1] == null ? this : arguments[1], _b_ok = 1 ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _b_ok &= _ref[_property].test( _tester, _ref[_property] ) ;
else
{
if ( !_tester.call( null, _ref[_property] ) )
{
_b_ok = 0 ;
break ;
}
}
}
}
return _b_ok ;
};
}
if ( !Array.prototype.work )
{
Array.prototype.work = function()
{
var _is_fn = typeof arguments[0] === "function" ? 1 : 0 ;
if ( !_is_fn ) return 0 ;
var _tester = arguments[0] ;
_recursive = safe_int( arguments[1], 0 );
var _ref = arguments[2] == null ? this : arguments[2], _out_array = [], _ret ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run )
_ret = _ref[_property].work( _tester, _recursive, _ref[_property], _property );
else
_ret = _tester.call( null, _ref[_property], _property ) ;
if ( _ret === false ) break ;
_out_array[_property] = _ret ;
}
}
return _out_array ;
};
}
if ( !Array.prototype.filtering )
{
Array.prototype.filtering = function()
{
// filters and preserves structure
var _is_fn = typeof arguments[0] === "function" ? 1 : 0 ;
if ( !_is_fn ) return 0 ;
var _tester = arguments[0] ;
var _ref = arguments[1] == null ? this : arguments[1];
var _level = arguments[2] == null ? 0 : arguments[2];
var _out_array = [], _ret ;
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run )
{
_ret = _ref[_property].filtering( _tester, _ref[_property], _level + 1 );
if ( _ret.size_recursive() > 0 ) _out_array[_property] = _ret ;
}
else if ( typeof _tester === "function" )
{
if ( _tester.call( null, _ref[_property] ) ) _out_array[_property] = _ref[_property] ;
}
}
}
_out_array.purge() ;
return _out_array.clone() ;
};
}
if ( !Array.print_r )
{
Array.prototype.print_r = function( _row_sep, _spacer, _b_display_key, _level, _node )
{
if ( _row_sep == null ) _row_sep = "\r\n" ;
if ( _spacer == null ) _spacer = " " ;
if ( _b_display_key == null ) _b_display_key = 1 ;
if ( _level == null ) _level = -1 ;
var _output = "" ;
_level++ ;
var _sp = _spacer.repeat(_level), _obj = this ;
if( is_array( _obj ) || typeof( _obj ) == "object" )
{
for( var _p = 0 ; _p < _obj.length ; _p++ )
{
if ( is_array( _obj[_p] ) && this.recursive_run ) _output += _obj[_p].print_r( _row_sep, _spacer, _b_display_key, _level + 1, _node );
else _output += _sp + ( _node != null ? _node : "" ) + _spacer + ( _obj[_p] + "" ) ;
_output += _row_sep ;
}
}
else _output += _sp + ( _obj + "" ) ;
return _output;
}
}
if ( !Array.clone )
{
Array.prototype.clone = function()
{
var _clone = [] ;
for ( var _prop in this )
{
if ( this.hasOwnProperty(_prop) )
{
if ( is_json( this[_prop] ) ) _clone[_prop] = JSONclone( this[_prop] );
else if ( is_array( this[_prop] ) ) _clone[_prop] = this[_prop].clone() ;
else _clone[_prop] = this[_prop] ;
}
}
return _clone;
}
}
if ( !Array.dismember )
{
Array.prototype.dismember = function( _token_size )
{
_token_size = safe_int( _token_size, 0 );
if ( _token_size <= 1 ) return this ;
var _arr = [], _tmp = [], _i = 0 ;
for( var _p = 0 ; _p < this.length ; _p++ )
{
if ( _tmp.length == _token_size )
{
_arr.push( _tmp.clone() );
_tmp = [] ;
}
_tmp.push( this[_p] );
}
if ( _tmp.length > 0 ) _arr.push( _tmp.clone() );
return _arr ;
}
}
if ( !Array.prototype.extract )
{
Array.prototype.extract = function()
{
var _out = [], _arg, _chunk ;
for( var _a = 0 ; _a < arguments.length ; _a++ )
{
_arg = arguments[_a] + "" ;
_arg = _arg.replace( /\;/, "," ) ;
if ( /^\d+\-\d+$/g.test( _arg ) )
{
_chunk = _arg.split( "-" );
_out = _out.concat( this.from_to( safe_int( _chunk[0], 0 ), safe_int( _chunk[1], 0 ) ) ) ;
}
else if ( _arg.includes( "," ) )
{
_chunk = _arg.split( "," );
for( var _i = 0 ; _i < _chunk.length ; _i++ )
_out.push( this[ safe_int( _chunk[_i], 0 ) ] );
}
else if ( /^\d+/.test( _arg ) ) _out.push( this[ safe_int( _arg, 0 ) ] );
}
return _out.clone();
}
}
if ( !Array.set_central )
{
Array.prototype.set_central = function( _c )
{
var _n = this.length, _a = [] ;
if ( _n % 2 == 0 ) return _a.concat( this.slice(0, _n / 2 ), _c, this.slice( -_n / 2 ) ) ; // insert in the middle
else
{
_a = this.clone();
_a[ Math.floor( _n / 2 ) ] = _c ; // replace the middle element ;
return _a ;
}
}
}
if ( !Array.includes )
{
Array.prototype.includes = function()
{
// it works recursively if sub-arrays are included
// return true if ALL input arguments are included inside this array
var _b_found = 0, _input = arguments[0] ;
if ( _input == null ) return 0 ;
var _ref = arguments[1] == null ? this : arguments[1];
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _b_found = _ref[_property].includes( _input, _ref[_property] );
else if ( _ref[_property] === _input ) _b_found = 1 ;
if ( _b_found ) break ;
}
}
return _b_found ;
}
}
if ( !Array.includes_i ) // case insensitive version
{
Array.prototype.includes_i = function( _input )
{
// it works recursively if sub-arrays are included
// return true if ALL input arguments are included inside this array
var _b_found = 0 ;
var _input = arguments[0] ;
if ( _input == null ) return 0 ;
var _ref = arguments[1] == null ? this : arguments[1];
for ( var _property in _ref )
{
if ( _ref.hasOwnProperty( _property ) )
{
if ( is_array( _ref[_property] ) && this.recursive_run ) _b_found = _ref[_property].includes_i( _input, _ref[_property] );
else if ( new String( _ref[_property] ).toLowerCase() === new String( _input ).toLowerCase() ) _b_found = 1 ;
if ( _b_found ) break ;
}
}
return _b_found ;
}
}
if ( !Array.not_includes ) { Array.prototype.not_includes = function() { return !this.includes.apply( this, arguments ) ; } }
if ( !Array.not_includes_i ) { Array.prototype.not_includes_i = function() { return !this.includes_i.apply( this, arguments ) ; } }
if ( !Array.all_in )
{
Array.prototype.all_in = function( _input )
{
// return true if ALL input arguments are included inside this array (case sensitive)
var _b_found = 1, _test_array = ( is_array( _input ) && this.recursive_run ) ? _input : Array.prototype.slice.call(arguments, 0);
for( var _i = 0 ; _i < _test_array.length ; _i++ )
_b_found &= this.includes( _test_array[_i] ) != -1 ? 1 : 0 ;
return _b_found ;
}
}
if ( !Array.one_in )
{
Array.prototype.one_in = function( _input )
{
// return true if AT LEAST ONE of the input arguments are included inside this array (case sensitive)
var _b_found = 0, _test_array = ( is_array( _input ) && this.recursive_run ) ? _input : Array.prototype.slice.call(arguments, 0);
for( var _i = 0 ; _i < _test_array.length ; _i++ )
_b_found |= this.indexOf( _test_array[_i] ) != -1 ? 1 : 0 ;
return _b_found ;
}
}
if ( !Array.one_in_i )
{
Array.prototype.one_in_i = function( _input )
{
// return true if AT LEAST ONE of the input arguments are included inside this array (case insensitive)
var _test_array = ( is_array( _input ) && this.recursive_run ) ? _input : Array.prototype.slice.call(arguments, 0);
var _b_found = 0, _tmp_array = [], _i ;
for( _i = 0 ; _i < this.length ; _i++ ) _tmp_array.push( ( new String( this[_i] ) ).toLowerCase() );
for( _i = 0 ; _i < _test_array.length ; _i++ ) _b_found |= this.indexOf( _test_array[_i].toLowerCase() ) != -1 ? 1 : 0 ;
return _b_found ;
}
}
if ( !Array.subset )
{
Array.prototype.subset = function( _subset_length )
{
_subset_length = safe_int( _subset_length, 0 );
var _n_array = this.length ;
if ( _subset_length == 0 || _n_array == 0 ) return [] ;
else
{
var _n_out = Math.min( Math.abs( _subset_length ), _n_array ) ;
return _subset_length > 0 ? this.slice( 0, _n_out ) : this.slice( _n_array - Math.abs( _n_out ), _n_array );
}
}
}
if ( !Array.compare_to )
{
Array.prototype.compare_to = function( _compare )
{
if ( !is_array( _compare ) ) return 0 ;
else return this.join("").localeCompare( _compare.join("") ) == 0 ? 1 : 0 ;
}
}
if ( !Array.deep_compare_to )
{
Array.prototype.deep_compare_to = function( _compare, _index_match )
{
if ( !is_array( _compare ) ) return 0 ;
else if ( this.length != _compare.length ) return 0 ;
else
{
var _ret = 1 ;
_index_match = safe_int( _index_match, 0 );
if ( _index_match )
{
for( var _i = 0 ; _i < this.length ; _i++ ) _ret &= this[_i].localeCompare( _compare[_i] ) == 0 ? 1 : 0 ;
}
else
{
for( var _i = 0 ; _i < this.length ; _i++ ) _ret &= _compare.includes( this[_i] ) ;
}
return _ret ;
}
}
}
if( !Array.pick_at_random )
{
Array.prototype.pick_at_random = function( _exclude_choices_array )
{
var _v = null ;
if ( _exclude_choices_array == null ) _exclude_choices_array = [];
if ( this.compare_to( _exclude_choices_array ) ) return null ;
while( true )
{
_v = this[ Math.floor( Math.random() * this.length ) ] ;
if ( !_exclude_choices_array.includes( _v ) ) break ;
}
return _v ;
}
}
if ( !Array.swap )
{
Array.prototype.swap = function( i1, i2 )
{
if ( i1 != i2 && i1 >= 0 && i2 >= 0 &&
i1 < this.length && i2 < this.length )
{
var TMP = this[i1] ;
this[i1] = this[i2] ;
this[i2] = TMP ;
}
}
}
if ( !Array.remove )
{
Array.prototype.remove = function()
{
if ( is_array( arguments[0] ) )
{
for( var _a = 0 ; _a < arguments[0].length ; _a++ )
{
var from = this.indexOf( arguments[0][_a] ), to = this.indexOf( arguments[0][_a] ) ;
var rest = this.slice( ( to || from ) + 1 || this.length ) ;
this.length = from < 0 ? this.length + from : from ;
this.push.apply( this, rest ) ;
}
return this ;
}
else // from-index-to-index syntax
{
var from, to ;
if ( arguments.length == 2 )
{
from = safe_int( arguments[0], -1 ), to = safe_int( arguments[1], -1 ) ;
}
else if ( arguments.length == 1 ) from = to = safe_int( arguments[0], -1 ) ;
if ( from != -1 && to != -1 && from <= to )
{
var rest = this.slice( ( to || from ) + 1 || this.length ) ;
this.length = from < 0 ? this.length + from : from ;
return this.push.apply( this, rest ) ;
}
else return this ;
}
}
}
if ( !Array.delete_entry )
{
Array.prototype.delete_entry = function( entry )
{
if ( is_array( entry ) )
{
var _i = -1 ;
for( var _d = 0 ; _d < entry.length ; _d++ )
{
_i = this.indexOf( entry[_d] ) ;
if ( _i != -1 ) this.remove( _i, _i );
}
}
else
{
var _ref = this.indexOf( entry ) ;
if ( is_array( _ref ) )
{
var _cmd = "this" ;
for( _a = 0 ; _a < _ref.length - 1 ; _a++ ) _cmd += "["+_ref[_a]+"]" ;
var _from = _ref.pick_last(), _to = _from ;
_cmd += ".remove( "+_from+", "+_to+" )" ;
try { _ret = eval( _cmd ) ; } catch( err ) {}
}
else if ( _ref != -1 ) this.remove( _ref, _ref );
}
return this ;
}
}
if ( !Array.keep_length )
{
Array.prototype.keep_length = function( length_from, length_to )
{
var _l_from = length_from <= length_to ? length_from : length_to ;
var _l_to = length_from >= length_to ? length_from : length_to ;
var _mark = 0, _i ;
for( _i = 0 ; _i < this.length ; _i++ )
{
if ( this[_i].length < _l_from || this[_i].length > _l_to )
{
this.remove( _i, _i );
_i = _mark - 1 ;
}
else _mark = _i ;
}
}
}
if ( !Array.sort_adv )
{
Array.prototype.sort_adv = function( _reverse )
{
_reverse = safe_int( _reverse, 0 ) ;
return _reverse == 0 ? this.sort() : this.sort().reverse() ;
}
}
if ( !Array.ksort )
{
Array.prototype.ksort = function( _reverse )
{
_reverse = safe_int( _reverse, 0 ) ;
//separate the keys into an array
var keys = [], sortedArray = [];
for(var k in this) { keys.push(k); }
keys.sort();
for( var _i=0; _i < keys.length ; _i++ ) sortedArray[keys[_i]] = this[keys[_i]];
return _reverse == 0 ? sortedArray : sortedArray.reverse() ;
}
}
if ( !Array.asort )
{
Array.prototype.asort = function( _reverse )
{
_reverse = safe_int( _reverse, 0 ) ;
var values = [], sortedArray = [], _i, _v, _key;
for( var _p in this ) { values.push(this[_p]); }
values.sort();
for( _i = 0; _i < values.length ; _i++ )
{
_v = values[_i], _key = "" ;
for( var _k in this )
{
if ( this[_k] == _v )
{
_key = _k ;
break ;
}
}
sortedArray[_key] = _v ;
}
return _reverse == 0 ? sortedArray : sortedArray.reverse() ;
}
}
if ( !Array.unique )
{
Array.prototype.unique = function()
{
var tmp = {}, out = [], _i, _n ;
for( _i = 0, _n = this.length; _i < _n; ++_i ) if(!tmp[this[_i]]) { tmp[this[_i]] = true; out.push(this[_i]); }
return out;
}
}
if ( !Array.get_duplicates )
{
Array.prototype.get_duplicates = function()
{
var _dups = [], _i = 0, _c ;
while ( _i < this.length )
{
_c = this.indexOf( this[_i], _i+1 ) ;
if ( _c != _i && _c != -1 ) _dups.push( this[_c] ) ;
_i++ ;
}
return _dups.unique() ;
}
}
if ( !Array.get_duplicates_index )
{
Array.prototype.get_duplicates_index = function()
{
var _dups = [], _i = 0, _c ;
while ( _i < this.length )
{
_c = this.indexOf( this[_i], _i+1 ) ;
if ( _c != _i && _c != -1 ) _dups.push( _c ) ;
_i++ ;
}
return _dups ;
}
}
if ( !Array.prototype.sum ) // float-version
{
Array.prototype.sum = function()
{
var _sum = 0 ;
for( var _i = 0 ; _i < this.length ; _i++ ) _sum += !isNaN( this[_i] ) ? safe_float( this[_i], 0 ) : 0 ;
return _sum ;
}
}
if ( !Array.prototype.permute ) // yields an multi-dimensional array including all permutations of the original one
{
Array.prototype.permute = function()
{
// refer to http://www.quickperm.org/
var _objects_list = this.clone();
var _n = _objects_list.length, _results = [] ;
var _p = [], _i = 0, _j = 0, _tmp ;
for( _i = 0 ; _i <= _n ; _i++ ) _p.push( 0 );
_i = 1 ;
_results.push( _objects_list.clone() );
while( _i < _n )
{
if ( _p[_i] < _i )
{
_j = _i % 2 == 1 ? _p[_i] : 0 ;
_tmp = _objects_list[_j];
_objects_list[_j] = _objects_list[_i];
_objects_list[_i] = _tmp ;
_results.push( _objects_list.clone() );
_p[_i] += 1 ;
_i = 1 ;
}
else
{
_p[_i] = 0 ;
_i++ ;
}
}
return _results ;
}
}
if ( !Array.subtract )
{
Array.prototype.subtract = function( _arr, _sub )
{
if ( is_array( _arr ) && is_array( _sub ) )
{
var _both_standard = !this.is_associative() && !_arr.is_associative() ? 1 : 0 ;
if ( _both_standard )
{
if ( this.length > _arr.length )
{
_sub = [] ;
var _a, _b, _found = 0 ;
for( _a = 0 ; _a < this.length ; _a++ )
{
_found = 0 ;