Skip to content

Commit

Permalink
Add helper methods for converting numeric/datetime/string array value…
Browse files Browse the repository at this point in the history
…s to User Object attribute values
  • Loading branch information
adams85 committed Oct 30, 2023
1 parent 3949d6d commit 96f6570
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/RolloutEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ import { errorToString, formatStringList, isArray, utf8Encode } from "./Utils";

/** User Object. Contains user attributes which are used for evaluating targeting rules and percentage options. */
export class User {
/**
* Converts the specified `Date` value to the format expected by datetime comparison operators (BEFORE/AFTER).
* @param date The `Date` value to convert.
* @returns The User Object attribute value in the expected format.
*/
static attributeValueFromDate(date: Date): string {
const unixTimeSeconds = date.getTime() / 1000.0;
return unixTimeSeconds + "";
}

/**
* Converts the specified `number` value to the format expected by number comparison operators.
* @param number The `number` value to convert.
* @returns The User Object attribute value in the expected format.
*/
static attributeValueFromNumber(number: number): string {
return number + "";
}

/**
* Converts the specified `string` items to the format expected by array comparison operators (ARRAY CONTAINS ANY OF/ARRAY NOT CONTAINS ANY OF).
* @param items The `string` items to convert.
* @returns The User Object attribute value in the expected format.
*/
static attributeValueFromStringArray(...items: ReadonlyArray<string>): string {
// TODO: What to do when items contain non string items?
return JSON.stringify(items);
}

constructor(
/** The unique identifier of the user or session (e.g. email address, primary key, session ID, etc.) */
public identifier: string,
Expand Down

0 comments on commit 96f6570

Please sign in to comment.