|
|
import type { ExperimentAssignment } from '../types'; |
|
|
|
|
|
export function isObject( x: unknown ): x is Record< string, unknown > { |
|
|
return typeof x === 'object' && x !== null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isName( name: unknown ): name is string { |
|
|
return typeof name === 'string' && name !== '' && /^[a-z0-9_]*$/.test( name ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isExperimentAssignment( |
|
|
experimentAssignment: unknown |
|
|
): experimentAssignment is ExperimentAssignment { |
|
|
return ( |
|
|
isObject( experimentAssignment ) && |
|
|
isName( experimentAssignment[ 'experimentName' ] ) && |
|
|
( isName( experimentAssignment[ 'variationName' ] ) || |
|
|
experimentAssignment[ 'variationName' ] === null ) && |
|
|
typeof experimentAssignment[ 'retrievedTimestamp' ] === 'number' && |
|
|
typeof experimentAssignment[ 'ttl' ] === 'number' && |
|
|
experimentAssignment[ 'ttl' ] !== 0 |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function validateExperimentAssignment( |
|
|
experimentAssignment: unknown |
|
|
): ExperimentAssignment { |
|
|
if ( ! isExperimentAssignment( experimentAssignment ) ) { |
|
|
throw new Error( 'Invalid ExperimentAssignment' ); |
|
|
} |
|
|
|
|
|
return experimentAssignment; |
|
|
} |
|
|
|