-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(utils): Add parameterize
function
#9145
Merged
Lms24
merged 32 commits into
getsentry:develop
from
AleshaOleg:feature/added-parametrize-function
Jan 5, 2024
Merged
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
db66295
Added parametrize function
AleshaOleg e15d5f1
Populate logentry parameter if it's exist for eventFromMessage functions
AleshaOleg 7b27a1e
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg e4c4d4c
Renamed parametrize to parameterize and moved ParameterizedString typ…
AleshaOleg 8fa0552
Added integration tests
AleshaOleg d83411a
Fixing tests and improvements
AleshaOleg 70e6864
isParameterized -> isParameterizedString in docs
AleshaOleg cbd0805
Fixed lint errors
AleshaOleg 1020b14
Minor optimization
AleshaOleg 99c4fbb
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg 3cbe665
Minor improvements, reverted prisma update
AleshaOleg de6e723
Fixed typo
AleshaOleg 28758b6
Fixed typo
AleshaOleg 17a4bbd
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg 2c3393f
prisma@3.12.0 -> prisma@^3.12.0
AleshaOleg f6347a3
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg cefa81c
prisma@^3.12.0 -> prisma@3.12.0
AleshaOleg e80f23a
prisma@3.12.0 -> prisma@^3.12.0
AleshaOleg 3b67901
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg 2789d04
prisma@3.12.0 -> prisma@^3.12.0
AleshaOleg 3e3b441
Merge branch 'develop' of github.com:AleshaOleg/sentry-javascript int…
AleshaOleg 0d67997
Fixed Biome errors
AleshaOleg dcd1549
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg ca8a58a
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg 8c8bf10
Skipping test for some bundles
AleshaOleg a631c55
Prevent running test for tracing builds
AleshaOleg aa3c9d1
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg de11d01
Skipping all test for bundle builds
AleshaOleg 5aeedf7
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg f68be1f
Test migration to dev-packages folder
AleshaOleg 5681add
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg 4b0c53c
Merge branch 'develop' into feature/added-parametrize-function
AleshaOleg 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 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 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 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 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 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,20 @@ | ||
export interface ParamerizedString extends String { | ||
__sentry_template_string__: string; | ||
__sentry_template_values__: string[]; | ||
} | ||
|
||
/** | ||
* Tagged template function which returns paramaterized representation of the message | ||
* For example: parametrize`This is a log statement with ${x} and ${y} params`, would return: | ||
* "__sentry_template_string__": "My raw message with interpreted strings like %s", | ||
* "__sentry_template_values__": ["this"] | ||
* @param strings An array of string values splitted between expressions | ||
* @param values Expressions extracted from template string | ||
* @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties | ||
*/ | ||
export function parametrize(strings: TemplateStringsArray, ...values: string[]): ParamerizedString { | ||
const formatted = new String(String.raw(strings, ...values)) as ParamerizedString; | ||
formatted.__sentry_template_string__ = strings.join('\x00').replace(/%/g, '%%').replace(/\0/g, '%s'); | ||
formatted.__sentry_template_values__ = values; | ||
return formatted; | ||
} |
This file contains 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,26 @@ | ||
import type { ParamerizedString } from '../src/parametrize'; | ||
import { parametrize } from '../src/parametrize'; | ||
|
||
describe('parametrize()', () => { | ||
test('works with empty string', () => { | ||
const string = new String() as ParamerizedString; | ||
string.__sentry_template_string__ = ''; | ||
string.__sentry_template_values__ = []; | ||
|
||
const formatted = parametrize``; | ||
expect(formatted.__sentry_template_string__).toEqual(''); | ||
expect(formatted.__sentry_template_values__).toEqual([]); | ||
}); | ||
|
||
test('works as expected with template literals', () => { | ||
const x = 'first'; | ||
const y = 'second'; | ||
const string = new String() as ParamerizedString; | ||
string.__sentry_template_string__ = 'This is a log statement with %s and %s params'; | ||
string.__sentry_template_values__ = ['first', 'second']; | ||
|
||
const formatted = parametrize`This is a log statement with ${x} and ${y} params`; | ||
expect(formatted.__sentry_template_string__).toEqual(string.__sentry_template_string__); | ||
expect(formatted.__sentry_template_values__).toEqual(string.__sentry_template_values__); | ||
}); | ||
}); |
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.
Can we extract
ParemeterizedString
from this PR as a type export it from @sentry/types? Then we can reuse it here.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.
Tried to replace it everywhere where I think it makes sense. Thanks for the tip!