| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 this.swarming = this.swarming || function() { | 5 this.swarming = this.swarming || function() { |
| 6 | 6 |
| 7 var swarming = {}; | 7 var swarming = {}; |
| 8 | 8 |
| 9 // return the longest string in an array | 9 // return the longest string in an array |
| 10 swarming.longest = function(arr) { | 10 swarming.longest = function(arr) { |
| 11 var most = ""; | 11 var most = ""; |
| 12 for(var i = 0; i < arr.length; i++) { | 12 for(var i = 0; i < arr.length; i++) { |
| 13 if (arr[i] && arr[i].length > most.length) { | 13 if (arr[i] && arr[i].length > most.length) { |
| 14 most = arr[i]; | 14 most = arr[i]; |
| 15 } | 15 } |
| 16 } | 16 } |
| 17 return most; | 17 return most; |
| 18 }; | 18 }; |
| 19 | 19 |
| 20 // naturalCompare tries to use natural sorting (e.g. sort ints by value). | |
| 21 swarming.naturalCompare = function(a, b) { | |
| 22 // Try numeric, aka "natural" sort and use it if ns is not NaN. | |
| 23 // Javascript will try to corece these to numbers or return NaN. | |
| 24 var ns = a - b; | |
| 25 if (!isNaN(ns)) { | |
| 26 return ns; | |
| 27 } | |
| 28 return String(a).localeCompare(b); | |
| 29 }; | |
| 30 | |
| 31 | |
| 32 swarming.stableSort = function(arr, comp) { | 20 swarming.stableSort = function(arr, comp) { |
| 33 if (!arr || !comp) { | 21 if (!arr || !comp) { |
| 34 console.log("missing arguments to stableSort", arr, comp); | 22 console.log("missing arguments to stableSort", arr, comp); |
| 35 return; | 23 return; |
| 36 } | 24 } |
| 37 // We can guarantee a potential non-stable sort (like V8's | 25 // We can guarantee a potential non-stable sort (like V8's |
| 38 // Array.prototype.sort()) to be stable by first storing the index in the | 26 // Array.prototype.sort()) to be stable by first storing the index in the |
| 39 // original sorting and using that if the original compare was 0. | 27 // original sorting and using that if the original compare was 0. |
| 40 arr.forEach(function(e, i){ | 28 arr.forEach(function(e, i){ |
| 41 if (e !== undefined && e !== null) { | 29 if (e !== undefined && e !== null) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 return response; | 67 return response; |
| 80 }).catch(function(reason) { | 68 }).catch(function(reason) { |
| 81 console.log("Request failed", reason); | 69 console.log("Request failed", reason); |
| 82 sk.errorMessage("Request failed. Reason: "+reason, 5000); | 70 sk.errorMessage("Request failed. Reason: "+reason, 5000); |
| 83 return Promise.reject(reason); | 71 return Promise.reject(reason); |
| 84 }); | 72 }); |
| 85 } | 73 } |
| 86 | 74 |
| 87 return swarming; | 75 return swarming; |
| 88 }(); | 76 }(); |
| OLD | NEW |