-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCP.js
1159 lines (1073 loc) · 31.6 KB
/
CP.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
/**
*
* @projectDescription (Certain Point) Lightweight Functional Style JavaScript Framework Providing Commonly Used Set Of Functions
*
* @author Tornike Nanobashvili
*
* @copyright Copyright (c) 2014
*
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*
* @example Any function within the framework can be invoked like CP.*functionName*
*
*/
;(function() {
"use strict";
var scope = this || window; // defining scope
var message = { /** @private, Messages list */
'inv_args' : 'Invalid Argument(s) Supplied !',
'inv_base' : 'Function Doesn\'t Support Convertation On Specified Base !',
'success' : '"Certain Point" Has Been Successfully Initialized !'
};
function log( msgs, flag ) { /** @private, Function for messages logging */
flag = ( typeof flag === 'undefined' || flag === true ) ? true : false;
for ( var i = 0; i < msgs.length; i++ ) {
console.log( msgs[i] );
if ( flag ) {
alert( msgs[i] );
}
}
}
/**
* @classDescription This class creates a new CP.
* @constructor CP
*/
function CP() {
var _scope = this;
/**
* This function defends public functions from wrong arguments
* @private
* @return {Boolean}
*/
function defend( args, types ) {
if ( args.length === 0 ) {
return false;
}
for ( var i = 0; i < args.length; i++ ) {
if ( _scope.type(args[i]) != types[i] ) {
return false;
}
}
return true;
}
/**
* This function creates an array of types for argument defending purposes
* @private
* @return {Array}
*/
function createTypes( args, str ) {
var ret = [];
for ( var i = 0; i < args.length; i++ ) {
ret.push(str);
}
return ret;
}
// ------------------------------------------------------------------------ Collection functions ------------------------------------------------------------------------
/**
* Function returns type of the specified argument
* @param {arg} Any type of argument is applicable
* @return {String}
*/
this.type = function( arg ) {
return /^.\w+\s(\w+).$/.exec(Object.prototype.toString.call(arg))[1].toLowerCase();
};
/**
* Function checks if specified argument's type is a number
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isNumber = function( arg ) {
return ( this.type(arg) === 'number' );
};
/**
* Function checks if specified argument's type is a function
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isFunction = function( arg ) {
return ( this.type(arg) === 'function' );
};
/**
* Function checks if specified argument's type is a date
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isDate = function( arg ) {
return ( this.type(arg) === 'date' );
};
/**
* Function checks if specified argument's type is an arguments
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isArguments = function ( arg ) {
return ( this.type(arg) === 'arguments' );
};
/**
* Function checks if specified argument's type is an object
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isObject = function ( arg ) {
return ( this.type(arg) === 'object' );
};
/**
* Function checks if specified argument's type is an array
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isArray = function( arg ) {
return ( this.type(arg) === 'array' );
};
/**
* Function checks if specified argument's type is a regexp
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isRegExp = function( arg ) {
return ( this.type(arg) === 'regexp' );
};
/**
* Function checks if specified argument's type is a boolean
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isBoolean = function( arg ) {
return ( this.type(arg) === 'boolean' );
};
/**
* Function checks if specified argument's type is a null
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isNull = function( arg ) {
return ( this.type(arg) === 'null' );
};
/**
* Function checks if specified argument's type is an undefined
* @param {arg} Any type of argument is applicable
* @return {Boolean}
*/
this.isUndefined = function( arg ) {
return ( this.type(arg) === 'undefined' );
};
/**
* Function checks if specified array is an empty
* @param {arg} Array is applicable
* @return {Boolean}
*/
this.isEmpty = function ( arg ) {
if ( !defend(arguments,['array']) ) {
return log([message.inv_args]);
}
return ( arg.length == 0 );
};
/**
* Function iterates through the specified object elements and returns the function with following parameters : (context CP), value, index, object itself
* @param {obj} Array and Object are applicable
* @param {fn} User defined function
* @return {Function} Returns an user defined function
*/
this.each = function( obj, fn ) {
if ( this.type(obj) === 'object' || this.type(obj) === 'array' ) {
for ( var i in obj ) {
if ( obj.hasOwnProperty(i) ) {
fn.call(this || null, obj[i], i, obj);
}
}
} else {
return log([message.inv_args]);
}
};
/**
* Function iterates through the specified object elements and returns the function with following parameters : (context CP), previous value, current value, index, next value, object itself
* @param {obj} Array and Object are applicable
* @param {fn} User defined function
* @return {Function} Returns an user defined function
*/
this.eachSide = function( obj, fn ) {
if ( this.type(obj) === 'object' || this.type(obj) === 'array' ) {
var prev = obj[0], next = obj[1];
for ( var i in obj ) {
if ( obj.hasOwnProperty(i) ) {
if ( i != 0 ) {
prev = obj[parseInt(i) - 1];
}
if ( typeof obj[parseInt(i) + 1] !== 'undefined' ) {
next = obj[parseInt(i) + 1];
}
fn.call(this || null, prev, obj[i], i, next, obj);
}
}
} else {
return log([message.inv_args]);
}
};
/**
* Function iterates through the specified object elements and returns the new array based on user defined function
* @param {obj} Array and Object are applicable
* @param {fn} User defined function
* @return {Array} Returns a new array
*/
this.map = function( obj, fn ) {
if ( this.type(obj) === 'object' || this.type(obj) === 'array' ) {
var ret = [];
for ( var i in obj ) {
if ( obj.hasOwnProperty(i) ) {
ret.push(fn.call(this || null, obj[i], i, obj));
}
}
return ret;
}
return log([message.inv_args]);
};
/**
* Function iterates through the specified object elements and returns the new array of elements where user defined function returns true (Boolean)
* @param {obj} Array and Object are applicable
* @param {fn} User defined function
* @return {Array} Returns a new array
*/
this.filter = function( obj, fn ) {
if ( this.type(obj) === 'object' || this.type(obj) === 'array' ) {
var ret = [];
for ( var i in obj ) {
if ( obj.hasOwnProperty(i) ) {
if ( fn.call(this || null, obj[i], i, obj) ) {
ret.push(obj[i]);
}
}
}
return ret;
}
return log([message.inv_args]);
};
/**
* Function returns a new object where the keys will become the values and the values the keys
* @param {obj} Object is applicable
* @return {Array} Returns a new inverted array
*/
this.invert = function( obj ) {
if ( !defend(arguments,['object']) ) {
return log([message.inv_args]);
}
var ret = {};
this.each(obj, function(v,i) {
ret[v] = i;
});
return ret;
};
/**
* Function returns (Boolean) based on specified two objects are equal
* @param {objA} Object is applicable
* @param {objB} Object is applicable
* @return {Boolean} Returns a new inverted array
*/
this.isEqual = function( objA, objB ) {
if ( !defend(arguments,['object','object']) ) {
return log([message.inv_args]);
}
var temp = this.invert(objB);
for ( var i in objA ) {
if ( objA.hasOwnProperty(i) ) {
if ( i !== temp[objA[i]] ) {
return false;
}
}
}
return true;
};
/**
* Function returns clone of the specified list
* @param {obj} Object is applicable
* @return {Object} Returns a clone of the object
*/
this.clone = function( obj ) {
if ( !defend(arguments,['object']) ) {
return log([message.inv_args]);
}
var ret = {};
for ( var i in obj ) {
if ( this.isObject(obj[i]) && !this.isNull(obj[i]) ) {
ret[i] = this.clone(obj[i]);
} else {
ret[i] = obj[i];
}
}
return ret;
};
/**
* Function returns the value from the object based on index
* @param {obj} Object is applicable
* @param {index} Object index is applicable
* @return {Any} Returns array element
*/
this.indexBy = function( obj, index ) {
if ( !defend(arguments,['object','string']) ) {
return log([message.inv_args]);
}
for ( var i in obj ) {
if ( obj.hasOwnProperty(i) ) {
if ( i == index ) {
return obj[i];
}
}
}
};
/**
* Function returns a new merged object by specified objects
* @param {objA} Object is applicable
* @param {objB} Object is applicable
* @return {Object} Returns a new object
*/
this.mergeLists = function( objA, objB ) {
if ( !defend(arguments,['object','object']) ) {
return log([message.inv_args]);
}
var ret = {};
this.each(objA, function(v,i) {
ret[i] = v;
});
this.each(objB, function(v,i) {
ret[i] = v;
});
return ret;
};
/**
* Function escapes whitespaces from boths sides
* @param {str} String is applicable
* @return {String} Returns a trimmed string
*/
this.trim = function( str ) {
return str.replace(/^\s+|\s+$/g,"");
};
/**
* Function escapes whitespaces from right side
* @param {str} String is applicable
* @return {String} Returns a trimmed string
*/
this.rtrim = function( str ) {
return str.replace(/\s+$/,"");
};
/**
* Function escapes whitespaces from left side
* @param {str} String is applicable
* @return {String} Returns a trimmed string
*/
this.ltrim = function( str ) {
return str.replace(/^\s+/,"");
};
// ------------------------------------------------------------------ End Of Collection functions -----------------------------------------------------------------------
// ------------------------------------------------------------------------ String functions ------------------------------------------------------------------------
/**
* Function checks if specified argument's type is a string
* @param {str} Any type of argument is applicable
* @return {Boolean}
*/
this.isString = function( str ) {
return ( this.type(str) === 'string' );
};
/**
* Function returns capitalized string
* @param {str} Any type of argument is applicable
* @return {String} Returns capitalized string
*/
this.capitalize = function( str ) {
return str[0].toUpperCase() + str.substr(1).toLowerCase();
};
/**
* Function checks if specified string is a palindrome
* @param {str} String is applicable
* @return {Boolean}
*/
this.palindrome = function( str ) {
var len = str.length;
for ( var i = 0; i < Math.floor(len/2); i++ ) {
if ( str.charAt(i) !== str.charAt(len - 1 - i) ) {
return false;
}
}
return true;
};
/**
* Function returns random character between specified arguments
* @param {charFrom} Character is applicable
* @param {charTo} Character is applicable
* @return {Boolean} Returns random character
*/
this.randomChar = function( charFrom, charTo ) {
if ( !defend([charFrom,charTo],['string','string']) ) {
return log([message.inv_args]);
}
return String.fromCharCode(this.random(String.charCodeAt(charFrom), String.charCodeAt(charTo) + 1));
};
// ------------------------------------------------------------------ End Of String functions -----------------------------------------------------------------------
// ------------------------------------------------------------------------ Array functions -------------------------------------------------------------------------
/**
* Function returns sorted array based on flag (default true) parameter, true asc, false desc
* @param {array} Array is applicable
* @param {flag} Optional
* @return {Array} Returns sorted array
*/
this.sort = function( array, flag ) {
flag = ( typeof flag === 'undefined' || flag === true ) ? true : false;
if ( defend(array, createTypes(array, 'number')) ) {
if ( flag ) {
return array.sort(function(a,b){
return (a-b);
});
} else {
return array.sort(function(a,b){
return (b-a);
});
}
} else {
if ( flag ) {
return array.sort();
} else {
return array.sort().reverse();
}
}
};
/**
* Function returns a new array without/with arrB elements, flag optional (default true), true -> unique elements from first array, false -> repeated elements
* @param {arrA} Array is applicable
* @param {arrB} Array is applicable
* @param {flag} Optional
* @return {Array} Returns sorted array
*/
this.eliminate = function( arrA, arrB, flag ) {
if ( !defend([arrA, arrB], ['array','array']) ) {
return log([message.inv_args]);
}
flag = ( typeof flag === 'undefined' || flag === true ) ? true : false;
var ret = ( flag ) ? this.filter(arrA, function(v) {
return ( arrB.indexOf(v) === -1 );
}) : this.filter(arrA, function(v) {
return ( arrB.indexOf(v) !== -1 );
});
return ret;
};
/**
* Functions returns a new array with distinct elements
* @param {array} Array is applicable
* @return {Array} Returns array with distinct elements
*/
this.distinct = function( array ) {
if ( !defend(arguments, ['array']) ) {
return log([message.inv_args]);
}
var ret = [];
this.each(array, function(v) {
if ( ret.indexOf(v) === -1 ) {
ret.push(v);
}
});
return ret;
};
/**
* Functions returns a new array with distinct elements after merging arrays
* @param {array,...[array]} Array is applicable
* @return {Array} Returns a new array with distinct elements
*/
this.union = function() {
var types = createTypes(arguments, 'array');
if ( !defend(arguments, types) ) {
return log([message.inv_args]);
}
var ret = [];
for ( var i = 0; i < arguments.length; i++ ) {
ret = ret.concat(arguments[i]);
}
ret = this.distinct(ret);
return ret;
};
/**
* Functions returns a new array of repeated elements
* @param {array} Array is applicable
* @return {Array} Returns array of repeated elements
*/
this.repeat = function( array ) {
if ( !defend(arguments, ['array']) ) {
return log([message.inv_args]);
}
var ret = [];
this.each(array, function(v,i,o) {
o.splice(i,1);
if ( array.indexOf(v) !== -1 ) {
ret.push(v);
}
o.splice(i,0,v);
});
ret = this.distinct(ret);
return ret;
};
/**
* Functions returns a new array with unique elements
* @param {array} Array is applicable
* @return {Array} Returns array with unique elements
*/
this.unique = function( array ) {
if ( !defend(arguments, ['array']) ) {
return log([message.inv_args]);
}
return this.eliminate(array, this.repeat(array));
};
/**
* Functions returns Boolean based on existence of element in array
* @param {array} Array is applicable
* @param {value} Array element
* @return {Boolean} Returns Boolean
*/
this.contains = function( array, value ) {
if ( !defend([array], ['array']) ) {
return log([message.inv_args]);
}
if ( this.isInteger(value) ) {
value = parseInt(value);
}
return (array.indexOf(value) !== -1);
};
/**
* Functions returns a new array with first n elements from the array
* @param {array} Array is applicable
* @param {n} Optional
* @return {Array} Returns first n elements from the array
*/
this.first = function( array, n ) {
n = ( typeof n === 'undefined' || !this.isInteger(n) ) ? 1 : n;
if ( !defend([array], ['array']) ) {
return log([message.inv_args]);
}
var ret = [];
for ( var i = 0; i < array.length; i++ ) {
if ( n > 0 ) {
if ( !this.isUndefined(array[i]) ) {
ret.push(array[i]);
}
}
n--;
}
return ret;
};
/**
* Functions returns a new array with last n elements from the array
* @param {array} Array is applicable
* @param {n} Optional
* @return {Array} Returns last n elements from the array
*/
this.last = function( array, n ) {
if ( !defend([array], ['array']) ) {
return log([message.inv_args]);
}
array = array.reverse();
return this.first(array,n).reverse();
};
/**
* Functions returns a new array from start index to to index elements from the array
* @param {array} Array is applicable
* @param {fr} From array index
* @param {to} To array index
* @return {Array} Returns rest elements from the array
*/
this.rest = function( array, fr, to ) {
if ( this.type(array) === 'array' && this.isInteger(fr) && this.isInteger(to) ) {
return array.slice(fr, to);
}
return log([message.inv_args]);
};
/**
* Functions returns a new array of random n elements from the array
* @param {array} Array is applicable
* @param {n} Optional
* @return {Array} Returns random n elements from the array
*/
this.randElem = function( array, n ) {
if ( !defend([array], ['array']) ) {
return log([message.inv_args]);
}
n = ( typeof n === 'undefined' || !this.isInteger(n) ) ? 1 : n;
var ret = [], r = Math.floor(Math.random() * array.length);
for ( var i = 0; i < array.length; i++ ) {
if ( n > 0 ) {
while ( ret.indexOf(array[r]) !== -1 ) {
r = Math.floor(Math.random() * array.length);
}
ret.push(array[r]);
} else {
break;
}
n--;
}
return ret;
};
/**
* Functions returns a shuffled copy of the array
* @param {array} Array is applicable
* @return {Array} Returns a shuffled copy of the array
*/
this.shuffle = function( array ) {
if ( !defend(arguments, ['array']) ) {
return log([message.inv_args]);
}
return this.randElem(array,array.length);
};
/**
* Functions Boolean if user defined function will pass at least once
* @param {array} Array is applicable
* @param {fn} User defined function
* @return {Array} Returns boolean
*/
this.some = function( array, fn ) {
if ( !defend([array], ['array']) ) {
return log([message.inv_args]);
}
for ( var i = 0; i < array.length; i++ ) {
if ( fn.call(array || null, array[i], i, array) ) {
return true;
}
}
return false;
};
/**
* Functions Boolean if user defined function will pass for all array elements
* @param {array} Array is applicable
* @param {fn} User defined function
* @return {Array} Returns boolean
*/
this.every = function( array, fn ) {
if ( !defend([array], ['array']) ) {
return log([message.inv_args]);
}
for ( var i = 0; i < array.length; i++ ) {
if ( !fn.call(array || null, array[i], i, array) ) {
return false;
}
}
return true;
};
/**
* Functions returns keys of the object
* @param {obj} Object is applicable
* @return {Array} Returns array
*/
this.keys = function( obj ) {
if ( !defend(arguments, ['object']) ) {
return log([message.inv_args]);
}
var ret = this.map(obj, function(v,i) {
return i;
});
return ret;
};
/**
* Functions returns values of the object
* @param {obj} Object is applicable
* @return {Array} Returns array
*/
this.values = function( obj ) {
if ( !defend(arguments, ['object']) ) {
return log([message.inv_args]);
}
var ret = this.map(obj, function(v) {
return v;
});
return ret;
};
/**
* Functions returns an array ranged from start to stop by step
* @param {start} Number is applicable
* @param {stop} Number is applicable
* @param {step} Number is applicable
* @return {Array} Returns array
*/
this.range = function( start, stop, step ) {
if ( arguments.length === 0 || !defend(arguments, ['number','number','number']) ) {
return log([message.inv_args]);
}
var ret = [];
for ( var i = start; i < stop; i += step ) {
ret.push(i);
}
return ret;
};
// ---------------------------------------------------------------------- End Of Array functions ------------------------------------------------------------------------
// ------------------------------------------------------------------------ Mathematical functions ---------------------------------------------------------------------
/**
* Function returns Boolean if specified argument is integer
* @param {n} Number is applicable
* @return {Boolean}
*/
this.isInteger = function( n ) {
return (n % 1 === 0);
};
/**
* Function returns min/max number from the array accordingly flag parameter
* @param {array} Number is applicable
* @param {flag} True -> min, False -> max
* @return {number}
*/
this.minMax = function( array, flag ) {
if ( !defend([array],['array']) ) {
return log([message.inv_args]);
}
flag = ( typeof flag === 'undefined' || flag === true ) ? true : false;
var ret = array[0];
for ( var i = 0; i < array.length; i++ ) {
if ( flag ) {
if ( array[i] < ret ) {
ret = array[i];
}
} else {
if ( array[i] > ret ) {
ret = array[i];
}
}
}
return ret;
};
/**
* Function returns Boolean/Array based on flag parameter if the number is prime
* @param {n} Number is applicable
* @param {flag} True -> Display n primes from 0, False (default) -> boolean
* @return {Boolean/Array}
*/
this.isPrime = function( n, flag ) {
if ( !defend([n],['number']) ) {
return log([message.inv_args]);
}
( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;
function isPrime( num ) {
if ( num === 0 || num === 1 ) {
return false;
}
for ( var i = 2; i < num; i++ ) {
if ( num % i === 0 ) {
return false;
}
}
return true;
}
if ( flag ) {
var arr = [2];
for ( var i = 3; i <= n; i+=2 ) {
if ( isPrime(i) ) {
arr.push(i);
}
}
return arr;
} else {
return isPrime(n);
}
};
/**
* Function returns Boolean/Array based on flag parameter
* @param {n} Number is applicable
* @param {flag} True -> Display facotiral of n based, False (default) -> Number
* @return {String/Number}
*/
this.isFactorial = function( n, flag ) {
if ( !defend([n],['number']) ) {
return log([message.inv_args]);
}
( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;
if ( flag ) {
var sum = 1;
for ( var i = 1; i <= n && sum <= n; i++ ) {
if ( sum === n ) {
return n + " is factorial number based on " + (i - 1);
}
sum *= i;
}
return false;
} else {
var sum = 1;
for ( var i = 1; i <= n; i++ ) {
sum *= i;
}
return sum;
}
};
/**
* Function returns Boolean/Array based on flag parameter if the number belongs to fibonacci number sequence
* @param {n} Number is applicable
* @param {flag} True -> Display n fibonacci numbers from 0 to n, False (default) -> boolean
* @return {Boolean/Array}
*/
this.isFibonacci = function( n, flag ) {
if ( !defend([n],['number']) ) {
return log([message.inv_args]);
}
( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;
var a = 0, b = 1, z = null;
if ( flag ) {
var arr = [];
for ( var i = 0; i <= n; i++ ) {
arr.push(a);
z = a + b;
a = b;
b = z;
}
return arr;
} else {
for ( var i = 0; i <= n && a <= n; i++ ) {
if ( a === n ) {
return true;
}
z = a + b;
a = b;
b = z;
}
return false;
}
};
// ---------------------------------------------------------------------- End Of Mathematical functions -----------------------------------------------------------------
// ------------------------------------------------------------------------ Utility functions --------------------------------------------------------------------------
/**
* Function returns an array of library function names
* @return {Array} Returns function names array
*/
this.functions = function() {
var ret = [];
this.each(this, function(v,i) {
if ( _scope.isFunction(v) ) {
ret.push(i);
}
});
this.sort(ret);
return ret;
};
var HTMLEntities = { /** @private, HTMLEntities list */
'escape' : {
'&' : '&',
'<' : '<',
'>' : '>',
'"' : '"',
'\'': ''',
'(' : '(',
')' : ')'
},
'unescape' : function() {
return _scope.invert(this.escape);
}
};
/**
* Function Adds entities to an escape list (HTMLEntities.escape)
* @param {list} Object is applicable
*/
this.addEntities = function( list ) {
if ( !defend(arguments,['object']) ) {
return log([message.inv_args]);
}
HTMLEntities.escape = this.mergeLists( HTMLEntities.escape, list );
console.log(HTMLEntities.escape);
};
/**
* Function Removes entities from an escape list (HTMLEntities.escape)
* @param {array} Array is applicable
*/
this.removeEntities = function( array ) {
if ( !defend(arguments,['array']) ) {
return log([message.inv_args]);
}
var ret = {};
this.each(HTMLEntities.escape, function(v,i) {
if ( array.indexOf(i) == -1 ) {
ret[i] = v;
}
});
HTMLEntities.escape = ret;
};
/**
* Function returns escaped string based on HTMLEntities escape parameters
* @param {str} String is applicable
* @return {String} Returns trimmed string
*/
this.escape = function( str ) {
if ( !defend(arguments,['string']) ) {
return log([message.inv_args]);
}
return str.replace(new RegExp('[' + this.keys(HTMLEntities.escape).join('') + ']','g'),function(a) {
return _scope.indexBy(HTMLEntities.escape,a);
});
};
/**
* Function returns unescaped string based on HTMLEntities escape parameters
* @param {str} String is applicable
* @return {String} Returns untrimmed string
*/
this.unescape = function( str ) {
if ( !defend(arguments,['string']) ) {
return log([message.inv_args]);
}
var un = HTMLEntities.unescape();
return str.replace(new RegExp('(' + this.keys(un).join('|') + ')','g'),function(a) {
return _scope.indexBy(un,a);
});
};
/**
* Function returns converted string on specific base
* @param {str} String is applicable
* @param {base} Number is applicable
* @return {String} Returns converted string
*/
this.convert = function( str, base ) {
if ( !defend(arguments,['string','number']) ) {
return log([message.inv_args]);
}
if ( this.isInteger(str) ) {
return parseInt(str).toString(base);
} else {
var n = ( base == 2 ) ? 8 : ( ( base == 8 ) ? 3 : ( base == 16 ) ? 2 : 0 );
if ( n == 0 ) {
return log([message.inv_base]);
}
var ret = '', temp = '';
for ( var i = 0; i < str.length; i++ ) {
temp += str.charCodeAt(i).toString(base);
while ( temp.length < n ) {
temp = '0' + temp;
}