File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ export interface ParamerizedString extends String {
2+ __sentry_template_string__ : string ;
3+ __sentry_template_values__ : string [ ] ;
4+ }
5+
6+ /**
7+ * Tagged template function which returns paramaterized representation of the message
8+ * For example: parametrize`This is a log statement with ${x} and ${y} params`, would return:
9+ * "__sentry_template_string__": "My raw message with interpreted strings like %s",
10+ * "__sentry_template_values__": ["this"]
11+ * @param strings An array of string values splitted between expressions
12+ * @param values Expressions extracted from template string
13+ * @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties
14+ */
15+ export function parametrize ( strings : TemplateStringsArray , ...values : string [ ] ) : ParamerizedString {
16+ const formatted = new String ( String . raw ( strings , ...values ) ) as ParamerizedString ;
17+ formatted . __sentry_template_string__ = strings . join ( '\x00' ) . replace ( / % / g, '%%' ) . replace ( / \0 / g, '%s' ) ;
18+ formatted . __sentry_template_values__ = values ;
19+ return formatted ;
20+ }
Original file line number Diff line number Diff line change 1+ import type { ParamerizedString } from '../src/parametrize' ;
2+ import { parametrize } from '../src/parametrize' ;
3+
4+ describe ( 'parametrize()' , ( ) => {
5+ test ( 'works with empty string' , ( ) => {
6+ const string = new String ( ) as ParamerizedString ;
7+ string . __sentry_template_string__ = '' ;
8+ string . __sentry_template_values__ = [ ] ;
9+
10+ const formatted = parametrize `` ;
11+ expect ( formatted . __sentry_template_string__ ) . toEqual ( '' ) ;
12+ expect ( formatted . __sentry_template_values__ ) . toEqual ( [ ] ) ;
13+ } ) ;
14+
15+ test ( 'works as expected with template literals' , ( ) => {
16+ const x = 'first' ;
17+ const y = 'second' ;
18+ const string = new String ( ) as ParamerizedString ;
19+ string . __sentry_template_string__ = 'This is a log statement with %s and %s params' ;
20+ string . __sentry_template_values__ = [ 'first' , 'second' ] ;
21+
22+ const formatted = parametrize `This is a log statement with ${ x } and ${ y } params` ;
23+ expect ( formatted . __sentry_template_string__ ) . toEqual ( string . __sentry_template_string__ ) ;
24+ expect ( formatted . __sentry_template_values__ ) . toEqual ( string . __sentry_template_values__ ) ;
25+ } ) ;
26+ } ) ;
You can’t perform that action at this time.
0 commit comments