/** * Internal dependencies */ import { getExtraSegmentationInfo } from '../index'; import type { Attributes } from '../../../../../../packages/segmentation-rule-library/rules/ip-address/types'; export const type = 'nab/ip-address'; export function validate( attributes: Attributes ): boolean { const { condition, values } = attributes; const visitorIp = getExtraSegmentationInfo().ipAddress; if ( 'string' !== typeof visitorIp ) { return false; } switch ( condition ) { case 'is-equal-to': return values.some( ( ipPattern ) => matches( visitorIp, ipPattern ) ); case 'is-not-equal-to': return values.every( ( ipPattern ) => ! matches( visitorIp, ipPattern ) ); } } function matches( ip: string, pattern: string ) { const re = pattern.replace( '*', '\\d?\\d?\\d' ).replace( '.', '\\.' ); return new RegExp( re ).test( ip ); }