8000 feat: Convert custom_attribute_condition_evaluator to TS by yavorona · Pull Request #561 · optimizely/javascript-sdk · GitHub
[go: up one dir, main page]

Skip to content

feat: Convert custom_attribute_condition_evaluator to TS #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

yavorona
Copy link
Contributor
@yavorona yavorona commented Sep 1, 2020

Summary

  • Convert custom_attribute_condition_evaluator module from JS to TS.

Test plan

  • Existing unit tests and local bundle

@yavorona yavorona requested a review from a team as a code owner September 1, 2020 23:58
@yavorona yavorona self-assigned this Sep 1, 2020
@yavorona yavorona force-pushed the pnguen/convert-custom-attribute-condition-evaluator-to-ts branch from 4856aca to 408e825 Compare September 2, 2020 00:00
@coveralls
Copy link
coveralls commented Sep 2, 2020

Coverage Status

Coverage increased (+0.02%) to 96.761% when pulling c91fd6f on pnguen/convert-custom-attribute-condition-evaluator-to-ts into 80b4108 on master.

Comment on lines 33 to 35
type Condition = {
[name: string]: string;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more specific here. You can check out the structure of condition objects in the datafile by creating audiences with different condition types.

Condition objects have type, name, match, and value properties. The functions in this module use name, match, and value.


/**
* Returns true if the value is valid for exact conditions. Valid values include
* strings, booleans, and numbers that aren't NaN, -Infinity, or Infinity.
* @param value
* @returns {Boolean}
* @returns {?boolean}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be boolean, not ?boolean?

import { isNumber, isSafeInteger } from '../../utils/fns';
import {
LOG_LEVEL,
LOG_MESSAGES,
} from '../../utils/enums';

var MODULE_NAME = 'CUSTOM_ATTRIBUTE_CONDITION_EVALUATOR';
// TODO: Find place for UserAttributes and Condition types to live in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could create a shared types module at lib/shared_types.ts, and put this there.

It would be nice if somehow we could import < 10000 code class="notranslate">UserAttributes from there and use it here, as well as also in lib/index.d.ts. I had some trouble figuring that out, it may not be possible. If so I think we can just duplicate the definition (have one in lib/index.d.ts, and one in lib/shared_types.ts which is imported by out TS modules).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tried to use UserAttributes in lib/index.d.ts from lib/shared_types.ts i, but could not figure it out how to make it work, so left lib/index.d.ts as is and followed your suggestion.

@yavorona yavorona force-pushed the pnguen/convert-custom-attribute-condition-evaluator-to-ts branch from 9bbbd51 to 408e825 Compare September 2, 2020 18:20
@yavorona yavorona removed their assignment Sep 2, 2020
* TODO: Change to accept and object with named properties
*/
export var evaluate = function(condition, userAttributes, logger) {
var conditionMatch = condition.match;
export function evaluate(condition: Condition, userAttributes: UserAttributes, logger: LoggerFacade): boolean | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good opportunity to clean up logging in this module.

  • Let's change all these functions so that they use a logger defined inside this module (like const logger = getLogger(MODULE_NAME);, and then remove the logger argument from the list of parameters.
  • Call the logger method corresponding to the desired level, rather than passing in level (logger.debug(...) instead of logger.log(LOG_LEVEL.DEBUG, ...)

Copy link
Contributor
@mjc1283 mjc1283 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks way better, thank you! Just one requested change then this should be ready to merge!

Comment on lines 82 to 89
if (!conditionMatch) {
evaluatorForMatch = exactEvaluator;
} else {
evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch];
}
if (!evaluatorForMatch) {
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the whole codebase was TS and passed type checking, I don't think control can ever reach line 88. But the compiler will complain if we remove this if (!evaluatorForMatch) check. We don't want to return null without logging a warning message like in the beginning of this function.

Let's do this instead:

Suggested change
if (!conditionMatch) {
evaluatorForMatch = exactEvaluator;
} else {
evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch];
}
if (!evaluatorForMatch) {
return null;
}
if (!conditionMatch) {
evaluatorForMatch = exactEvaluator;
} else {
evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch] || exactEvaluator;
}

@yavorona yavorona removed their assignment Sep 4, 2020
@yavorona yavorona merged commit f9d96e6 into master Sep 5, 2020
@yavorona yavorona deleted the pnguen/convert-custom-attribute-condition-evaluator-to-ts branch September 5, 2020 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0