OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * Returns true if |toTest| contains only digits. Leading and trailing | |
7 * whitespace is allowed. | |
8 * @param {string} toTest The string to be tested. | |
9 */ | |
10 function isInteger(toTest) { | |
11 var numericExp = /^\s*[0-9]+\s*$/; | |
12 return numericExp.test(toTest); | |
13 } | |
14 | |
15 /** | |
16 * Returns true if |value| is a valid non zero positive integer. | |
17 * @param {string} value The string to be tested. | |
18 * | |
19 * @return {boolean} true if the |value| is valid non zero positive integer. | |
20 */ | |
21 function isPositiveInteger(value) { | |
22 return isInteger(value) && parseInt(value, 10) > 0; | |
23 } | |
24 | |
25 /** | |
26 * Returns true if the contents of the two arrays are equal. | |
Lei Zhang
2011/06/13 20:51:51
I realize this is cut + paste, but please add the
dpapad
2011/06/13 21:44:02
Done.
| |
27 */ | |
28 function areArraysEqual(array1, array2) { | |
29 if (array1.length != array2.length) | |
30 return false; | |
31 for (var i = 0; i < array1.length; i++) | |
32 if(array1[i] != array2[i]) | |
33 return false; | |
34 return true; | |
35 } | |
36 | |
37 /** | |
38 * Removes duplicate elements from |inArray| and returns a new array. | |
39 * |inArray| is not affected. It assumes that |inArray| is already sorted. | |
40 * | |
41 * @param {Array} inArray The array to be processed. | |
42 */ | |
43 function removeDuplicates(inArray) { | |
44 var out = []; | |
45 | |
46 if(inArray.length == 0) | |
47 return out; | |
48 | |
49 out.push(inArray[0]); | |
50 for (var i = 1; i < inArray.length; ++i) | |
51 if(inArray[i] != inArray[i - 1]) | |
52 out.push(inArray[i]); | |
53 return out; | |
54 } | |
55 | |
56 /** | |
57 * Checks if |pageRangeText| represents a valid page selection. | |
58 * A valid selection has a parsable format and every page identifier is | |
59 * <= |totalPageCount| unless wildcards are used (see examples). | |
60 * Example: "1-4, 9, 3-6, 10, 11" is valid, assuming |totalPageCount| >= 11. | |
61 * Example: "1-4, 6-6" is valid, assuming |totalPageCount| >= 6. | |
62 * Example: "2-" is valid, assuming |totalPageCount| >= 2, means from 2 to the | |
63 * end. | |
64 * Example: "1-10000" is valid, regardless of |totalPageCount|, means from 1 to | |
65 * the end if |totalPageCount| < 10000. | |
66 * Example: "1-4dsf, 11" is invalid regardless of |totalPageCount|. | |
67 * Example: "4-2, 11, -6" is invalid regardless of |totalPageCount|. | |
68 * | |
69 * Note: If |totalPageCount| is undefined the validation does not take | |
70 * |totalPageCount| into account. | |
71 * Example: "34853253" is valid. | |
72 * Example: "1-4, 9, 3-6, 10, 11" is valid. | |
73 * | |
74 * @return {boolean} true if the |pageRangeText| is valid. | |
75 */ | |
76 function isPageRangeTextValid(pageRangeText, totalPageCount) { | |
77 var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; | |
78 var successfullyParsed = 0; | |
79 | |
80 // Splitting around commas. | |
81 var parts = pageRangeText.split(/,/); | |
82 | |
83 for (var i = 0; i < parts.length; ++i) { | |
84 var match = parts[i].match(regex); | |
85 if (parts[i].length == 0) | |
86 continue; | |
87 | |
88 if (match && match[1] && isPositiveInteger(match[1])) { | |
89 var from = parseInt(match[1], 10); | |
90 var to = isPositiveInteger(match[2]) ? parseInt(match[2], 10) : | |
91 totalPageCount; | |
92 if (from > to) | |
93 return false; | |
94 } else if (!isPositiveInteger(parts[i]) || (totalPageCount != -1 && | |
95 parseInt(parts[i], 10) > totalPageCount)) { | |
96 return false; | |
97 } | |
98 successfullyParsed++; | |
99 } | |
100 return successfullyParsed > 0; | |
101 } | |
102 | |
103 /** | |
104 * Returns a list of all pages specified in |pagesRangeText|. The pages are | |
105 * listed in the order they appear in |pageRangeText| and duplicates are not | |
106 * eliminated. If |pageRangeText| is not valid according to | |
107 * isPageRangeTextValid(), or |totalPageCount| is undefined an empty list is | |
108 * returned. | |
109 * | |
110 * @return {Array} | |
111 */ | |
112 function pageRangeTextToPageList(pageRangeText, totalPageCount) { | |
113 var pageList = []; | |
114 if ((totalPageCount && !isPageRangeTextValid(pageRangeText, totalPageCount)) | |
115 || !totalPageCount) { | |
116 return pageList; | |
117 } | |
118 | |
119 var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; | |
120 var parts = pageRangeText.split(/,/); | |
121 | |
122 for (var i = 0; i < parts.length; ++i) { | |
123 var match = parts[i].match(regex); | |
124 | |
125 if (match && match[1]) { | |
126 var from = parseInt(match[1], 10); | |
127 var to = match[2] ? parseInt(match[2], 10) : totalPageCount; | |
128 | |
129 for (var j = from; j <= Math.min(to, totalPageCount); ++j) | |
130 pageList.push(j); | |
131 } else { | |
132 var singlePageNumber = parseInt(parts[i], 10); | |
133 if (isPositiveInteger(singlePageNumber) && | |
134 singlePageNumber <= totalPageCount) { | |
135 pageList.push(singlePageNumber); | |
136 } | |
137 } | |
138 } | |
139 return pageList; | |
140 } | |
141 | |
142 /** | |
143 * Returns the contents of |pageList| in ascending order and without any | |
144 * duplicates. |pageList| is not affected. | |
145 * | |
146 * @return {Array} | |
147 */ | |
148 function pageListToPageSet(pageList) { | |
149 var pageSet = []; | |
150 if (pageList.length == 0) | |
151 return pageSet; | |
152 pageSet = pageList.slice(0); | |
153 pageSet.sort(function(a,b) { return a - b; }); | |
154 pageSet = removeDuplicates(pageSet); | |
155 return pageSet; | |
156 } | |
157 | |
158 /** | |
159 * Converts |pageSet| to page ranges. It squashes whenever possible. | |
Lei Zhang
2011/06/13 20:51:51
Can you keep the example you had before?
dpapad
2011/06/13 21:44:02
Done.
| |
160 * | |
161 * @return {Array} an array of page range objects. A page range object has | |
162 * fields 'from' and 'to'. | |
163 */ | |
164 function pageSetToPageRanges(pageSet) { | |
165 var pageRanges = []; | |
166 for (var i = 0; i < pageSet.length; ++i) { | |
167 tempFrom = pageSet[i]; | |
168 while (i + 1 < pageSet.length && pageSet[i + 1] == pageSet[i] + 1) | |
169 ++i; | |
170 tempTo = pageSet[i]; | |
171 pageRanges.push({'from': tempFrom, 'to': tempTo}); | |
172 } | |
173 return pageRanges; | |
174 } | |
OLD | NEW |