OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 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. | 6 * @param {string} toTest The string to be tested. |
| 7 * @return {boolean} True if |toTest| contains only digits. Leading and trailing |
| 8 * whitespace is allowed. |
9 */ | 9 */ |
10 function isInteger(toTest) { | 10 function isInteger(toTest) { |
11 var numericExp = /^\s*[0-9]+\s*$/; | 11 var numericExp = /^\s*[0-9]+\s*$/; |
12 return numericExp.test(toTest); | 12 return numericExp.test(toTest); |
13 } | 13 } |
14 | 14 |
15 /** | 15 /** |
16 * Returns true if |value| is a valid non zero positive integer. | 16 * Returns true if |value| is a valid non zero positive integer. |
17 * @param {string} value The string to be tested. | 17 * @param {string} value The string to be tested. |
18 * | 18 * |
19 * @return {boolean} true if the |value| is valid non zero positive integer. | 19 * @return {boolean} true if the |value| is valid non zero positive integer. |
20 */ | 20 */ |
21 function isPositiveInteger(value) { | 21 function isPositiveInteger(value) { |
22 return isInteger(value) && parseInt(value, 10) > 0; | 22 return isInteger(value) && parseInt(value, 10) > 0; |
23 } | 23 } |
24 | 24 |
25 /** | 25 /** |
26 * Returns true if the contents of the two arrays are equal. | 26 * Returns true if the contents of the two arrays are equal. |
27 * @param {Array} array1 The first array. | 27 * @param {Array} array1 The first array. |
28 * @param {Array} array1 The second array. | 28 * @param {Array} array2 The second array. |
29 * | 29 * |
30 * @return {boolean} true if the arrays are equal. | 30 * @return {boolean} true if the arrays are equal. |
31 */ | 31 */ |
32 function areArraysEqual(array1, array2) { | 32 function areArraysEqual(array1, array2) { |
33 if (array1.length != array2.length) | 33 if (array1.length != array2.length) |
34 return false; | 34 return false; |
35 for (var i = 0; i < array1.length; i++) | 35 for (var i = 0; i < array1.length; i++) |
36 if(array1[i] != array2[i]) | 36 if (array1[i] != array2[i]) |
37 return false; | 37 return false; |
38 return true; | 38 return true; |
39 } | 39 } |
40 | 40 |
41 /** | 41 /** |
42 * Removes duplicate elements from |inArray| and returns a new array. | 42 * Removes duplicate elements from |inArray| and returns a new array. |
43 * |inArray| is not affected. It assumes that |inArray| is already sorted. | 43 * |inArray| is not affected. It assumes that |inArray| is already sorted. |
44 * | 44 * @param {array} inArray The array to be processed. |
45 * @param {Array} inArray The array to be processed. | 45 * @return {array} The array after processing. |
46 */ | 46 */ |
47 function removeDuplicates(inArray) { | 47 function removeDuplicates(inArray) { |
48 var out = []; | 48 var out = []; |
49 | 49 |
50 if(inArray.length == 0) | 50 if (inArray.length == 0) |
51 return out; | 51 return out; |
52 | 52 |
53 out.push(inArray[0]); | 53 out.push(inArray[0]); |
54 for (var i = 1; i < inArray.length; ++i) | 54 for (var i = 1; i < inArray.length; ++i) |
55 if(inArray[i] != inArray[i - 1]) | 55 if (inArray[i] != inArray[i - 1]) |
56 out.push(inArray[i]); | 56 out.push(inArray[i]); |
57 return out; | 57 return out; |
58 } | 58 } |
59 | 59 |
60 /** | 60 /** |
61 * Checks if |pageRangeText| represents a valid page selection. | 61 * Checks if |pageRangeText| represents a valid page selection. |
62 * A valid selection has a parsable format and every page identifier is | 62 * A valid selection has a parsable format and every page identifier is |
63 * <= |totalPageCount| unless wildcards are used (see examples). | 63 * <= |totalPageCount| unless wildcards are used (see examples). |
64 * Example: "1-4, 9, 3-6, 10, 11" is valid, assuming |totalPageCount| >= 11. | 64 * Example: "1-4, 9, 3-6, 10, 11" is valid, assuming |totalPageCount| >= 11. |
65 * Example: "1-4, 6-6" is valid, assuming |totalPageCount| >= 6. | 65 * Example: "1-4, 6-6" is valid, assuming |totalPageCount| >= 6. |
66 * Example: "2-" is valid, assuming |totalPageCount| >= 2, means from 2 to the | 66 * Example: "2-" is valid, assuming |totalPageCount| >= 2, means from 2 to the |
67 * end. | 67 * end. |
68 * Example: "1-10000" is valid, regardless of |totalPageCount|, means from 1 to | 68 * Example: "1-10000" is valid, regardless of |totalPageCount|, means from 1 to |
69 * the end if |totalPageCount| < 10000. | 69 * the end if |totalPageCount| < 10000. |
70 * Example: "1-4dsf, 11" is invalid regardless of |totalPageCount|. | 70 * Example: "1-4dsf, 11" is invalid regardless of |totalPageCount|. |
71 * Example: "4-2, 11, -6" is invalid regardless of |totalPageCount|. | 71 * Example: "4-2, 11, -6" is invalid regardless of |totalPageCount|. |
72 * | 72 * |
73 * Note: If |totalPageCount| is undefined the validation does not take | 73 * Note: If |totalPageCount| is undefined the validation does not take |
74 * |totalPageCount| into account. | 74 * |totalPageCount| into account. |
75 * Example: "34853253" is valid. | 75 * Example: "34853253" is valid. |
76 * Example: "1-4, 9, 3-6, 10, 11" is valid. | 76 * Example: "1-4, 9, 3-6, 10, 11" is valid. |
77 * | 77 * |
| 78 * @param {string} pageRangeText The text to be checked. |
| 79 * @param {number} totalPageCount The total number of pages. |
78 * @return {boolean} true if the |pageRangeText| is valid. | 80 * @return {boolean} true if the |pageRangeText| is valid. |
79 */ | 81 */ |
80 function isPageRangeTextValid(pageRangeText, totalPageCount) { | 82 function isPageRangeTextValid(pageRangeText, totalPageCount) { |
81 var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; | 83 var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; |
82 var successfullyParsed = 0; | 84 var successfullyParsed = 0; |
83 | 85 |
84 // Splitting around commas. | 86 // Splitting around commas. |
85 var parts = pageRangeText.split(/,/); | 87 var parts = pageRangeText.split(/,/); |
86 | 88 |
87 for (var i = 0; i < parts.length; ++i) { | 89 for (var i = 0; i < parts.length; ++i) { |
(...skipping 17 matching lines...) Expand all Loading... |
105 } | 107 } |
106 return successfullyParsed > 0; | 108 return successfullyParsed > 0; |
107 } | 109 } |
108 | 110 |
109 /** | 111 /** |
110 * Returns a list of all pages specified in |pagesRangeText|. The pages are | 112 * Returns a list of all pages specified in |pagesRangeText|. The pages are |
111 * listed in the order they appear in |pageRangeText| and duplicates are not | 113 * listed in the order they appear in |pageRangeText| and duplicates are not |
112 * eliminated. If |pageRangeText| is not valid according to | 114 * eliminated. If |pageRangeText| is not valid according to |
113 * isPageRangeTextValid(), or |totalPageCount| is undefined an empty list is | 115 * isPageRangeTextValid(), or |totalPageCount| is undefined an empty list is |
114 * returned. | 116 * returned. |
115 * | 117 * @param {string} pageRangeText The text to be checked. |
116 * @return {Array} | 118 * @param {number} totalPageCount The total number of pages. |
| 119 * @return {array} A list of all pages. |
117 */ | 120 */ |
118 function pageRangeTextToPageList(pageRangeText, totalPageCount) { | 121 function pageRangeTextToPageList(pageRangeText, totalPageCount) { |
119 var pageList = []; | 122 var pageList = []; |
120 if ((totalPageCount && !isPageRangeTextValid(pageRangeText, totalPageCount)) | 123 if ((totalPageCount && !isPageRangeTextValid(pageRangeText, totalPageCount)) |
121 || !totalPageCount) { | 124 || !totalPageCount) { |
122 return pageList; | 125 return pageList; |
123 } | 126 } |
124 | 127 |
125 var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; | 128 var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; |
126 var parts = pageRangeText.split(/,/); | 129 var parts = pageRangeText.split(/,/); |
(...skipping 12 matching lines...) Expand all Loading... |
139 if (isPositiveInteger(singlePageNumber) && | 142 if (isPositiveInteger(singlePageNumber) && |
140 singlePageNumber <= totalPageCount) { | 143 singlePageNumber <= totalPageCount) { |
141 pageList.push(singlePageNumber); | 144 pageList.push(singlePageNumber); |
142 } | 145 } |
143 } | 146 } |
144 } | 147 } |
145 return pageList; | 148 return pageList; |
146 } | 149 } |
147 | 150 |
148 /** | 151 /** |
149 * Returns the contents of |pageList| in ascending order and without any | 152 * @param {array} pageList The list to be processed. |
150 * duplicates. |pageList| is not affected. | 153 * @return {array} The contents of |pageList| in ascending order and without any |
151 * | 154 * duplicates. |pageList| is not affected. |
152 * @return {Array} | |
153 */ | 155 */ |
154 function pageListToPageSet(pageList) { | 156 function pageListToPageSet(pageList) { |
155 var pageSet = []; | 157 var pageSet = []; |
156 if (pageList.length == 0) | 158 if (pageList.length == 0) |
157 return pageSet; | 159 return pageSet; |
158 pageSet = pageList.slice(0); | 160 pageSet = pageList.slice(0); |
159 pageSet.sort(function(a,b) { return a - b; }); | 161 pageSet.sort(function(a, b) { return a - b; }); |
160 pageSet = removeDuplicates(pageSet); | 162 pageSet = removeDuplicates(pageSet); |
161 return pageSet; | 163 return pageSet; |
162 } | 164 } |
163 | 165 |
164 /** | 166 /** |
165 * Converts |pageSet| to page ranges. It squashes whenever possible. | 167 * Converts |pageSet| to page ranges. It squashes whenever possible. |
166 * Example: '1-2,3,5-7' becomes 1-3,5-7. | 168 * Example: '1-2,3,5-7' becomes 1-3,5-7. |
167 * | 169 * |
| 170 * @param {array} pageSet The set of pages to be processed. Callers should |
| 171 * ensure that no duplicates exist. |
168 * @return {Array} an array of page range objects. A page range object has | 172 * @return {Array} an array of page range objects. A page range object has |
169 * fields 'from' and 'to'. | 173 * fields 'from' and 'to'. |
170 */ | 174 */ |
171 function pageSetToPageRanges(pageSet) { | 175 function pageSetToPageRanges(pageSet) { |
172 var pageRanges = []; | 176 var pageRanges = []; |
173 for (var i = 0; i < pageSet.length; ++i) { | 177 for (var i = 0; i < pageSet.length; ++i) { |
174 tempFrom = pageSet[i]; | 178 tempFrom = pageSet[i]; |
175 while (i + 1 < pageSet.length && pageSet[i + 1] == pageSet[i] + 1) | 179 while (i + 1 < pageSet.length && pageSet[i + 1] == pageSet[i] + 1) |
176 ++i; | 180 ++i; |
177 tempTo = pageSet[i]; | 181 tempTo = pageSet[i]; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 240 } |
237 | 241 |
238 /** | 242 /** |
239 * Converts |value| from points to millimeters. | 243 * Converts |value| from points to millimeters. |
240 * @param {number} value The number in points. | 244 * @param {number} value The number in points. |
241 * @return {number} |value| in millimeters. | 245 * @return {number} |value| in millimeters. |
242 */ | 246 */ |
243 function convertPointsToMillimeters(value) { | 247 function convertPointsToMillimeters(value) { |
244 return value / POINTS_PER_MILLIMETER; | 248 return value / POINTS_PER_MILLIMETER; |
245 } | 249 } |
OLD | NEW |