-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Convert event_tag_utils module to TS #544
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
yavorona
merged 7 commits into
mcarroll/optimizely-sdk-typescript
from
pnguen/convert-event-tags-utils-to-ts
Jul 30, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9617f6
Convert event_tag_utils module to TS
yavorona 995b82d
Merge branch 'mcarroll/optimizely-sdk-typescript' of github.com:optim…
yavorona 3eb9074
Merge branch 'mcarroll/optimizely-sdk-typescript' of github.com:optim…
yavorona 0d9ca74
Convert event_tag_utils module to TS
yavorona 7caeddd
Add type guard to avoid using any
yavorona f69fd61
Move 'if (eventTags)' to top level functions
yavorona a61a375
Move let variables to inside the functions
yavorona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 0 additions & 74 deletions
74
packages/optimizely-sdk/lib/utils/event_tag_utils/index.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/optimizely-sdk/lib/utils/event_tag_utils/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright 2017, 2019-2020 Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { sprintf } from '@optimizely/js-sdk-utils'; | ||
|
||
import { EventTags } from '@optimizely/js-sdk-event-processor'; | ||
import { LoggerFacade } from '@optimizely/js-sdk-logging'; | ||
|
||
import { | ||
LOG_LEVEL, | ||
LOG_MESSAGES, | ||
RESERVED_EVENT_KEYWORDS, | ||
} from '../enums'; | ||
|
||
/** | ||
* Provides utility method for parsing event tag values | ||
*/ | ||
const MODULE_NAME = 'EVENT_TAG_UTILS'; | ||
const REVENUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.REVENUE; | ||
const VALUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.VALUE; | ||
|
||
/** | ||
* Grab the revenue value from the event tags. "revenue" is a reserved keyword. | ||
* @param {EventTags} eventTags | ||
* @param {LoggerFacade} logger | ||
* @return {number|null} | ||
*/ | ||
export function getRevenueValue(eventTags: EventTags, logger: LoggerFacade): number | null { | ||
mjc1283 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (eventTags.hasOwnProperty(REVENUE_EVENT_METRIC_NAME)) { | ||
const rawValue = eventTags[REVENUE_EVENT_METRIC_NAME]; | ||
let parsedRevenueValue; | ||
if (typeof rawValue === 'string') { | ||
parsedRevenueValue = parseInt(rawValue); | ||
if (isNaN(parsedRevenueValue)) { | ||
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_REVENUE, MODULE_NAME, rawValue)); | ||
return null; | ||
} | ||
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue)); | ||
return parsedRevenueValue; | ||
} | ||
if (typeof rawValue === 'number') { | ||
parsedRevenueValue = rawValue; | ||
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue)); | ||
return parsedRevenueValue; | ||
} | ||
return null; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Grab the event value from the event tags. "value" is a reserved keyword. | ||
* @param {EventTags} eventTags | ||
* @param {LoggerFacade} logger | ||
* @return {number|null} | ||
*/ | ||
export function getEventValue(eventTags: EventTags, logger: LoggerFacade): number | null { | ||
mjc1283 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (eventTags.hasOwnProperty(VALUE_EVENT_METRIC_NAME)) { | ||
const rawValue = eventTags[VALUE_EVENT_METRIC_NAME]; | ||
let parsedEventValue; | ||
if (typeof rawValue === 'string') { | ||
parsedEventValue = parseFloat(rawValue); | ||
if (isNaN(parsedEventValue)) { | ||
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_VALUE, MODULE_NAME, rawValue)); | ||
return null; | ||
} | ||
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue)); | ||
return parsedEventValue; | ||
} | ||
if (typeof rawValue === 'number') { | ||
parsedEventValue = rawValue; | ||
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue)); | ||
return parsedEventValue; | ||
} | ||
return null; | ||
} | ||
return null; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mjc1283 FYI
if (eventTags) {...}
already exists inevent_builder/index.js
, so this is the only place where we need to put it.