|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +function filterNumber (value) { |
| 19 | + if (/^[-+]?\d*\.?\d*$/.test(value)) { |
| 20 | + return Number(value) |
| 21 | + } |
| 22 | + return NaN |
| 23 | +} |
| 24 | + |
| 25 | +function stringComparator (a, b) { |
| 26 | + return a.localeCompare(b) |
| 27 | +} |
| 28 | + |
| 29 | +function numericComparator (a, b) { |
| 30 | + return filterNumber(a) < filterNumber(b) ? 1 : -1 |
| 31 | +} |
| 32 | + |
| 33 | +function ipV4AddressCIDRComparator (a, b) { |
| 34 | + a = a.split(/[./]/gm) |
| 35 | + b = b.split(/[./]/gm) |
| 36 | + for (var i = 0; i < a.length; i++) { |
| 37 | + if ((a[i] = parseInt(a[i])) < (b[i] = parseInt(b[i]))) { |
| 38 | + return -1 |
| 39 | + } else if (a[i] > b[i]) { |
| 40 | + return 1 |
| 41 | + } |
| 42 | + } |
| 43 | + return 0 |
| 44 | +} |
| 45 | + |
| 46 | +function ipV6AddressCIDRComparator (a, b) { |
| 47 | + a = a.split(/[:/]/gm) |
| 48 | + b = b.split(/[:/]/gm) |
| 49 | + for (var i = 0; i < a.length; i++) { |
| 50 | + if ((a[i] = parseInt('0x' + a[i], 16)) < (b[i] = parseInt('0x' + b[i], 16))) { |
| 51 | + return -1 |
| 52 | + } else if (a[i] > b[i]) { |
| 53 | + return 1 |
| 54 | + } |
| 55 | + } |
| 56 | + return 0 |
| 57 | +} |
| 58 | + |
| 59 | +function isIpV4Address (obj) { |
| 60 | + return !Array.isArray(obj) && (/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm).test(obj) |
| 61 | +} |
| 62 | + |
| 63 | +function isIpV6Address (obj) { |
| 64 | + return !Array.isArray(obj) && (/^[a-fA-F0-9:]+$/gm).test(obj) |
| 65 | +} |
| 66 | + |
| 67 | +function isIpV4CIDRAddress (obj) { |
| 68 | + return !Array.isArray(obj) && (/^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$/gm).test(obj) |
| 69 | +} |
| 70 | + |
| 71 | +function isIpV6CIDRAddress (obj) { |
| 72 | + return !Array.isArray(obj) && (/^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/gm).test(obj) |
| 73 | +} |
| 74 | + |
| 75 | +function isNumeric (obj) { |
| 76 | + return !Array.isArray(obj) && !isNaN(filterNumber(obj)) |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Compare elements, attempting to determine type of element to get the best comparison |
| 81 | + * |
| 82 | + */ |
| 83 | +export function genericCompare (a, b) { |
| 84 | + // strict function for filtering numbers (e.g. "2.3", "-2" but not "8 CPUs") |
| 85 | + var comparator = stringComparator |
| 86 | + |
| 87 | + if (a === b) { |
| 88 | + // Short circuit out to avoid unnecessary effort |
| 89 | + return 0 |
| 90 | + } |
| 91 | + if ((isIpV4CIDRAddress(a) || isIpV4Address(a)) && (isIpV4CIDRAddress(b) || isIpV4Address(b))) { |
| 92 | + comparator = ipV4AddressCIDRComparator |
| 93 | + } |
| 94 | + if ((isIpV6CIDRAddress(a) || isIpV6Address(a)) && (isIpV6CIDRAddress(b) || isIpV6Address(b))) { |
| 95 | + comparator = ipV6AddressCIDRComparator |
| 96 | + } |
| 97 | + if (isNumeric(a) && isNumeric(b)) { |
| 98 | + comparator = numericComparator |
| 99 | + } |
| 100 | + |
| 101 | + return comparator(a, b) |
| 102 | +} |
0 commit comments