@se-oss/ip-address 1.0.0 → 1.1.0

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.cjs CHANGED
@@ -27,6 +27,7 @@ __export(index_exports, {
27
27
  bigIntToIp: () => bigIntToIp,
28
28
  fastIpVersion: () => fastIpVersion,
29
29
  ipToBigInt: () => ipToBigInt,
30
+ isValidIP: () => isValidIP,
30
31
  parseCIDR: () => parseCIDR,
31
32
  parseIP: () => parseIP
32
33
  });
@@ -237,9 +238,6 @@ var IPv6 = class _IPv6 {
237
238
  * IPv6.isValid('2001:db8::g'); // false
238
239
  */
239
240
  static isValid(address) {
240
- if (address.includes("/")) {
241
- return false;
242
- }
243
241
  return IPV6_VALIDATION_REGEX.test(address);
244
242
  }
245
243
  // --- Getters ---
@@ -498,9 +496,6 @@ var IPv4 = class _IPv4 {
498
496
  * IPv4.isValid('1.2.3.256'); // false
499
497
  */
500
498
  static isValid(address) {
501
- if (address.includes("/")) {
502
- return false;
503
- }
504
499
  return IPV4_ADDRESS_REGEX.test(address);
505
500
  }
506
501
  // --- Getters ---
@@ -724,14 +719,9 @@ var CIDR = class {
724
719
  * @throws {InvalidIPAddressError} If the CIDR string is invalid.
725
720
  */
726
721
  constructor(cidr) {
727
- const [addressPart, prefixPart] = cidr.split("/");
728
- if (!prefixPart) {
729
- throw new InvalidIPAddressError(`Invalid CIDR notation: ${cidr}`);
730
- }
731
- const prefix = parseInt(prefixPart, 10);
732
- if (isNaN(prefix)) {
733
- throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
734
- }
722
+ const parts = cidr.split("/");
723
+ const addressPart = parts[0];
724
+ let prefixPart = parts[1];
735
725
  let address;
736
726
  let version;
737
727
  if (addressPart.includes(".")) {
@@ -741,6 +731,13 @@ var CIDR = class {
741
731
  address = new IPv6(addressPart);
742
732
  version = 6;
743
733
  }
734
+ if (prefixPart === void 0) {
735
+ prefixPart = String(BITS[version]);
736
+ }
737
+ const prefix = parseInt(prefixPart, 10);
738
+ if (isNaN(prefix)) {
739
+ throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
740
+ }
744
741
  if (prefix < 0 || prefix > BITS[version]) {
745
742
  throw new InvalidIPAddressError(`Prefix out of range for IPv${version}: ${prefix}`);
746
743
  }
@@ -878,6 +875,12 @@ function parseIP(address) {
878
875
  function parseCIDR(cidr) {
879
876
  return new CIDR(cidr);
880
877
  }
878
+ function isValidIP(maybeIP) {
879
+ if (typeof maybeIP !== "string") {
880
+ return false;
881
+ }
882
+ return IPv4.isValid(maybeIP) || IPv6.isValid(maybeIP);
883
+ }
881
884
  // Annotate the CommonJS export names for ESM import in node:
882
885
  0 && (module.exports = {
883
886
  CIDR,
@@ -887,6 +890,7 @@ function parseCIDR(cidr) {
887
890
  bigIntToIp,
888
891
  fastIpVersion,
889
892
  ipToBigInt,
893
+ isValidIP,
890
894
  parseCIDR,
891
895
  parseIP
892
896
  });
package/dist/index.d.ts CHANGED
@@ -496,5 +496,18 @@ declare function parseIP(address: string): IP;
496
496
  * @throws {InvalidIPAddressError} if the cidr is not a valid v4 or v6 cidr.
497
497
  */
498
498
  declare function parseCIDR(cidr: string): CIDR<IP>;
499
+ /**
500
+ * Safely checks if a value is a string representing a valid IPv4 or IPv6 address.
501
+ *
502
+ * @param maybeIP The value to check.
503
+ * @returns {boolean} True if the value is a valid IP address string.
504
+ *
505
+ * @example
506
+ * isValidIP('192.168.1.1'); // true
507
+ * isValidIP('2001:db8::1'); // true
508
+ * isValidIP('not an ip'); // false
509
+ * isValidIP(null); // false
510
+ */
511
+ declare function isValidIP(maybeIP: unknown): boolean;
499
512
 
500
- export { CIDR, type IP, type IPVersion, IPv4, IPv6, InvalidIPAddressError, bigIntToIp, fastIpVersion, ipToBigInt, parseCIDR, parseIP };
513
+ export { CIDR, type IP, type IPVersion, IPv4, IPv6, InvalidIPAddressError, bigIntToIp, fastIpVersion, ipToBigInt, isValidIP, parseCIDR, parseIP };
package/dist/index.js CHANGED
@@ -203,9 +203,6 @@ var IPv6 = class _IPv6 {
203
203
  * IPv6.isValid('2001:db8::g'); // false
204
204
  */
205
205
  static isValid(address) {
206
- if (address.includes("/")) {
207
- return false;
208
- }
209
206
  return IPV6_VALIDATION_REGEX.test(address);
210
207
  }
211
208
  // --- Getters ---
@@ -464,9 +461,6 @@ var IPv4 = class _IPv4 {
464
461
  * IPv4.isValid('1.2.3.256'); // false
465
462
  */
466
463
  static isValid(address) {
467
- if (address.includes("/")) {
468
- return false;
469
- }
470
464
  return IPV4_ADDRESS_REGEX.test(address);
471
465
  }
472
466
  // --- Getters ---
@@ -690,14 +684,9 @@ var CIDR = class {
690
684
  * @throws {InvalidIPAddressError} If the CIDR string is invalid.
691
685
  */
692
686
  constructor(cidr) {
693
- const [addressPart, prefixPart] = cidr.split("/");
694
- if (!prefixPart) {
695
- throw new InvalidIPAddressError(`Invalid CIDR notation: ${cidr}`);
696
- }
697
- const prefix = parseInt(prefixPart, 10);
698
- if (isNaN(prefix)) {
699
- throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
700
- }
687
+ const parts = cidr.split("/");
688
+ const addressPart = parts[0];
689
+ let prefixPart = parts[1];
701
690
  let address;
702
691
  let version;
703
692
  if (addressPart.includes(".")) {
@@ -707,6 +696,13 @@ var CIDR = class {
707
696
  address = new IPv6(addressPart);
708
697
  version = 6;
709
698
  }
699
+ if (prefixPart === void 0) {
700
+ prefixPart = String(BITS[version]);
701
+ }
702
+ const prefix = parseInt(prefixPart, 10);
703
+ if (isNaN(prefix)) {
704
+ throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
705
+ }
710
706
  if (prefix < 0 || prefix > BITS[version]) {
711
707
  throw new InvalidIPAddressError(`Prefix out of range for IPv${version}: ${prefix}`);
712
708
  }
@@ -844,6 +840,12 @@ function parseIP(address) {
844
840
  function parseCIDR(cidr) {
845
841
  return new CIDR(cidr);
846
842
  }
843
+ function isValidIP(maybeIP) {
844
+ if (typeof maybeIP !== "string") {
845
+ return false;
846
+ }
847
+ return IPv4.isValid(maybeIP) || IPv6.isValid(maybeIP);
848
+ }
847
849
  export {
848
850
  CIDR,
849
851
  IPv4,
@@ -852,6 +854,7 @@ export {
852
854
  bigIntToIp,
853
855
  fastIpVersion,
854
856
  ipToBigInt,
857
+ isValidIP,
855
858
  parseCIDR,
856
859
  parseIP
857
860
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@se-oss/ip-address",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A modern, immutable, and zero-dependency library for working with IPv4 and IPv6 addresses in JavaScript and TypeScript.",
5
5
  "keywords": [
6
6
  "ip",