| 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 // naturalCompare tries to use natural sorting (e.g. sort ints by value). |
| 10 swarming.naturalCompare = function(a, b) { |
| 11 // Try numeric, aka "natural" sort and use it if ns is not NaN. |
| 12 // Javascript will try to corece these to numbers or return NaN. |
| 13 var ns = a - b; |
| 14 if (!isNaN(ns)) { |
| 15 return ns; |
| 16 } |
| 17 return String(a).localeCompare(b); |
| 18 }; |
| 19 |
| 20 |
| 9 swarming.stableSort = function(arr, comp) { | 21 swarming.stableSort = function(arr, comp) { |
| 10 if (!arr || !comp) { | 22 if (!arr || !comp) { |
| 11 console.log("missing arguments to stableSort", arr, comp); | 23 console.log("missing arguments to stableSort", arr, comp); |
| 12 return; | 24 return; |
| 13 } | 25 } |
| 14 // We can guarantee a potential non-stable sort (like V8's | 26 // We can guarantee a potential non-stable sort (like V8's |
| 15 // Array.prototype.sort()) to be stable by first storing the index in the | 27 // Array.prototype.sort()) to be stable by first storing the index in the |
| 16 // original sorting and using that if the original compare was 0. | 28 // original sorting and using that if the original compare was 0. |
| 17 arr.forEach(function(e, i){ | 29 arr.forEach(function(e, i){ |
| 18 if (e !== undefined && e !== null) { | 30 if (e !== undefined && e !== null) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 32 return -1; | 44 return -1; |
| 33 } | 45 } |
| 34 var c = comp(a, b); | 46 var c = comp(a, b); |
| 35 if (c === 0) { | 47 if (c === 0) { |
| 36 return a.__sortIdx - b.__sortIdx; | 48 return a.__sortIdx - b.__sortIdx; |
| 37 } | 49 } |
| 38 return c; | 50 return c; |
| 39 }); | 51 }); |
| 40 } | 52 } |
| 41 | 53 |
| 42 // naturalCompare tries to use natural sorting (e.g. sort ints by value). | |
| 43 swarming.naturalCompare = function(a, b) { | |
| 44 // Try numeric, aka "natural" sort and use it if ns is not NaN. | |
| 45 // Javascript will try to corece these to numbers or return NaN. | |
| 46 var ns = a - b; | |
| 47 if (!isNaN(ns)) { | |
| 48 return ns; | |
| 49 } | |
| 50 return String(a).localeCompare(b); | |
| 51 }; | |
| 52 | |
| 53 // postWithToast makes a post request and updates the error-toast | 54 // postWithToast makes a post request and updates the error-toast |
| 54 // element with the response, regardless of failure. See error-toast.html | 55 // element with the response, regardless of failure. See error-toast.html |
| 55 // for more information. | 56 // for more information. |
| 56 swarming.postWithToast = function(url, msg, auth_headers) { | 57 swarming.postWithToast = function(url, msg, auth_headers) { |
| 57 // Keep toast displayed until we hear back from the request. | 58 // Keep toast displayed until we hear back from the request. |
| 58 sk.errorMessage(msg, 0); | 59 sk.errorMessage(msg, 0); |
| 59 | 60 |
| 60 sk.request("POST", url, undefined, auth_headers).then(function(response) { | 61 sk.request("POST", url, undefined, auth_headers).then(function(response) { |
| 61 sk.errorMessage("Request sent. Response: "+response, 3000); | 62 sk.errorMessage("Request sent. Response: "+response, 3000); |
| 62 }).catch(function(reason) { | 63 }).catch(function(reason) { |
| 63 console.log("Request failed", reason); | 64 console.log("Request failed", reason); |
| 64 sk.errorMessage("Request failed. Reason: "+reason, 5000); | 65 sk.errorMessage("Request failed. Reason: "+reason, 5000); |
| 65 }); | 66 }); |
| 66 } | 67 } |
| 67 | 68 |
| 68 return swarming; | 69 return swarming; |
| 69 }(); | 70 }(); |
| OLD | NEW |