UnitTC: Additional unit testcases have been added
[platform/framework/web/web-ui-fw.git] / src / js / util / range.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Makes array with given range
3 //>>label: Range
4 //>>group: Tizen:Utilities
5
6 define( [ ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
8
9 /*
10  * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL licenses
11  * http://phpjs.org/functions/range
12  * original by: Waldo Malqui Silva
13  * version: 1107.2516
14  */
15 function range( low, high, step ) {
16     // Create an array containing the range of integers or characters
17     // from low to high (inclusive)  
18     // 
19     // version: 1107.2516
20     // discuss at: http://phpjs.org/functions/range
21     // +   original by: Waldo Malqui Silva
22     // *     example 1: range ( 0, 12 );
23     // *     returns 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
24     // *     example 2: range( 0, 100, 10 );
25     // *     returns 2: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
26     // *     example 3: range( 'a', 'i' );
27     // *     returns 3: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
28     // *     example 4: range( 'c', 'a' );
29     // *     returns 4: ['c', 'b', 'a']
30         var matrix = [],
31                 inival,
32                 endval,
33                 plus,
34                 walker = step || 1,
35                 chars = false;
36
37     if (!isNaN(low) && !isNaN(high)) {
38         inival = low;
39         endval = high;
40     } else if (isNaN(low) && isNaN(high)) {
41         chars = true;
42         inival = low.charCodeAt(0);
43         endval = high.charCodeAt(0);
44     } else {
45         inival = (isNaN(low) ? 0 : low);
46         endval = (isNaN(high) ? 0 : high);
47     }
48
49     plus = ((inival > endval) ? false : true);
50     if (plus) {
51         while (inival <= endval) {
52             matrix.push(((chars) ? String.fromCharCode(inival) : inival));
53             inival += walker;
54         }
55     } else {
56         while (inival >= endval) {
57             matrix.push(((chars) ? String.fromCharCode(inival) : inival));
58             inival -= walker;
59         }
60     }
61
62     return matrix;
63 }
64
65 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
66 } );
67 //>>excludeEnd("jqmBuildExclude");