-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZprototypes_only - Copy [2].js
5049 lines (4413 loc) · 171 KB
/
EZprototypes_only - Copy [2].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
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
/*global EZ, g, dw, DWfile */
var e;
(function jshint_globals_not_used() { //list global variables and/or functions defined but not used
e = [e, g, dw, DWfile]
})
EZ.prototypeFunctions = EZ.prototypeFunctions || {"String":[], "Number":[], "Boolean":[]};
/*--------------------------------------------------------------------------------------------------
EZ.createPrototype(fn)
Create non-enumerable prototype function using Object.defineProperty if available or __proto__ for
dw enviornment, otherwise create with prototype keyword if EZ.isPrototypeFunctionAllowed !== false.
ARGUMENTS:
obj specifies Object (e.g. Array or Object)
fn specifies named function -- cannot be anouymous
REFFERENCE:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
https://github.com/es-shims/es5-shim
TODO:
if (!dw.isNotDW)
else if isPrototypeAllowed return false
// var fnName = name + 'EZprototype' + Object.prototype.toString.call(obj).substr(8);
// var fnName = obj.prototype[name] = name + 'EZprototype'
// + Object.prototype.toString.call(obj).substr(8)
--------------------------------------------------------------------------------------------------*/
EZ.createPrototype = function EZcreatePrototype(fn)
{
if (typeof(fn) != 'function' || !fn.name) return false;
try
{
var results = fn.name.match(/(.*)EZprototype(.*)/i);
if (!results) return false;
var name = results[1];
var objName = results[2];
var obj = eval(objName);
if (obj.prototype[name]) return true;
if (EZ.prototypeFunctions[objName])
EZ.prototypeFunctions[objName].push(name)
if (Object.defineProperty)
Object.defineProperty(obj.prototype, name, {value: fn})
/*TODO:
else if (!dw.isNotDW) //use __proto__ if dw supports for non-enumerable prototype
{
}
else if (EZ.isPrototypeFunctionAllowed !== false)
return false
*/
else
obj.prototype[name] = fn;
return true;
}
catch (e)
{
return EZ.techSupport(e);
}
}
/* _______________________________________________________________________________
| |
|Remainder of file contains prototpes and related varients in alphabetical order |
|________________________________________________________________________________|
*/
/*---------------------------------------------------------------------------------------------
Add ordinal suffix to any number: e.g. 1st, 2nd, 3rd, 4th ...
http:https://stackoverflow.com/questions/13627308/add-st-nd-rd-and-th-ordinal-suffix-to-a-number
---------------------------------------------------------------------------------------------*/
if (EZ.createPrototype(function suffixEZprototypeNumber()
{
var n=this.toString().split('.')[0];
var lastDigits=n.substring(n.length-2);
if (lastDigits==='11' || lastDigits==='12' || lastDigits==='13')
return this+'th'; //add exception just for 11, 12 and 13
switch (n.substring(n.length-1))
{
case '1': return this+'st';
case '2': return this+'nd';
case '3': return this+'rd';
default : return this+'th';
}
}))
//_______________________________________________________________________________
Number.prototype.suffix.test = function()
{
var number = 1234;
EZ.test.run(number, {EZ: {ex:'1234th'}})
}
/*---------------------------------------------------------------------------------------------
Clip end of string by specified number of char (default: 1)
---------------------------------------------------------------------------------------------*/
if (EZ.createPrototype(function clipEZprototypeString(nochar)
{
if (nochar == null) nochar = 1;
if (!nochar)
return this + '';
else if (nochar >= this.length)
return '';
else if (nochar < 0)
return this.substr(-nochar);
else
return this.substr(0,this.length-nochar);
}))
//_______________________________________________________________________________
String.prototype.clip.test = function()
{
var str = 'abc';
EZ.test.run(str, {EZ: {ex:'ab', ctx:str}})
EZ.test.run(str, 0, {EZ: {ex:'abc', ctx:str}})
EZ.test.run(str, 2, {EZ: {ex:'a', ctx:str}})
EZ.test.run(str, 3, {EZ: {ex:'', ctx:str}})
EZ.test.run(str, 4, {EZ: {ex:'', ctx:str}})
EZ.test.run(str, -1, {EZ: {ex:'bc', ctx:str}})
}
/*--------------------------------------------------------------------------------------------------
Number.pad(size, ch)
String.pad(size, ch)
pads AND truncates Number or String to specified size. If no argument is specified or size is zero,
return String without padding or truncating.
ARGUMENTS:
size (Number) specifies padding size -- >0 for right padding <0 for left padding
(String), specifies character(s) used for padding -- size is length of size String.
right padding if numeric e.g. "0..." -- otherwise left padding
ch (optional) specifies padding character(s) when size is Number.
default is blank character for String or Number when size is > 0
default is "0" for Number when size < 0
EXAMPLES:
'1'.pad('00') returns: "01"
1.pad('00')
'1'.pad(-2)
1.pad(-2)
1.pad(2) returns: " 1"
1.pad('...') "..1"
"abc".pad(5) returns: "abc "
"abc".pad(-5) returns: " abc"
"abc".pad('..') returns: "ab"
"abc".pad(-2,'.') returns: "bc"
"abc".pad('...') returns: "abc"
"abc".pad('....') returns: "abc."
"abc".pad(-5,'.') returns: "..abc"
RETURNS:
padded / truncated String
--------------------------------------------------------------------------------------------------*/
String.prototype.pad = function pad_EZ_String(size, ch)
{
if (size == null)
return this + '';
switch (typeof size)
{
case 'number':
{
ch = (ch !== undefined) ? ch + ''
: (size < 0 && this instanceof Number) ? '0'
: ' ';
break;
}
case 'string':
{
ch = size + '';
size = ch.length;
//if (!isNaN(Number(ch)) && typeof(this) == 'number')
//if (!isNaN(Number(ch)) && !isNaN(Number(this)))
if (!isNaN(Number(this)))
size = -size;
break;
}
default:
{
return this;
}
}
if (size < 0)
return String(ch.dup(-size) + this).slice(size);
else if (size > 0)
return String(this + ch.dup(size)).slice(0, size);
else
return this + '';
}
//________________________________________________________________________________________
/**
*
*/
String.prototype.pad.test = function()
{
var msg, arr, ctx, arg, args, o, obj, note='', ex, exfn, notefn, fn, val, rtnValue;
/* jshint: avoid unused variable error */
e = [msg, arr, ctx, arg, args, o, obj, note='', ex, exfn, notefn, fn, val, , rtnValue];
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
exfn = function(results, expected, testrun)
{
void( [results, expected, testrun] ) //jshint
testrun.exfnDone = true; //don't call as legacy
//if (typeof(testrun.argsClone[0]) == 'number')
if (results != null)
testrun.argsClone[0] = 'length: ' + results.length;
//else if (typeof(testrun.argsClone[1] == 'number'))
// testrun.argsClone[1] = (results.length)
}
//_______________________________________________________________________________________
EZ.test.settings( {exfn:exfn} )
ctx = 'abcdefg'
EZ.test.run('abc', 10)
EZ.test.run('xyz', -10)
EZ.test.run('abc', 5, '0')
EZ.test.run('abc', -5, '0')
EZ.test.run('1', '000')
EZ.test.run('1', '...')
EZ.test.run('1', 2, '0')
EZ.test.run('12', 2, '0')
EZ.test.run('123', 2, '0')
EZ.test.run('1', -2, '0')
EZ.test.run('12', -2, '0')
EZ.test.run('123', -2, '0')
EZ.test.run('123', 2, '0')
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
EZ.test.run(1, 2)
EZ.test.run(1, -2)
EZ.test.run(12, 2)
EZ.test.run(12, 3)
EZ.test.run(12, 0)
EZ.test.run(12, '0')
EZ.test.run(12, '00')
EZ.test.run(12, '000')
EZ.test.run(123, '000')
EZ.test.run(1234, '000')
EZ.test.run(1, '00')
EZ.test.run(1, '...')
EZ.test.run(1, 3, '...')
EZ.test.run(1, -3, '...')
if (true) return;
}
/*--------------------------------------------------------------------------------------------------
truncate String if more than specified size
--------------------------------------------------------------------------------------------------*/
String.prototype.truncate = function truncate_EZ_String(size, more)
{
var str = this.toString();
size = EZ.toInt(size);
more = (more === '' || more) ? more : EZ.MORE;
if (size && str.length > size)
str = str.substr(0,size).trim() + more;
return str;
}
/*---------------------------------------------------------------------------------------------
EZ.merge([options,] obj, object(s)...)
Merge all properties from each in newObj into 1st object replacing existing properties and
appending new ones. if 1st Object is undefined or null, clone as next non-null, non-empty
Object that follows or new Object if none follow.
When any property is an object type, it is cloned upto the maxdepth option described below.
ARGUMENTS:
this Object merged with properties from all the folowing objects
use empty object to merge all objects into new object
options (optional) 1st argument if Object containing mergeOptions property which is Object
containing any of the following options:
maxdepth Object properties at this level are not cloned (top level is 0)
they are copied as a reference to the original (default: 9)
exclude Array or comma delimited String containing Property names excluded.
objects all arguments following options specify one or more objects to merge
RETURNS:
new Object containing Object with merged properties from all other specified Objects.
//if (!fromObj || fromObj == null || typeof(fromObj) != 'object') return;
--------------------------------------------------------------------------------------------------*/
EZ.mergeObj = function EZmergeObj(options, obj)
{
obj = obj; //jshint: document var -- code uses arguments
var newObj;
var args = [].slice.call(arguments);
//----- options is 1st arg if object with mergeOptions property
options = '.mergeOptions'.ov(options) ? (args.shift().mergeOptions).clone() : {};
var mergeOptions = {
depth: 0,
maxdepth: '.maxdepth'.ov(options, 9),
append: EZ.toArray('.append'.ov(options, [])),
exclude: EZ.toArray('.exclude'.ov(options, []))
}
if (mergeOptions.append.indexOf('*all') != -1)
mergeOptions.append = '*all';
// Create new Object using constructor of 1st Object
while (true)
{
if (!args.length) return {};
newObj = args.shift();
if (!(newObj instanceof Object)) continue;
newObj = newObj.clone(options);
break;
}
// remaining args are objects merged into newObj
args.forEach(function EZmergeObjects(fromObj)
{
Object.keys(fromObj).forEach(function(p)
{
if (mergeOptions.exclude.indexOf(p) != -1) return;
if (p in newObj
&& (mergeOptions.append == '*all' || mergeOptions.append.indexOf(p) != -1))
return;
if (fromObj[p] instanceof Object)
{
newObj[p] = fromObj[p].clone(mergeOptions.maxdepth);
if (EZ.isArray(fromObj[p]) && newObj[p].length == EZ.undefined)
newObj[p].length = fromObj[p].length;
}
else
newObj[p] = fromObj[p];
});
if (EZ.isArray(fromObj) && newObj.length == EZ.undefined)
{
newObj.length = fromObj.length;
}
});
return newObj;
}
/*--------------------------------------------------------------------------------------------------
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
if (EZ.createPrototype(function mergeEZprototypeObject(options)
{
var args = [].slice.call(arguments);
var idx = (options instanceof Object && options.mergeOptions != EZ.undefined) ? 1 : 0;
args.splice(idx,0,this);
return EZ.mergeObj.apply(EZ.mergeObj, args);
}))
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object.prototype.merge.test = function()
{
var ex, ctx, note, obj;
// #1
var objectLike = [1,2]
objectLike.x = 24
objectLike.y = 25
obj = {a:1, b:2}
ex = {
0: 1, 1: 2, length: 2,
a: 1, b: 2,
x: 24, y: 25
};
EZ.test.run(obj,objectLike, {EZ:{ex:ex, ctx:obj, note:note}})
EZ.test.run(obj,{mergeOptions:{exclude:'x'}},objectLike,
{EZ:{ex:ex, ctx:obj, note:note}})
var fn = function(arg) {return arg};
fn.z = 66;
EZ.test.run(fn, obj,objectLike, {EZ:{ex:ex, ctx:fn, note:note}})
//____________________________________________________________________________
// #2
obj = {array: objectLike}
ex = obj
EZ.test.run(obj, {EZ:{ex:ex, ctx:ctx, note:note}})
//____________________________________________________________________________
obj = {a:1, b:2};
ex = {b:2, a:11, c:22}
EZ.test.run(obj, {a:11, c:22}, {EZ:{ex:ex, ctx:ctx, note:note}})
//______________________________________________________________________________
return;
var div = document.createElement('div');
var span = document.createElement('span');
var divObj = {d:div}
var spanObj = {d:span}
EZ.test.run({},divObj,spanObj, {EZ: {ex:{d:div, s:span}, note:'' }})
EZ.test.results.push( EZ.merge() );
EZ.test.results.push( EZ.merge({}) );
EZ.test.results.push( EZ.merge({}, {a:11, b:22, c:33}) );
EZ.test.showResults();
}
--------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------
TODO: mayby use toArray()
--------------------------------------------------------------------------------------------------*/
String.prototype.collapse = function collapseEZprototypeString()
{
return this.toString();
}
/*--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------*/
if (EZ.createPrototype(function collapseEZprototypeArray(str)
{
var o;
if (!length) return '';
if (str) return o.join(str+'');
return o[0];
}))
//_______________________________________________________________________________
Array.prototype.collapse.test = function()
{
}
/*--------------------------------------------------------------------------------------------------
Object.compare()
Not refactored as prototype
Never tested -- Used EZ.stringify()
--------------------------------------------------------------------------------------------------*/
/*
if (EZ.createPrototype(function compareEZprototypeObject(obj, dotName, options)
{
var topLevel = false;
if (!options)
{
topLevel = true;
dotName = dotName || ''
options = {
maxItems: Math.min(maxItems || 11, 100),
log: {diff:[], same:[], skip:[]},
processed: [] //only process each object once
};
}
// if either a or b is not Object ...
if (a === null || typeof a !== 'object'
|| b === null || typeof b !== 'object')
{
if (a === b)
options.log.same.push([dotName,a,b]);
else if (options.skip.include(dotName))
options.log.skip.push([dotName,a,b]);
else
options.log.diff.push([dotName,a,b]);
}
else if (options.processed.indexOf(a) != -1 || options.processed.indexOf(b) != -1)
{
return;
}
else
{
// get list of unique keys from a and/or b
var keys = Object.getOwnPropertyNames(a);
Object.getOwnPropertyNames(b).forEach(function(key)
{
if (keys.indexOf(key) == -1) keys.push(key);
});
// for all keys
keys.every(function(key)
{
var dotNamePlus = dotName + isNaN(key) ? '.' + key : '[' + key + ']';
if (!(key in a))
options.log.diff.push([dotNamePlus,undefined,b[key]]);
else if (!(key in b))
options.log.diff.push([dotNamePlus,a[key],undefined]);
compareObject(a[key], b[key], dotNamePlus, options);
if (options.diff.count > options.maxItems)
return false;
});
}
return options;
}))
//_____________________________________________________________________________________________
Object.prototype.compare.test = function()
{
var a = [1,2,3];
EZ.test.run(arg0, {EZ: {ex:'', note:'' }})
}
*/
/*--------------------------------------------------------------------------------------------------
String.count(char[, [start,] end])
Count number of char found in String -- from start if specified -- up to end if specified
ARGUMENTS:
2nd argument is "end" when function called with only 2 arguments
char (required) character to count -- use number to specify unicode
start (optional) position in String to start count (default: 0)
default used when not specified (< 3 arguments), NaN, blank or < 0
end (optional) position in String to stop counting (default: String.length)
default used when not specified, NaN, blank or <= 0
RETURNS:
count of char in String from start to end
--------------------------------------------------------------------------------------------------*/
String.prototype.count = function countEZprototypeString(char,start,end)
{
switch(arguments.length)
{
case 0: return 0; //or insert undefined for 2nd argument\\
case 1: Array.prototype.splice.call(arguments,1,0,undefined)
}
var str = this.toString();
char = (typeof(char) == 'number') ? String.fromCharCode(char) : char;
start = (!isNaN(start) && start > 0) ? start : 0;
end = (!isNaN(end) && end > 0) ? end : str.length;
try
{
var regex = new RegExp('\\' + char, 'g')
var results = str.substr(start,end).match(regex);
return results ? results.length
: str.length ? 1 : 0;
}
catch (e)
{
EZ.oops(e);
return 0;
}
}
/*--------------------------------------------------------------------------------------------------
String.dup(times[,maxlength])
return string duplicated specified number of times.
optional maxlength argument specified maximun length of returned string.
--------------------------------------------------------------------------------------------------*/
String.prototype.dup = function dupEZprototypeString(times,maxlength)
{
times = parseInt(times) || 80;
if (times <= 0) return '';
var str = ''
for (var i=0;i<times;i++) str += this;
if (maxlength && !isNaN(maxlength) && maxlength > 0)
str = str.substr(0,maxlength)
return str;
}
/*--------------------------------------------------------------------------------------------------
String.equals(str)
return true if str not null or undefined and equal to String otherwise return false.
--------------------------------------------------------------------------------------------------*/
String.prototype.equals = function equalsEZprototypeString(str)
{
if (str == null || str === undefined)
return false;
else
return this === str;
}
/*--------------------------------------------------------------------------------------------------
String.equalsIgnoreCase(str)
return true if str not null or undefined and case insenitive equal to String otherwise false.
--------------------------------------------------------------------------------------------------*/
String.prototype.equalsIgnoreCase = function equalsEZprototypeString(str)
{
if (str == null || str === undefined)
return false;
else
return this.toLowerCase() === str.toLowerCase();
}
/*--------------------------------------------------------------------------------------------------
Array.fill(value [, start = 0 [, end = this.length]]) -- ECMAScript 2015 (ES6) standard.
Fill all the elements of Array from start index to end index with specified value.
Changes this Array besides returning filled Array. Does not increase Array length.
Provides easy way to initialize Array to values other than undefined as follows:
new Array(5).fill(0) creates Array with 5 item set to 0.
For objects, call as follows: obj = [].fill.call({length:5}, 0)
ARGUMENTS:
value (required) specifies fill value
start (optional) start index (default: 0)
end (optional) end index (default: Array length)
RETURNS:
filled Array -- throws error if this is null ([].fill.call(object, ...))
REFEENCE:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
--------------------------------------------------------------------------------------------------*/
/* jshint ignore:start*/
if (EZ.createPrototype(function fillEZprototypeArray(value, start, end)
{
// Steps 1-2.
if (this == null)
throw new TypeError('fill called with null or not defined');
var obj = Object(this); //clone
// Steps 3-5.
var len = obj.length >>> 0; //returns 0 for undefined
// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;
// Step 8.
var k = relativeStart < 0
? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Steps 9-10.
var end = arguments[2];
var relativeEnd = end == EZ.undefined
? len
: end >> 0;
// Step 11.
var final = relativeEnd < 0
? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Step 12.
while (k < final)
{
obj[k] = value;
k++;
}
// Step 13.
return obj;
}))
/* jshint ignore:end */
//_______________________________________________________________________________
Array.prototype.fill.test = function()
{
EZ.test.run();
EZ.test.run(new Array(2), 1);
}
/*--------------------------------------------------------------------------------------------------
Array.find(callback[, thisObj])
Calls the specified callback function for each item of Array until callback returns true then
returns the VALUE of that Array item or undefined if callback never returns true.
Very useful for getting specific object from an Array of Object:
// get first div with class containing of 'hidden'
var el = [].find.call( document.getElementsByTagName('div'), function(tag)
{
return tag.className && className.indexOf('hidden') != -1;
});
Of course there are alternatives for above example, but often not for Array of custom Objects.
ARGUMENTS:
callback (required) function which returns true if Array item meets selection critera
thisObj (optional) value of this keyword in the callback function
RETURNS:
value of 1st Array item meeting selection critera of callback function
-OR- undefined if none of the Array items meet the callback selection critera.
REFEENCE:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
--------------------------------------------------------------------------------------------------*/
if (EZ.createPrototype(function findEZprototypeArray(callback, thisObj)
{
thisObj = thisObj || this; //TODO: leave as undefined ??
var name = arguments.callee.name;
if (this === null)
throw new TypeError(name + ' called on null or undefined Object');
if (typeof callback !== 'function')
throw new TypeError(name + ' callback must be a function');
var list = Object(this);
/* jshint ignore:start*/
var length = list.length >>> 0; //return 0 if length undefined
/* jshint ignore:end */
var value = EZ.undefined;
for (var i = 0; i < length; i++) //for all Array items . . .
{
var value = list[i];
if (callback.call(thisObj, value, i, list))
return value;
}
return undefined;
}))
//_______________________________________________________________________________
Array.prototype.find.test = function()
{
}
/*--------------------------------------------------------------------------------------------------
Array.findIndex(callback[, thisObj])
Calls the specified callback function for each item of Array until callback returns true then
returns the INDEX of that Array item or -1 if callback never returns true.
ARGUMENTS:
callback (required) function which returnS true if Array item meets selection critera
thisObj (optional) value of this keyword in the callback function
RETURNS:
index of 1st Array item meeting selection critera of callback function
-OR- -1 if none of the Array items meet the callback selection critera.
--------------------------------------------------------------------------------------------------*/
if (EZ.createPrototype(function findIndexEZprototypeArray(callback)
{
var thisObj = thisObj || this; //TODO: leave as undefined ??
var name = arguments.callee.name;
if (this === null)
throw new TypeError(name + ' called on null or undefined Object');
if (typeof callback !== 'function')
throw new TypeError(name + ' callback must be a function');
var list = Object(this);
/* jshint ignore:start*/
var length = list.length >>> 0;
/* jshint ignore:end */
var value = EZ.undefined;
for (var i = 0; i < length; i++)
{
value = list[i];
if (callback.call(thisObj, value, i, list)) {
return i;
}
return -1;
}
}))
//_______________________________________________________________________________
Array.prototype.findIndex.test = function()
{
}
/*--------------------------------------------------------------------------------------------------
Array.forEach(callback, thisObject)
Implements forEach and associated variants for older browsers -- calls the callback function
for each array elenemt with the following arguments: element, index, array
Variants:
every(): quits loop and returns false when callback function returns false;
i.e. must return true to keep looping
otherwise returns true after every item processed
some(): quits loop and returns true when callback function returns true;
otherwise returns false if all items return false
i.e. acts like includes() -- quits on 1st success
ARGUMENTS:
callback function called for each defined array element
thisObject Object used as this when executing callback function
RETURNS:
nothing
TODO:
filter (creates a new array including elements where the filter function returns true
and omitting the ones where it returns false)
map (creates a new array from the values returned by the iterator function)
reduce (builds up a value by repeated calling the iterator, passing in previous values;
see the spec for the details; useful for summing the contents of an array and many other things)
reduceRight (like reduce, but works in descending rather than ascending order)
REFERENCE:
http:https://www.tutorialspoint.com/javascript/array_foreach.htm
http:https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript
http:https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.16 (forEvery #hash)
--------------------------------------------------------------------------------------------------*/
if (EZ.createPrototype(function EZforEachEZprototypeArray(callback, thisObj)
{
if (typeof callback != "function")
throw new TypeError();
var callerName = arguments.callee.caller.displayName || '';
var o = thisObj || this;
var l = o.len || 1; //range set before 1st call
for (var i = 0; i < l; i++)
{
if (!(i in o)) continue; //element no longer exists
if (!callback.call(o, o[i], i, o))
{
if (callerName.indexOf('every') != -1) return false;
}
else
{
if (callerName.indexOf('some')) return false;
}
}
return (callerName.indexOf('every') != -1) ? true
: (callerName.indexOf('some') != -1) ? false
: undefined;
}))
//_______________________________________________________________________________
/**
* initially added for testing EZforEach() in browsers with native forEach()
*/
Array.prototype.EZforEach.test = function()
{
}
//_______________________________________________________________________________
/**
*
*/
EZ.createPrototype(function forEachEZprototypeArray(callback, thisObj)
{
return this.EZforEach(callback, thisObj);
})
//_______________________________________________________________________________
/**
*
*/
EZ.createPrototype(function everyEZprototypeArray(callback, thisObj)
{
return this.EZforEach(callback, thisObj);
})
//_______________________________________________________________________________
/**
*
*/
EZ.createPrototype(function someEZprototypeArray(callback, thisObj)
{
return this.EZforEach(callback, thisObj);
})
/*--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------*/
String.prototype.forEach = function(fn)
{
var str = this;
EZ.toArray(str).forEach(fn)
}
//_________________________________________________________________________________________________
e = function _____FORMAT_FUNCTIONS_____() {} //convenience for DW functions list
//_________________________________________________________________________________________________
/*--------------------------------------------------------------------------------------------------
String.prototype.fromRanges(highest) created: 06-17-2016
returns Array of numbers or letters from string of ranges: e.g. -2, 4-8,11, 14-
cloned and updated code originally created for: EZ.test_assistant onlyList.
Array.fromRanges(): creates String of ranges from Array of numbers.
ARGUMENTS:
highest (optional) highest number allowed (+/-) if >0 return all numbers for enpty string
if omitted range of form: "14-" interpreted as "14" only
RETURNS:
Array of sorted unique numbers
TODO:
letter ranges
--------------------------------------------------------------------------------------------------*/
String.prototype.fromRanges = function fromRanges_String_EZprototype(highest)
{
highest = parseInt(highest);
var str = this.trim();
var list = [];
if (str === '')
{
if (!isNaN(highest)) //all numbers
for (var i=0; i<highest; i++)
list[i] = i + 1;
return list;
}
EZ.toArray(str, ', ').forEach(function(range)
{
var n = range.split('-');
n[0] = parseInt(n[0]);
switch (n.length)
{
case 1:
{
if (range.startsWith('-'))
n[0] = 1; //e.g -12
else if (range.endsWith('-'))
n[1] = Math.abs(highest || n[0]);
else
n[1] = n[0];
break;
}
case 2:
{
n[1] = parseInt(n[1]);
if (n[1] < n[0]) return;
break;
}
default: return
}
for (var i=n[0]; i<= n[1]; i++)
list.push(EZ.toInt(i));
});
return list.removeDups().sortPlus();
}
//________________________________________________________________________________________
String.prototype.fromRanges.test = function()
{
var msg, arr, ctx, arg, args, o, obj, note='', ex, exfn, notefn, fn, val, rtnValue;
/* jshint: avoid unused variable error */
e = [msg, arr, ctx, arg, args, o, obj, note='', ex, exfn, notefn, fn, val, , rtnValue];
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
//EZ.test.skip(999) //count to skip
//EZ.test.settings({group: 'persistant note'});
//______________________________________________________________________________________
arg = '-2, 4-8,11, 14-'
EZ.test.run(arg, {EZ: {ex:ex, note:note }})
EZ.test.run(arg, 10, {EZ: {ex:ex, note:note }})
EZ.test.run('', 5, {EZ: {ex:ex, note:note }})
arg = '4-8, 11-10, -2, 14-'
EZ.test.run(arg, 15, {EZ: {ex:ex, note:note }})
EZ.test.run('1', 3, {EZ: {ex:[1], note:note }})
}
/*--------------------------------------------------------------------------------------------------
Array.toRanges(options)
Extracted from: Array.prototype.format() -- bad naming convention.
Originally created for EZ.testAssistant::loadTestResults()
String.toRanges(): creates Array of numbers.
If Array contains numbers, format range of numbers: 1-2, 5, 7-9
If Array contains String . . . TODO:
1st Array element determines above type.
ARGUMENTS:
this (Array)
options (optional) Object containing one or more of the following properties:
how: 'seq' (default) ???
RETURNS:
Array of Strings: rnumber ranges -or- aligned strings.
--------------------------------------------------------------------------------------------------*/
Array.prototype.toRanges = function toRanges_Array_EZprototype(options)
{
if (!this.length) return '';
var isNumber = !isNaN(this[0]);
if (isNumber)
{
var str = '';
options = options || {how:'sequence'}
switch (options.how)
{
case 'sequence':
{
var first = '', last = '', numbers = this.slice();
numbers.push(99999);
while (true)
{
var next = numbers.shift();
if ((next-1) === last)