posthog-node
Advanced tools
Comparing version 3.3.0 to 3.4.0
@@ -0,1 +1,6 @@ | ||
# 3.4.0 - 2024-01-09 | ||
1. Numeric property handling for feature flags now does the expected: When passed in a number, we do a numeric comparison. When passed in a string, we do a string comparison. Previously, we always did a string comparison. | ||
2. Add support for relative date operators for local evaluation. | ||
# 3.3.0 - 2024-01-02 | ||
@@ -2,0 +7,0 @@ |
@@ -58,2 +58,3 @@ /// <reference types="node" /> | ||
declare function matchProperty(property: FeatureFlagCondition['properties'][number], propertyValues: Record<string, any>): boolean; | ||
export { FeatureFlagsPoller, matchProperty, InconclusiveMatchError, ClientError }; | ||
declare function relativeDateParseForFeatureFlagMatching(value: string): Date | null; | ||
export { FeatureFlagsPoller, matchProperty, relativeDateParseForFeatureFlagMatching, InconclusiveMatchError, ClientError, }; |
{ | ||
"name": "posthog-node", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "PostHog Node.js integration", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -467,7 +467,28 @@ import { createHash } from 'rusha' | ||
function computeExactMatch(value: any, overrideValue: any): boolean { | ||
if (Array.isArray(value)) { | ||
return value.map((val) => String(val).toLowerCase()).includes(String(overrideValue).toLowerCase()) | ||
} | ||
return String(value).toLowerCase() === String(overrideValue).toLowerCase() | ||
} | ||
function compare(lhs: any, rhs: any, operator: string): boolean { | ||
if (operator === 'gt') { | ||
return lhs > rhs | ||
} else if (operator === 'gte') { | ||
return lhs >= rhs | ||
} else if (operator === 'lt') { | ||
return lhs < rhs | ||
} else if (operator === 'lte') { | ||
return lhs <= rhs | ||
} else { | ||
throw new Error(`Invalid operator: ${operator}`) | ||
} | ||
} | ||
switch (operator) { | ||
case 'exact': | ||
return Array.isArray(value) ? value.indexOf(overrideValue) !== -1 : value === overrideValue | ||
return computeExactMatch(value, overrideValue) | ||
case 'is_not': | ||
return Array.isArray(value) ? value.indexOf(overrideValue) === -1 : value !== overrideValue | ||
return !computeExactMatch(value, overrideValue) | ||
case 'is_set': | ||
@@ -484,14 +505,44 @@ return key in propertyValues | ||
case 'gt': | ||
return typeof overrideValue == typeof value && overrideValue > value | ||
case 'gte': | ||
return typeof overrideValue == typeof value && overrideValue >= value | ||
case 'lt': | ||
return typeof overrideValue == typeof value && overrideValue < value | ||
case 'lte': | ||
return typeof overrideValue == typeof value && overrideValue <= value | ||
case 'lte': { | ||
// :TRICKY: We adjust comparison based on the override value passed in, | ||
// to make sure we handle both numeric and string comparisons appropriately. | ||
let parsedValue = typeof value === 'number' ? value : null | ||
if (typeof value === 'string') { | ||
try { | ||
parsedValue = parseFloat(value) | ||
} catch (err) { | ||
// pass | ||
} | ||
} | ||
if (parsedValue != null && overrideValue != null) { | ||
// check both null and undefined | ||
if (typeof overrideValue === 'string') { | ||
return compare(overrideValue, String(value), operator) | ||
} else { | ||
return compare(overrideValue, parsedValue, operator) | ||
} | ||
} else { | ||
return compare(String(overrideValue), String(value), operator) | ||
} | ||
} | ||
case 'is_date_after': | ||
case 'is_date_before': { | ||
const parsedDate = convertToDateTime(value) | ||
case 'is_date_before': | ||
case 'is_relative_date_before': | ||
case 'is_relative_date_after': { | ||
let parsedDate = null | ||
if (['is_relative_date_before', 'is_relative_date_after'].includes(operator)) { | ||
parsedDate = relativeDateParseForFeatureFlagMatching(String(value)) | ||
} else { | ||
parsedDate = convertToDateTime(value) | ||
} | ||
if (parsedDate == null) { | ||
throw new InconclusiveMatchError(`Invalid date: ${value}`) | ||
} | ||
const overrideDate = convertToDateTime(overrideValue) | ||
if (operator === 'is_date_before') { | ||
if (['is_date_before', 'is_relative_date_before'].includes(operator)) { | ||
return overrideDate < parsedDate | ||
@@ -502,4 +553,3 @@ } | ||
default: | ||
console.error(`Unknown operator: ${operator}`) | ||
return false | ||
throw new InconclusiveMatchError(`Unknown operator: ${operator}`) | ||
} | ||
@@ -642,2 +692,45 @@ } | ||
export { FeatureFlagsPoller, matchProperty, InconclusiveMatchError, ClientError } | ||
function relativeDateParseForFeatureFlagMatching(value: string): Date | null { | ||
const regex = /^(?<number>[0-9]+)(?<interval>[a-z])$/ | ||
const match = value.match(regex) | ||
const parsedDt = new Date(new Date().toISOString()) | ||
if (match) { | ||
if (!match.groups) { | ||
return null | ||
} | ||
const number = parseInt(match.groups['number']) | ||
if (number >= 10000) { | ||
// Guard against overflow, disallow numbers greater than 10_000 | ||
return null | ||
} | ||
const interval = match.groups['interval'] | ||
if (interval == 'h') { | ||
parsedDt.setUTCHours(parsedDt.getUTCHours() - number) | ||
} else if (interval == 'd') { | ||
parsedDt.setUTCDate(parsedDt.getUTCDate() - number) | ||
} else if (interval == 'w') { | ||
parsedDt.setUTCDate(parsedDt.getUTCDate() - number * 7) | ||
} else if (interval == 'm') { | ||
parsedDt.setUTCMonth(parsedDt.getUTCMonth() - number) | ||
} else if (interval == 'y') { | ||
parsedDt.setUTCFullYear(parsedDt.getUTCFullYear() - number) | ||
} else { | ||
return null | ||
} | ||
return parsedDt | ||
} else { | ||
return null | ||
} | ||
} | ||
export { | ||
FeatureFlagsPoller, | ||
matchProperty, | ||
relativeDateParseForFeatureFlagMatching, | ||
InconclusiveMatchError, | ||
ClientError, | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
943450
13115