@ztimson/utils 0.28.6 → 0.28.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1859,6 +1859,15 @@ function gravatar(email, def = "mp") {
1859
1859
  if (!email) return "";
1860
1860
  return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
1861
1861
  }
1862
+ function matchesCidr(ip, cidr) {
1863
+ if (!cidr) return true;
1864
+ if (!ip) return false;
1865
+ if (!cidr?.includes("/")) return ip === cidr;
1866
+ const [range, bits] = cidr.split("/");
1867
+ const mask = ~(2 ** (32 - parseInt(bits)) - 1);
1868
+ const ipToInt = (str) => str.split(".").reduce((int, octet) => (int << 8) + parseInt(octet), 0) >>> 0;
1869
+ return (ipToInt(ip) & mask) === (ipToInt(range) & mask);
1870
+ }
1862
1871
  function ipV6ToV4(ip) {
1863
1872
  if (!ip) return null;
1864
1873
  const ipv4 = ip.split(":").splice(-1)[0];
@@ -2795,6 +2804,7 @@ export {
2795
2804
  makeArray,
2796
2805
  makeUnique,
2797
2806
  matchAll,
2807
+ matchesCidr,
2798
2808
  md5,
2799
2809
  mixin,
2800
2810
  month,